這裡http://www.linuxidc.com/Linux/2012-02/55379.htm詳細的粘貼了很多判斷網絡的方法。
最近,遇到這樣一個需求:
手機可以隨時監聽網絡狀態,如果網絡狀態發生變化要及時的更新 app 信息通知用戶。
實現這個需求,有個較好的辦法(個人認為,你一定有更好的辦法,希望分享),分享給大家!
隨時監聽,需要實現一個 service 在後台監聽網絡狀態,那麽如何接收到網絡狀態發生變化的信息呢?
恩,當然是 BroadcastReceiver.
網絡狀態發生變化的時候,系統會發出 Android.net.conn.CONNECTIVITY_CHANGE .
該值詳細描述如下:
public static final String CONNECTIVITY_ACTION
Since: API Level 1
A change in network connectivity has occurred.
A connection has either been established or lost.
The NetworkInfo for the affected network is sent as an extra;
it should be consulted to see what kind of connectivity event occurred.
If this is a connection that was the result of failing over from a disconnected network,
then the FAILOVER_CONNECTION boolean extra is set to true.
For a loss of connectivity,
if the connectivity manager is attempting to connect (or has already connected) to another network,
the NetworkInfo for the new network is also passed as an extra.
This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible.
Instead, the reciever should expect another broadcast soon,
indicating either that the failover attempt succeeded (and so there is still overall data connectivity),
or that the failover attempt failed, meaning that all connectivity has been lost.
For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all.
Constant Value: "android.net.conn.CONNECTIVITY_CHANGE"
這是 ConnectivityManager 類的一個常量。
ok,下面是實現的 demo :
package mark.zhang;
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 ListenNetStateService extends Service {
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("mark", "網絡狀態已經改變");
connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
info = connectivityManager.getActiveNetworkInfo();
if(info != null && info.isAvailable()) {
String name = info.getTypeName();
Log.d("mark", "當前網絡名稱:" + name);
} else {
Log.d("mark", "沒有可用網絡");
}
}
}
};
@Override
public IBinder onBind(Intent intent) {
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);
}
}
在 manifest 文件中需要加上一條權限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
回頭再看看關於 CONNECTIVITY_ACTION 的介紹,從 api 中,我們還可以得到一個信息:
通過 intent 可以獲取一些 EXTRA,如 EXTRA_NO_CONNECTIVITY。
boolean b = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
更多信息可以參考 ConnectivityManager.