閱讀362 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android 登錄前檢查網絡狀態

https://1002878825-qq-com.iteye.com/blog/1194801


Java代碼  收藏代碼
  1. package com.dx;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.net.ConnectivityManager;  
  7. import android.net.NetworkInfo;  
  8. import android.net.NetworkInfo.State;  
  9. import android.os.Bundle;  
  10. import android.widget.TextView;  
  11.   
  12. public class Main extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17. //        setContentView(R.layout.main);  
  18.         TextView textView = new TextView(this);  
  19.         textView.setText("網絡檢測");  
  20.         setContentView(textView);  
  21.           
  22.         if(checkNetWorkInfo()){  
  23.             goToNetWork();  
  24.         };  
  25.     }  
  26.   
  27.     private boolean goToNetWork() {  
  28.         // TODO Auto-generated method stub  
  29.         ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  30.         NetworkInfo info = connectivityManager.getActiveNetworkInfo();  
  31.         if(info == null || !info.isAvailable()){  
  32.             new AlertDialog.Builder(this).setMessage("沒有可以使用的網絡").setPositiveButton("Ok"null).show();      
  33.             return false;  
  34.         }  
  35.         else{  
  36.             new AlertDialog.Builder(this).setMessage("網絡正常可以使用").setPositiveButton("Ok"null).show();      
  37.             return true;  
  38.         }  
  39.           
  40.           
  41.     }  
  42.   
  43.     private boolean checkNetWorkInfo() {  
  44.         // TODO Auto-generated method stub  
  45.         ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  46.         State wifi  = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
  47.         if(wifi != null){  
  48.             new AlertDialog.Builder(this).setMessage(wifi.toString()).setPositiveButton("wifi"null).show();//顯示wifi網絡連接狀態      
  49.             return true;  
  50.               
  51.         }else{  
  52.             State mobile  = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
  53.             if(mobile != null){  
  54.                 new AlertDialog.Builder(this).setMessage(mobile.toString()).setPositiveButton("3G"null).show();//顯示3G網絡連接狀態      
  55.                 return true;  
  56.             }  
  57.         }  
  58.         return false;  
  59.           
  60.     }  
  61. }  



在手機應用與網絡交互數據的時候,我們首先要判斷有沒有可用的網絡,如果沒有則跳到相應的網絡設置頁麵,方法詳見代碼:

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


最後更新:2017-04-02 17:51:26

  上一篇:go android 檢測網絡是否存在
  下一篇:go android 將圖片內容解析成字節數組,將字節數組轉換為ImageView可調用的Bitmap對象,圖片縮放,把字節數組保存為一個文件,把Bitmap轉Byte