zoukankan      html  css  js  c++  java
  • android 检查可用网络

       在手机应用与网络交互数据的时候,我们首先要判断有没有可用的网络,如果没有则跳到相应的网络设置页面,方法详见代码:

    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("请开启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;
       }

  • 相关阅读:
    Java类对象转json字符串,servlet或js的json字符串转json对象或数组
    大三下每周总结--第一周
    阅读架构漫谈九篇博客有感-1500字
    大三上寒假15天--第15天
    大三上寒假15天--第14天
    大三上寒假15天--第13天
    jenkins+appium android app自动化测试
    windows jenkins 卸载
    jenkins运行Python
    pytest+jenkins安装+allure导出报告
  • 原文地址:https://www.cnblogs.com/stulife/p/1847581.html
Copyright © 2011-2022 走看看