【发布时间】:2016-03-29 17:26:21
【问题描述】:
这不是一个重复的问题,我现在如何检查是否有互联网连接,我不知道的是如何在 while 循环中弹出对话框并重试直到互联网连接恢复
如果没有互联网连接,我正在尝试弹出警报对话框, 然后等待用户点击“再试一次” 当他点击按钮时,检查互联网连接,如果没有互联网连接,则再次弹出此警报对话框。
当我使用 if 语句执行此操作时效果很好 - 在没有互联网时弹出对话框并在点击“重试”时检查连接 但是,当我尝试将其放入 while 循环时,循环不会等待/向用户显示对话框。
这样做的正确方法是什么?为什么它现在不起作用?
while (netInfo == null || !netInfo.isConnected()) {
new AlertDialog.Builder(this)
.setTitle("title")
.setMessage("message")
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo = cm.getActiveNetworkInfo();
System.out.println("cm: "+cm+ " netinfo: "+ netInfo);
}
})
.show();
}
【问题讨论】:
-
使用
BroadcastReceiver。
标签: android android-alertdialog internet-connection