I want to show an Alert Dialog via AlertDialogManager
class to a non-activity
class DeviceAdminReceiverSample
's method onDisabled
, but whenever I call alertDialog
via that method it generates error with following text
Error
06-12 12:01:19.923: E/AndroidRuntime(468): FATAL EXCEPTION: main
06-12 12:01:19.923: E/AndroidRuntime(468): java.lang.RuntimeException: Unable to start
receiver com.android.remotewipedata.DeviceAdminReceiverSample:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not
for an application
I know the issue is with context
thing but I don't know what to put there so that it work, I tried this
, getApplicationContext()
but all vain. My code for both classes is below
AlertDialogManager
public class AlertDialogManager {
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
if (status != null)
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
}
DeviceAdminReceiverSample
public class DeviceAdminReceiverSample extends DeviceAdminReceiver {
static final String TAG = "DeviceAdminReceiver";
AlertDialogManager alert = new AlertDialogManager();
/** Called when this application is no longer the device administrator. */
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Toast.makeText(context, R.string.device_admin_disabled,
Toast.LENGTH_LONG).show();
// intent.putExtra("dialogMessage", "Device admin has been disabled");
// intent.setClass(context, DialogActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(intent);
alert.showAlertDialog(context, "Alert",
"Device admin has been disabled", true);
}
Just add this before your
alertDialog.show();
and use this permission:
As AJAY suggested, the best way is to work with the 'Activity' in parameter instead of using the 'context'.
In your personal class, just ask for the activity in its constructor as a mandatory parameter => public void constructorOfTheClass(Activity activity){...}.
When you call the constructor in your Activity, just indicate this parameter and you'll be able to work with it inside the class directly.
Then you can use this 'activity' information in your AlertDialog method within your class as SUNIL noticed to be prompted correctly in the desired Activity.
Hope it helps... and be sure it will work ! ;o)
Here's what I made and use:
myDialog.java:
dialog_box_text.xml:
Example code:
If you always want to get the current activity from anywhere in the app you can register an ActivityLifecycleCallback on your Application instance.
Here's an untested implementation that might get you closer.
Then to use this throughout your app you would do some call like this ...
The advantages are you can always keep track of your current activity, however its a little too overkill for just handling Dialogs from within the Activity.
call this method in activity class
Here's a quick method of properly performing this task that has done the job for me. Basically, what you would do is just create a new thread.
Declare a public and static variable with a type that matches the original activity class.
public static Activity1 activity;
Activity1 is the class that the variable resides in.
onCreate();
, set the variable to be equal to the context of the activity, otherwise known as this.Example:
3. Since we now have the context of the activity, we can use it to create a function with an alert dialog by using the
runOnUiThread();
method inside of the function that will call the alert dialog. We would use anew Runnable()
for the runnable action required forrunOnUiThread();
, and to have the alert dialog actually open, we would override the run function of a runnable item and place the code for the alert dialog in there.Example function:
Hope this helps :)