每當應用程序需要互聯網連接和連接失敗,我得到一個消息對話框如何在沒有連接的情況下彈出默認的連接失敗對話框?
Connection failed
This application requires network access. Enable mobile network or Wi-Fi to download data.
和兩個按鈕,設置,取消。
如何檢測沒有互聯網連接?
如何在我的應用程序中彈出相同的對話框?
每當應用程序需要互聯網連接和連接失敗,我得到一個消息對話框如何在沒有連接的情況下彈出默認的連接失敗對話框?
Connection failed
This application requires network access. Enable mobile network or Wi-Fi to download data.
和兩個按鈕,設置,取消。
如何檢測沒有互聯網連接?
如何在我的應用程序中彈出相同的對話框?
在這裏你可以找到關於如何建立對話
http://developer.android.com/guide/topics/ui/dialogs.html
這裏引用的代碼片段,應該幫助您檢測網絡連接:
http://www.androidsnippets.org/snippets/131/
BTW:WASN」你是否已經在2009年11月15日提出這個問題? http://groups.google.com/group/android-beginners/browse_thread/thread/715cedfc5fd6f020?utoken=QyP-dzQAAACEe9Lph6eUOAakqA3-BnR-KvPfK0ltyCETgVsCM3D7GeEWTAM9S5G8WBs1q2tBppm1FwwMIvCGKnxkm-CrwSdp
/**
* 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();
}
是啊,那就是我,完全忘了那個。謝謝。但是......它接縫這個對話框在許多應用程序中正式使用,是不是這個對話框是內置的? – Pentium10 2010-05-12 10:24:59
我不確定。也許Android會檢查你是否想訪問Internet,但你不能,然後出現這個對話框。 或者因爲這個對話框或多或少看起來都一樣,因爲它們只是定製的內置對話框 – RoflcoptrException 2010-05-12 10:38:51
可能這是HTC Sense的一件事,因爲花了一些時間調查之後,它只出現在他們的應用上。 – Pentium10 2010-05-12 10:40:28