我有办法每 15 秒检查一次互联网连接。如果没有互联网连接,该方法将调用dialogfragment。到目前为止,它运行得很好。建立互联网连接后,我无法关闭dialogFragment。Logcat 没有给出错误。“dialogfragment dissmis”不起作用,dialogfragment 仍保留在屏幕上。
首先,我断开与互联网的连接并调用dialogFragment。随后互联网连接正在运行并且“设备已连接到互联网”。我看到文字了。但对话框片段并没有消失。
检查InternetAsyncTask.class
public class CheckInternetAsyncTask extends AsyncTask<Void, Integer, Boolean> {
Activity activity;
private Context context;
public CheckInternetAsyncTask(Activity activity) {
this.context = activity.getApplicationContext();
this.activity = activity;
}
@Override
protected Boolean doInBackground(Void... params) {
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
assert cm != null;
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnected();
if (isConnected) {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
//You can replace it with your name
return !ipAddr.equals("");
} catch (Exception e) {
Log.e("TAG", "Error checking internet connection"+ e.getMessage());
return false;
}
} else {
//Log.d("TAG", "No network available!");
return false;
}
}
慕尼黑5688855
沧海一幻觉
相关分类