我正在构建一个 android 应用程序,如果没有活动的互联网连接,它会检查设备上的活动互联网连接,会弹出一个警告对话框,要求重试并退出。
我希望如果用户点击重试按钮,应用程序将再次检查活动的互联网连接等等。
我在循环此代码时遇到问题。这是我的代码
if(cm.getActiveNetworkInfo()==null){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SplashActivity.this);
alertBuilder.setMessage("No internet connection").setCancelable(false);
alertBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
// Want to run this code again
}
})
.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
// negetive button action goes here
}
});
AlertDialog alertDialog = alertBuilder.create();
alertDialog.setTitle("Rates and Reviews");
alertDialog.show();
}
像这样?
private void checkInternet(){
if(cm.getActiveNetworkInfo()==null){
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SplashActivity.this);
alertBuilder.setMessage("No internet connection").setCancelable(false);
alertBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
// Want to run this code again
checkInternet();
}
})
.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
// negetive button action goes here
}
});
AlertDialog alertDialog = alertBuilder.create();
alertDialog.setTitle("Rates and Reviews");
alertDialog.show();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句