Android检查当前网络状态是否可用并
来源:互联网 发布:加强网络舆情管理通知 编辑:程序博客网 时间:2024/06/17 20:35
当我们进入App的时候,首先会判断当前的网络状态是否可用,如果不可用就弹出一个Dialog让用户进行网络的设置,点击设置的时候跳转到系统的网络设置界面。
运行效果:
网络状态的检查(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(); }}
0 0
- Android检查当前网络状态是否可用并
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- [Android] 检查网络是否可用
- [Android]检查网络是否可用
- Android 检查网络是否可用
- iOS 检查网络状态是否可用
- 检查Android网络连接是否可用
- 检查网络是否可用(Android)
- 检查网络是否可用
- 检查网络是否可用
- 安卓学习笔记---Android检测网络状态,判断当前网络是否可用
- 检查当前网络状态
- 算法设计方法:递归的内涵与经典应用
- NBUT
- 解决nginx负载均衡的session共享问题
- 本地包含配合文件上传
- 配置一个Lua工程(支持命令行运行 和 vs断点调试)
- Android检查当前网络状态是否可用并
- Android studio百度地图的使用
- mysql5.7.17报错:ERROR! The server quit without updating PID file
- The Overview of Rule Placement Problem in SDN(规则放置问题的概述)
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- js,jq,css多方面实现简易Tab切换
- hanoi问题
- raspi-config command not found resolution
- 闭包(Closures)