在开发基于网络的应用的时候,需要判断是否有可用的网络,如果没有开启WIFI或者APN的话,是无法连接网络的。以下代码是判断是否有网络,以及没有网络的话,打开系统设置进行开启。

private boolean CheckNetwork() {
boolean flag = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null)
flag = cwjManager.getActiveNetworkInfo().isAvailable();
if (!flag) {
Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(getResources().getString("请开启GPRS或WIFI网络连接"));
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
startActivity(mIntent);
}
}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();
b.show();
}

return flag;
}

主要是通过Intent来调用开启网络的Activity。

更多相关文章

  1. Android(安卓)教你一步步搭建MVP+Retrofit+RxJava网络请求框架
  2. android网络与通信(三种网络接口简述 )
  3. adb code
  4. Android:使用Socket网络通信时异常:java.net.SocketException: soc
  5. Activity加载模式
  6. Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)
  7. Android(安卓)基于NetworkCallback的网络状态监听框架
  8. Android中从网络获取数据的方法
  9. 谈谈Android的三种网络通信方式

随机推荐

  1. python单向循环链表实例详解
  2. C++超详细讲解树与二叉树
  3. C++算法学习之分支限界法的应用
  4. VSCode配置PHP函数跳转
  5. TextView添加Onclick点击无效没反应解决
  6. android 长按菜单的使用
  7. 『ANDROID』反射取子类、父类 属性
  8. Android(安卓)获取当前设备的IP地址
  9. Android(安卓)获取当前设备的IP地址
  10. Android(安卓)Layout inflate分析(2) - V