2010-05-12 50 views

回答

3
+0

是啊,那就是我,完全忘了那個。謝謝。但是......它接縫這個對話框在許多應用程序中正式使用,是不是這個對話框是內置的? – Pentium10 2010-05-12 10:24:59

+0

我不確定。也許Android會檢查你是否想訪問Internet,但你不能,然後出現這個對話框。 或者因爲這個對話框或多或少看起來都一樣,因爲它們只是定製的內置對話框 – RoflcoptrException 2010-05-12 10:38:51

+2

可能這是HTC Sense的一件事,因爲花了一些時間調查之後,它只出現在他們的應用上。 – Pentium10 2010-05-12 10:40:28

17
/** 
* Checks if we have a valid Internet Connection on the device. 
* @param ctx 
* @return True if device has internet 
* 
* Code from: http://www.androidsnippets.org/snippets/131/ 
*/ 
public static boolean haveInternet(Context ctx) { 

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx 
      .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 

    if (info == null || !info.isConnected()) { 
     return false; 
    } 
    if (info.isRoaming()) { 
     // here is the roaming option you can change it if you want to 
     // disable internet while roaming, just return false 
     return true; 
    } 
    return true; 
} 
/** 
    * Display a dialog that user has no internet connection 
    * @param ctx1 
    * 
    * Code from: http://osdir.com/ml/Android-Developers/2009-11/msg05044.html 
    */ 
    public static void showNoConnectionDialog(Context ctx1) { 
     final Context ctx = ctx1; 
     AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
     builder.setCancelable(true); 
     builder.setMessage(R.string.no_connection); 
     builder.setTitle(R.string.no_connection_title); 
     builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); 
      } 
     }); 
     builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       return; 
      } 
     }); 
     builder.setOnCancelListener(new DialogInterface.OnCancelListener() { 
      public void onCancel(DialogInterface dialog) { 
       return; 
      } 
     }); 

     builder.show(); 
    } 
相關問題