android检查网络连接状态的变化,无网络时跳转到设置界面
在AndroidManifest.xml中加一个声明
<receiver android:name="NetCheckReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
NetCheckReceive.java文件如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetCheckReceiver extends BroadcastReceiver{
//android 中网络变化时所发的Intent的名字
private static final String netACTION="android.net.conn.CONNECTIVITY_CHANGE";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(netACTION)){
//Intent中ConnectivityManager.EXTRA_NO_CONNECTIVITY这个关键字表示着当前是否连接上了网络
//true 代表网络断开 false 代表网络没有断开
if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)){
aActivity.stopFlag = true;
}else{
aActivity.stopFlag = false;
}
}
}
}
aActivity.stopFlag 是一个static布尔值 可以出现在程序需要判断网络情况的地方。
更多相关文章
- Github 上Top1的Android 异步网络请求框架
- Android之父安迪·鲁宾:极客文化的代表
- 开启Android程序时检测网络与GPS是否打开
- Android Dialog的四种情况
- Android 设备监听网络状态变化
- 使用 Android快速开发框架 Afinal 0.3 快速开发网络应用相关APK
- 浅入浅出Android(015):使用ImageView显示网络图片