部落格 http://blog.csdn.net/androidbluetooth/article/details/6860146 詳細的粘貼了很多判斷網絡的方法。
推薦一個網站,關于人工智能教程,教程不僅是零基礎,通俗易懂,而且非常風趣幽默,像看小說一樣!覺得太牛了,是以分享給大家。點 這裡 可以跳轉到教程。
最近,遇到這樣一個需求:
手機可以随時監聽網絡狀态,如果網絡狀态發生變化要及時的更新 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.