很多时候我们都需要实时监听网络状态,当网络状态发生变化后通知程序实行不同的策略。
这里,我采用Service+BroadcastReceiver的形式实时监听网络变化。
原理是程序在背后启一个Service,在Service中注册一个广播,当网络变化时会产生一个ConnectivityManager.CONNECTIVITY_ACTION事件,只要监听这个事件就可以了。代码如下:

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;

public class NetworkStateService extends Service{

	private static final String tag="tag";
	private ConnectivityManager connectivityManager;
    private NetworkInfo info;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                Log.d(tag, "网络状态已经改变");
                connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                info = connectivityManager.getActiveNetworkInfo();  
                if(info != null && info.isAvailable()) {
                    String name = info.getTypeName();
                    Log.d(tag, "当前网络名称:" + name);
                    //doSomething()
                } else {
                    Log.d(tag, "没有可用网络");
                  //doSomething()
                }
            }
        }
    };
    
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		IntentFilter mFilter = new IntentFilter();
		mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(mReceiver, mFilter);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}
	
}




别忘了在Manifast中配置这个Service:


<service android:enabled="true" android:name="com.reyo.util.NetworkStateService" />



还有权限:


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



在必要的地方启动该Service即可:


Intent i=new Intent(context,NetworkStateService.class);
startService(i);




另外附加几个

Android网络连接的判断


1)判断是否有网络连接


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;  
 }



2)判断WIFI网络是否可用


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;  
 }



3)判断MOBILE网络是否可用


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;  
 }



4)获取当前网络连接的类型信息


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;  
 }