Android中网络情况时有变化,比如从有网到没网,从wifi到gprs,gprs又从cmwap到cmnet...等等!
如果你的程序有些功能是需要网络支持的,有时候就需要监听到网络的变化情况进行相应的处理。
比如说下载一个文件,如果突然断网了,怎么处理?网络又恢复了,如何监听到并重连?
当网络变化的时候系统会发出义个广播broadcast,只要在程序中注册一个广播接收器BroadcastReceiver,并在IntentFilter中添加相应的过滤,这样一旦网络有变化,程序就能监听到
public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
private void registerDateTransReceiver() {
Log.i(TAG, "register receiver " + CONNECTIVITY_CHANGE_ACTION);
IntentFilter filter = new IntentFilter();
filter.addAction(CONNECTIVITY_CHANGE_ACTION);
filter.setPriority(1000);
registerReceiver(new MyReceiver(), filter);
}
在MyReceiver中:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "PfDataTransReceiver receive action " + action);
if(TextUtils.equals(action, CONNECTIVITY_CHANGE_ACTION)){//网络变化的时候会发送通知
Log.i(TAG, "网络变化了");
return;
}
}
当网络变化时,从有网到没网也会发广播,就举的例子来说,如果下载时断网了,接收到广播的时候要判断当前网络是可用还是不可用状态,如果可用进行什么操作;不可用进行什么操作:
public static NetworkInfo getActiveNetwork(Context context){
if (context == null)
return null;
ConnectivityManager mConnMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnMgr == null)
return null;
NetworkInfo aActiveInfo = mConnMgr.getActiveNetworkInfo(); // 获取活动网络连接信息
return aActiveInfo;
}
如果你的程序有些功能是需要网络支持的,有时候就需要监听到网络的变化情况进行相应的处理。
比如说下载一个文件,如果突然断网了,怎么处理?网络又恢复了,如何监听到并重连?
当网络变化的时候系统会发出义个广播broadcast,只要在程序中注册一个广播接收器BroadcastReceiver,并在IntentFilter中添加相应的过滤,这样一旦网络有变化,程序就能监听到
public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
private void registerDateTransReceiver() {
Log.i(TAG, "register receiver " + CONNECTIVITY_CHANGE_ACTION);
IntentFilter filter = new IntentFilter();
filter.addAction(CONNECTIVITY_CHANGE_ACTION);
filter.setPriority(1000);
registerReceiver(new MyReceiver(), filter);
}
在MyReceiver中:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "PfDataTransReceiver receive action " + action);
if(TextUtils.equals(action, CONNECTIVITY_CHANGE_ACTION)){//网络变化的时候会发送通知
Log.i(TAG, "网络变化了");
return;
}
}
当网络变化时,从有网到没网也会发广播,就举的例子来说,如果下载时断网了,接收到广播的时候要判断当前网络是可用还是不可用状态,如果可用进行什么操作;不可用进行什么操作:
public static NetworkInfo getActiveNetwork(Context context){
if (context == null)
return null;
ConnectivityManager mConnMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mConnMgr == null)
return null;
NetworkInfo aActiveInfo = mConnMgr.getActiveNetworkInfo(); // 获取活动网络连接信息
return aActiveInfo;
}
这个方法返回的aActiveInfo可以判断网络的有无,如果返回的是null,这时候是断网了,如果返回对象不为空,则是连上了网。在返回的NetworkInfo对象里,可以有对象的方法获取更多的当前网络信息,比如是wifi还是cmwap等,就不多说了。
==============================================================================
BroadcastReceiver
- public class MyReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- //Toast.makeText(context, intent.getAction(), 1).show();
- ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- NetworkInfo activeInfo = manager.getActiveNetworkInfo();
- Toast.makeText(context, "mobile:"+mobileInfo.isConnected()+"\n"+"wifi:"+wifiInfo.isConnected()
- +"\n"+"active:"+activeInfo.getTypeName(), 1).show();
- } //如果无网络连接activeInfo为null
- }
在清单文件中注册 BroadcastReceiver
- <receiver android:name=".MyReceiver">
- <intent-filter>
- <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
- </intent-filter>
- </receiver>
要读取网络状态记得要加权限
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
===================================================================================================================================
网络状态发生变化的时候,系统会发出 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>
分类:
Android,
Technology