判断网络状态 | 跳转至设置Activity

来源:互联网 发布:五洲传播中心 知乎 编辑:程序博客网 时间:2024/06/05 09:25

判断网络状态 | 跳转至设置Activity 收藏

 

view plaincopy to clipboardprint?
  1. /* 
  2.      * 判断网络状态  
  3.      */  
  4.     private boolean JudgeNetWorkStatus() {  
  5.         boolean netStatus = false;  
  6.         ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  7.         conManager.getActiveNetworkInfo();  
  8.         if (conManager.getActiveNetworkInfo() != null) {  
  9.             netStatus = conManager.getActiveNetworkInfo().isAvailable();  
  10.         }  
  11.         if (!netStatus) {  
  12.             Builder b = new AlertDialog.Builder(this).  
  13.             setTitle(R.string.no_Network_found).  
  14.             setMessage(R.string.whether_set_network);  
  15.             b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
  16.                 public void onClick(DialogInterface dialog, int whichButton) {  
  17.                     Intent mIntent = new Intent("/");  
  18.                     ComponentName comp = new ComponentName("com.android.settings",  
  19.                             "com.android.settings.WirelessSettings");  
  20.                     mIntent.setComponent(comp);  
  21.                     mIntent.setAction("android.intent.action.VIEW");  
  22.                     startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写  
  23.                 }  
  24.             }).setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {  
  25.                 public void onClick(DialogInterface dialog, int whichButton) {  
  26.                     dialog.cancel();  
  27.                 }  
  28.             }).show();  
  29.         }  
  30.         return netStatus;  
  31.     }