原文路径:http://www.xue5.com/Mobile/Android/655948.html
很多时候我们都需要实时监听网络状态,当网络状态发生变化后通知程序实行不同的策略。
这里,我采用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);
二、也可以接收广播WifiManager.NETWORK_STATE_CHANGED_ACTION
方法:
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
Log.e(TAG, "WifiManager.NETWORK_STATE_CHANGED_ACTION");
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo.getState() != null
&& NetworkInfo.State.CONNECTED == mWiFiNetworkInfo.getState()) {
Log.e(TAG, "NetworkInfo.State.CONNECTED");
} else if (NetworkInfo.State.DISCONNECTED == mWiFiNetworkInfo.getState()) {
Log.e(TAG, "NetworkInfo.State.DISCONNECTED");
}
}
注:判断时要用NetworkInfo的getState()方法判断是否和NetworkInfo.State.CONNECTED或NetworkInfo.State.DISCONNECTED或其他状态相等。这样可以确保只有在连接成功后只走一次if (mWiFiNetworkInfo.getState() != null && NetworkInfo.State.CONNECTED == mWiFiNetworkInfo.getState())判断 (第一种方法中的判断在某些手机上会走多次if(info != null && info.isAvailable()) {}判断。方法二就是为了解决这个问题)