Android 检测网络连接状态

来源:互联网 发布:迅雷9网络诊断工具 编辑:程序博客网 时间:2024/05/02 12:54

Android连接网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置。


首先,要判断网络状态,需要有相应的权限,下面为权限代码(AndroidManifest.xml):

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <uses-permission android:name="android.permission.INTERNET"/>

然后,检测网络状态是否可用

[java] view plaincopyprint?
  1. /** 
  2.  * 对网络连接状态进行判断 
  3.  * @return  true, 可用; false, 不可用 
  4.  */  
  5. private boolean isOpenNetwork() {  
  6.     ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  
  7.     if(connManager.getActiveNetworkInfo() != null) {  
  8.         return connManager.getActiveNetworkInfo().isAvailable();  
  9.     }  
  10.   
  11.     return false;  
  12. }  

最后,不可用则打开网络设置

[java] view plaincopyprint?
  1. /** 
  2.  * 访问百度主页,网络不可用则需设置 
  3.  */  
  4. private void initMoreGames() {  
  5.     String URL_MOREGAMES = "http://www.baidu.com";  
  6.     mWebView = (WebView) findViewById(R.id.view_gamesort);  
  7.   
  8.     if (mWebView != null) {  
  9.         mWebView.requestFocus();  
  10.         WebSettings webSettings = mWebView.getSettings();  
  11.         if (webSettings != null) {  
  12.             webSettings.setJavaScriptEnabled(true);  
  13.             webSettings.setCacheMode(MODE_PRIVATE);  
  14.             webSettings.setDefaultTextEncodingName("utf-8");  
  15.         }  
  16.   
  17.         // 判断网络是否可用  
  18.         if(isOpenNetwork() == true) {  
  19.             mWebView.loadUrl(URL_MOREGAMES);  
  20.         } else {  
  21.             AlertDialog.Builder builder = new AlertDialog.Builder(MoreGamesActivity.this);  
  22.             builder.setTitle("没有可用的网络").setMessage("是否对网络进行设置?");  
  23.               
  24.             builder.setPositiveButton("是"new DialogInterface.OnClickListener() {  
  25.                 @Override  
  26.                 public void onClick(DialogInterface dialog, int which) {  
  27.                     Intent intent = null;  
  28.                       
  29.                     try {  
  30.                         String sdkVersion = android.os.Build.VERSION.SDK;  
  31.                         if(Integer.valueOf(sdkVersion) > 10) {  
  32.                             intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);  
  33.                         }else {  
  34.                             intent = new Intent();  
  35.                             ComponentName comp = new ComponentName("com.android.settings""com.android.settings.WirelessSettings");  
  36.                             intent.setComponent(comp);  
  37.                             intent.setAction("android.intent.action.VIEW");  
  38.                         }  
  39.                         MoreGamesActivity.this.startActivity(intent);  
  40.                     } catch (Exception e) {  
  41.                         Log.w(TAG, "open network settings failed, please check...");  
  42.                         e.printStackTrace();  
  43.                     }  
  44.                 }  
  45.             }).setNegativeButton("否"new DialogInterface.OnClickListener() {  
  46.                 @Override  
  47.                 public void onClick(DialogInterface dialog, int which) {  
  48.                     dialog.cancel();          
  49.                     finish();  
  50.                 }  
  51.             }).show();  
  52.         }  
  53.     } else {  
  54.         Log.w(TAG, "mWebView is null, please check...");  
  55.     }  
  56. }  


运行界面:

0 0
原创粉丝点击