• Android 检测网络连接状态


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

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

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

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

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

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

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



    运行界面:

  • 相关阅读:
    Android 拍照 代码实例
    利用Android手机里的摄像头进行拍照
    看视频时,类加载器没太理解,现在再整理下几个要点
    关于java设计模式与极品飞车游戏的思考
    【Mood-3】心声
    源自梦想 eclipse快捷键整理
    2020重新出发,JAVA语言,JAVA的诞生和发展史
    2020重新出发,序章: 语言的诞生
    2020重新出发,JAVA学前了解,DOS常用命令
    2020重新出发,JAVA学前了解,Windosws常用快捷键
  • 原文地址:https://www.cnblogs.com/tdalcn/p/3494183.html
Copyright © 2020-2023  润新知