I have an application where internet connectivity is must. So, what I want, is that whenever user gets disconnected from Internet (Wifi or Mobile), the Activity
should be able to detect the network state and should display an AlertDialog
prompting user to check its connection. I have tried implementing this using broadcast receiver, but my application is getting crashed. Following is the code snippet for the same:
Following code is in onCreate()
Method
c = this.getApplicationContext();
broadcastReceiver = new BroadcastReceiver() {
AlertDialog.Builder builder;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo activeNetWifi = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isConnectedMobile = activeNetInfo != null
&& activeNetInfo.isConnectedOrConnecting();
boolean isConnectedWifi = activeNetWifi != null
&& activeNetWifi.isConnectedOrConnecting();
AlertDialog alert = alertNoNetwork();
if (isConnectedMobile || isConnectedWifi) {
if (alert != null && alert.isShowing()) {
alert.dismiss();
}
} else {
if (alert != null && !alert.isShowing()) {
alert.show();
}
}
}
public AlertDialog alertNoNetwork() {
builder = new AlertDialog.Builder(c);
builder.setMessage(R.string.err_network_failure_title)
.setMessage(R.string.err_network_failure_message)
.setCancelable(false)
.setPositiveButton("Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
};
Declaring c
and broadcastReceiver
as:
private Context c;
private BroadcastReceiver broadcastReceiver ;
and registering broadcastReceiver
in onResume()
method:
registerReceiver(broadcastReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
My error logs are:
02-25 15:53:35.836: E/AndroidRuntime(24768): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.example.ActivityCapturImg$1@1bca6ea7 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:880) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.os.Handler.handleCallback(Handler.java:739) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.os.Handler.dispatchMessage(Handler.java:95) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.os.Looper.loop(Looper.java:135) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.app.ActivityThread.main(ActivityThread.java:5312) 02-25 15:53:35.836: E/AndroidRuntime(24768): at java.lang.reflect.Method.invoke(Native Method) 02-25 15:53:35.836: E/AndroidRuntime(24768): at java.lang.reflect.Method.invoke(Method.java:372) 02-25 15:53:35.836: E/AndroidRuntime(24768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 02-25 15:53:35.836: E/AndroidRuntime(24768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 02-25 15:53:35.836: E/AndroidRuntime(24768): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.view.ViewRootImpl.setView(ViewRootImpl.java:583) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.app.Dialog.show(Dialog.java:298) 02-25 15:53:35.836: E/AndroidRuntime(24768): at com.swachhmap.ActivityCapturImg$1.onReceive(ActivityCapturImg.java:235) 02-25 15:53:35.836: E/AndroidRuntime(24768): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:870) 02-25 15:53:35.836: E/AndroidRuntime(24768): ... 8 more
Kindly help.
Thanks, Arpit
EDIT: Context c
that is passed in AlertDialog.Builder
should have been
c = ActivityName.this;
instead of getApplicationContext()