Android判斷當前網絡是否可用–示例代碼 – Android移動開發技術文章_手機開發 Android移動開發教學課程

在Android平臺上開發基於網絡的應用,必然需要去判斷當前的網絡連接情況。下面的代碼,作為例子,詳細說明瞭對於當前網絡情況的判斷。


 


先看一個自己定義的應用類。


 


view plaincopy to clipboardprint?
public class NetworkDetector {  
   
    public static boolean detect(Activity act) {  
        
       ConnectivityManager manager = (ConnectivityManager) act  
              .getApplicationContext().getSystemService(  
                     Context.CONNECTIVITY_SERVICE);  
        
       if (manager == null) {  
           return false;  
       }  
        
       NetworkInfo networkinfo = manager.getActiveNetworkInfo();  
        
       if (networkinfo == null || !networkinfo.isAvailable()) {  
           return false;  
       }  
   
       return true;  
    }  

public class NetworkDetector {
 
    public static boolean detect(Activity act) {
     
       ConnectivityManager manager = (ConnectivityManager) act
              .getApplicationContext().getSystemService(
                     Context.CONNECTIVITY_SERVICE);
     
       if (manager == null) {
           return false;
       }
     
       NetworkInfo networkinfo = manager.getActiveNetworkInfo();
     
       if (networkinfo == null || !networkinfo.isAvailable()) {
           return false;
       }
 
       return true;
    }
}


 


這個類隻有一個靜態方法,用來檢測當前系統的網絡是否可用。如果可用,返回true。


還要進一步說明的是,NetworkInfo類中有一個方法getType(),這個方法可以用來判斷當前可用的網絡是wifi,還是mobile等等。


 


再來看使用的例子。


 


view plaincopy to clipboardprint?
boolean networkState = NetworkDetector.detect(XXXActivity.this);  
if (!networkState) {  
    DialogUtil.openMsgDialog(XXXActivity.this,  
           android.R.drawable.ic_dialog_info,  
           “網絡不可用,是否現在設置網絡?”, android.R.string.ok,  
           android.R.string.cancel,  
           new DialogInterface.OnClickListener() {  
              @Override 
              public void onClick(DialogInterface dialog, int which) {  
                  startActivityForResult(new Intent(  
                         ACTION_WIRELESS_SETTINGS), 0);  
              }  
           }, new DialogInterface.OnClickListener() {  
              @Override 
              public void onClick(DialogInterface dialog, int which) {  
                  dialog.cancel();  
              }  
           }).show();  

       boolean networkState = NetworkDetector.detect(XXXActivity.this);
       if (!networkState) {
           DialogUtil.openMsgDialog(XXXActivity.this,
                  android.R.drawable.ic_dialog_info,
                  “網絡不可用,是否現在設置網絡?”, android.R.string.ok,
                  android.R.string.cancel,
                  new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         startActivityForResult(new Intent(
                                ACTION_WIRELESS_SETTINGS), 0);
                     }
                  }, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.cancel();
                     }
                  }).show();
       }


 


其中XXXActivity是我自己

You May Also Like