Android判断网络状态是否可用的
来源:互联网 发布:淘宝宝贝属性是什么 编辑:程序博客网 时间:2024/04/28 19:38
/**如果要使用网络状态的判断,需要权限....<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>**<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>*<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>*<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*/public class NetWorkUtil { /** * 这个方法是判断网络状态是否可用的 * */ public static boolean isConn(Context context){ boolean bisConnFlag=false; //1.获取网络连接的管理对象 ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); //2.通过管理者对象拿到网络的信息 NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ //3.网络状态是否可用的返回值 bisConnFlag=network.isAvailable(); } return bisConnFlag; } /** * 如果没有网络 弹出dialog对话框,,,是否进入设置网络的页面 * */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.mipmap.ic_launcher) // .setTitle("警告") // .setMessage("当前无网络,是否去设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到系统的网络设置界面 Intent intent = null; // 先判断当前系统版本 if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上 intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); }else{ intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); } context.startActivity(intent); } }).setNegativeButton("取消", null).show(); }}
/**
* 获取网络数据的方法
*/
public static void getData(String path, Context context){ if (NetWorkUtil.isConn(context)){
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); }};asyncTask.execute();
}else { NetWorkUtil.showNoNetWorkDlg(context); }}
阅读全文
0 0
- Android判断网络状态是否可用的
- 判断网络状态是否可用
- 判断网络状态是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android-判断网络连接是否可用及监控网络状态
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android检测网络状态,判断当前网络是否可用
- Android判断网络是否可用的方法
- android 判断网络是否可用
- Android判断网络是否可用
- Android 判断网络是否可用
- 【Android】判断网络是否可用
- Android 判断网络是否可用
- Android 判断网络是否可用
- 第十单元
- 083 定积分总结
- [Flink]Flink1.3 指南二 安装与启动
- Linux_下的jdk环境变量配置
- 多盒子的运动---用目标值减去对象的变量属性(target
- Android判断网络状态是否可用的
- 机器学习中的L0,L1,L2讲解
- Struts 2中<s:action>标签的用法
- TextureView 的使用
- 构造器的深入理解
- ionic单选框redio
- shell脚本简介
- RecyclerView直播聊天界面判断最后一条消息位置
- Lock实现之AbstractQueuedSynchronizer——AQS