Android中怎么判断手机是否联网,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联公司主营港南网站建设的网络公司,主营网站建设方案,重庆App定制开发,港南h5微信小程序开发搭建,港南网站营销推广欢迎港南等地区企业咨询android欢迎页自动跳转及手机网络检测,具体实现代码如下所示:
在1.2s后从欢迎页(welcome)跳转到登陆页(Login_Activity),并进行手机网络检测
package com.example.amusic.welcome;import android.content.Intent;import android.content.IntentFilter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import com.example.amusic.monitor.NetworkChangeReceiver;import com.example.amusic.R;import java.util.Timer;import java.util.TimerTask;public class welcome extends AppCompatActivity { private final int SKIP_DELAY_TIME = 1200; private NetworkChangeReceiver networkChangeReceiver; private IntentFilter intentFilter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); //手机网络检测 intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver = new NetworkChangeReceiver(); registerReceiver ( networkChangeReceiver, intentFilter); Timer time = new Timer(); TimerTask task = new TimerTask(){ @Override public void run() { startActivity(new Intent(welcome.this,Login_Activity.class)); finish(); //onDestroy(); } }; time.schedule(task, SKIP_DELAY_TIME); } protected void onDestroy(){ super.onDestroy(); unregisterReceiver( networkChangeReceiver); }}// An highlighted blockvar foo = 'bar';public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo!= null&&networkInfo.isAvailable()) { Toast.makeText(context, "手机网络已开启!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "手机网络未开启!", Toast.LENGTH_SHORT).show(); } }}// An highlighted blockvar foo = 'bar';
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。
标题名称:Android中怎么判断手机是否联网-创新互联
网站网址:http://lswzjz.com/article/phshg.html