Android 启动Service监听网络状态

我们在做多线程下载的时候,或者是在加载h5界面的时候,常常会遇到网络状态不好或者断网的时候,在这或者当我们的应用程序启动没有退出的时候,我们就需要对网络状态监听加以判断。

这时候,我们一般情况下,两种方式进行处理。

第一: 开启服务。

第二:发送广播的形式。

建议采用方法二。


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  registerReceiver(connectionReceiver, intentFilter);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 public boolean isNetworkConnected(Context context) {
  if (context != null) {
   ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     .getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
   if (mNetworkInfo != null) {
    return mNetworkInfo.isAvailable();
   }
  }
  return false;
 }
 public boolean isWifiConnected(Context context) {
  if (context != null) {
   ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     .getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo mWiFiNetworkInfo = mConnectivityManager
     .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
   if (mWiFiNetworkInfo != null) {
    return mWiFiNetworkInfo.isAvailable();
   }
  }
  return false;
 }
 public boolean isMobileConnected(Context context) {
  if (context != null) {
   ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     .getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo mMobileNetworkInfo = mConnectivityManager
     .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
   if (mMobileNetworkInfo != null) {
    return mMobileNetworkInfo.isAvailable();
   }
  }
  return false;
 }

 public static int getConnectedType(Context context) {
  if (context != null) {
   ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     .getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
   if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
    return mNetworkInfo.getType();
   }
  }
  return -1;
 }
 BroadcastReceiver	connectionReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
   ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
   NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
   NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
   if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
    Log.i(TAG, "unconnect");
    // unconnect network
   }else {
    // connect network
   }
  }
 };
 /*@Override
 protected void onDestroy() {

 };*/

 @Override
 protected void onDestroy() {
  if (connectionReceiver != null) {
   unregisterReceiver(connectionReceiver);
  }


  super.onDestroy();
 }


广播:源代码如下:


/**
	 * 有网络的广播
	 */
	BroadcastReceiver	connectionReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
			NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
				Log.i("yuyahao", "断网了");
				Message msg = new Message();
				msg.what = DownLoadFileUtil.isNetConnectionError;
				mHandler.sendMessage(msg);
				// unconnect network
			}else {
				// connect network
				Log.i(TAG, "有来网了");
			}
		}
	};

注册:

//有网络	的时候在注册网络监听广播
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
			registerReceiver(connectionReceiver, intentFilter);
			showDialog();
			pool.execute(mUpdateUIThread);
其完整的代码如下:


方法1就是开启service的方式。代码很简单。

demo下载地址如下:

监听网络状态demo.jar


你可能感兴趣的:(Android,广播监听网络状态,开发经历,Android总结,android,网络状态,android,如何监听网络状态)