Android检查当前网络状态是否可用并
2017-05-13 10:55
639 查看
当我们进入App的时候,首先会判断当前的网络状态是否可用,如果不可用就弹出一个Dialog让用户进行网络的设置,点击设置的时候跳转到系统的网络设置界面。
运行效果:
网络状态的检查(WangKeUtils.java):
需要注意的是,在检查当前设备的网络状态是否可用的时候需要加上权限:
设置界面的跳转:
完整代码:
运行效果:
网络状态的检查(WangKeUtils.java):
/** * 检查网络是否可用 * @param context * @return true,false */ public static boolean isNetWorkAvailable(Context context){ ConnectivityManager connectManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectManager.getActiveNetworkInfo(); return (info!=null && info.isAvailable()); }
需要注意的是,在检查当前设备的网络状态是否可用的时候需要加上权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
设置界面的跳转:
Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(intent);
完整代码:
public class T44Fragment extends Fragment { private AlertDialog alertDialog; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_t44, container, false); if(!WangkeUtils.isNetWorkAvailable(getActivity())){ showSetNetWorkDialog(getActivity()); } return view; } private void showSetNetWorkDialog(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("网络信息提示"); builder.setMessage("当前网络不可用,请先进行设置"); builder.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(intent); } }); builder.setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); alertDialog = builder.create(); alertDialog.show(); } }
相关文章推荐
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- 安卓学习笔记---Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android判断当前网络是否可用
- [Android开发常见问题-6] 如何检测手机当前网络是否可用?
- android检测当前网络是否可用
- Android 判断当前网络连接是否可用、打开设置网络界面
- Android判断当前网络是否可用--示例代码
- Android中判断网络连接是否可用及监控网络状态
- Android之检查网络是否可用(跳转网络设置页面)
- Android判断当前网络是否可用
- Android 检测当前网络是否可用