参考:http://blog.csdn.net/androidbluetooth/article/details/7199708
博客 http://blog.csdn.net/androidbluetooth/article/details/6860146 详细的粘贴了很多判断网络的方法。
网络状态发生变化的时候,系统会发出 android.net.conn.CONNECTIVITY_CHANGE .
下面是实现的 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.
===========================================
在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开恩?今天我们就针对这一应用技巧进行一个详细的分析。
public class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if ( activeNetInfo != null ) { Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } if( mobNetInfo != null ) { Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show(); } } }
< !-- Needed to check when the network connection changes --> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> < receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver" android:label="NetworkConnection"> < intent-filter> < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> < /intent-filter> < /receiver>