Skip to content Skip to sidebar Skip to footer

Alertdialog.show Silently Ignored Within A Service

I have a service running a background thread. What I'd like to do is to show an AlertDialog initiated from my background thread. I know that this is not the recommended way of no

Solution 1:

It is possible to open a dialog from a background thread. The trick is to start an activity which looks like a dialog:

<activity android:label="@string/app_name" android:name="YourDialog" android:theme="@android:style/Theme.Dialog"/>

Then, to start your activity:

Intent dialog = newIntent(this, YourDialog.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialog);

Note that this is asyncrhonous and doesn't block. If you want to process the result, you'll have to use startService() and pass a custom activity to indicate a result.

Emmanuel

Solution 2:

Are there any limitations for showing an AlertDialog initiated from a service background thread?

The limitation is: it's not possible, AFAIK. You have to show dialogs from the main application thread, not just some arbitrary thread on which you have a Handler.

Some people seem to recommend a Dialog themed Activity to get a similar behavior.

That would seem to be the most likely solution, AFAICT.

Solution 3:

Another trick is to stay with the AlertDialog but with an additional window type TYPE_SYSTEM_ALERT:

AlertDialogalertDialog=newAlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

But don't forget to add this permission:

<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW" />

Post a Comment for "Alertdialog.show Silently Ignored Within A Service"