android 检查可用网络

长平狐 发布于 2013/07/01 15:31
阅读 93
收藏 0

转载,以备日后查阅


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;
   }



原文链接:http://blog.csdn.net/voiceofnet/article/details/7741697
加载中
返回顶部
顶部