我尝试使用一个button
创建自定义对话框( TRY AGAIN )
这个对话框测试是否有互联网连接。当用户点击( TRY AGAIN )按钮并且如果没有互联网连接时,将再次显示自定义对话框,并且当有互联网连接对话框时将被解除
private void showDialog() {
myDialog = new Dialog(getActivity());
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
assert myDialog.getWindow() != null;
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.setContentView(R.layout.alert_dialog);
text.setText(getResources().getText(R.string.nointernent));
myDialog.setCancelable(false);
Button tyragain = myDialog.findViewById(R.id.tryagainbtn);
tyragain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isNetworkConnected()) {
showDialog();
}
else{
myDialog.dismiss();
}
}
});
myDialog.show();
}
/ ** /
public boolean isNetworkConnected() {
ConnectivityManager connectionManager = (ConnectivityManager) getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectionManager == null) {
return false;
}
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
if (networkInfo == null) {
return false;
}
return networkInfo.isConnected();
}
答案 0 :(得分:0)
最后我通过添加myDialog.isShowing()
if (!isNetworkConnected() && myDialog.isShowing()) {
showDialog();
}
else{
myDialog.dismiss();
refreshContent();
}