开发过程中经常会遇到一种需求,在你的Activity中需要获得网络连接的状态,以便确认下一步的流程,之前设计过一个复杂的方法,后来发现每次网络状态发生变化了都会发广播出来,广播这是个好东西,有了广播,我们就可以灵活的去处理了。
当然,首先要加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这样,我们写receiver就可以了。
<
receiver
android:name ="NetworkReceiver" >
< intent-filter >
< action android:name ="android.net.conn.CONNECTIVITY_CHANGE" />
</ intent-filter >
android:name ="NetworkReceiver" >
< intent-filter >
< action android:name ="android.net.conn.CONNECTIVITY_CHANGE" />
</ intent-filter >
</receiver>
code里面写
public
class NetworkReceiver
extends BroadcastReceiver {
protected Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
// 第一种
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobileNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
showToast("Active Network Type", activeNetInfo);
showToast("Mobile Network Type", mobileNetInfo);
// 第二种
NetworkInfo networkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
String extraInfo = intent
.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
boolean isFailOver = intent.getBooleanExtra(
ConnectivityManager.EXTRA_IS_FAILOVER, false);
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo otherNetworkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
//
showToast("networkInfo", networkInfo);
showToast("extraInfo", extraInfo);
showToast("isFailOver", isFailOver);
showToast("noConnectivity", noConnectivity);
showToast("otherNetworkInfo", otherNetworkInfo);
showToast("reason", reason);
}
public void showToast(String info, Object object) {
if (object == null) {
return;
}
Toast.makeText(mContext, info + " : " + object.toString(), 0).show();
}
protected Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
// 第一种
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobileNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
showToast("Active Network Type", activeNetInfo);
showToast("Mobile Network Type", mobileNetInfo);
// 第二种
NetworkInfo networkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
String extraInfo = intent
.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
boolean isFailOver = intent.getBooleanExtra(
ConnectivityManager.EXTRA_IS_FAILOVER, false);
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo otherNetworkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
//
showToast("networkInfo", networkInfo);
showToast("extraInfo", extraInfo);
showToast("isFailOver", isFailOver);
showToast("noConnectivity", noConnectivity);
showToast("otherNetworkInfo", otherNetworkInfo);
showToast("reason", reason);
}
public void showToast(String info, Object object) {
if (object == null) {
return;
}
Toast.makeText(mContext, info + " : " + object.toString(), 0).show();
}
}