I want to check if the internet is connected and show a dialog if it is not. This is my code:
network_class.java
public class network_class {
public static boolean isConnectedToInternet(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
return false;
}
}
network_detector.java
public class network_detector extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
View alertdialog_1 = LayoutInflater.from(context).inflate(R.layout.network_dialog,null);
AlertDialog.Builder dialog_1 = new AlertDialog.Builder(context);
dialog_1.setView(alertdialog_1);
AlertDialog dialog = dialog_1.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(false);
if(network_class.isConnectedToInternet(context)){
if(dialog.isShowing())
dialog.cancel();
}
else
dialog.show();
}
}
After that, I am using the receiver in my MainActivity:
network_detector detector = new network_detector();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//some irrelevant code here
}
@Override
protected void onStart() {
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(detector,intentFilter);
super.onStart();
}
@Override
protected void onStop() {
unregisterReceiver(detector);
super.onStop();
}
While the application is running in my device, when I switch off the internet the dialog shows (which is correct), and then again when I switch on the internet, strangely enough the dialog still keeps on showing. Why is that? Where am I going wrong?
EDIT: These permissions are there in my manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
>Solution :
It doesn’t dismiss because you are creating a different AlertDialog every time you get on the onReceive so you should store a variable in the NetworkDetector class.
public class network_detector extends BroadcastReceiver {
private AlertDialog dialog;
@Override
public void onReceive(Context context, Intent intent) {
if (dialog != null) {
boolean isConnected = networkClass.isConnectedToInternet(context);
if (isConnected) {
dialog.dismiss();
dialog = null;
} else if (!dialog.isShowing()) {
showNoInternetDialog(context);
}
} else {
showNoInternetDialog(context);
}
}
private void showNoInternetDialog(Context context) {
View alertdialogView = LayoutInflater.from(context).inflate(R.layout.network_dialog, null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setView(alertdialogView);
dialog = dialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(false);
dialog.show();
}
}