Alert dialog from Android service

How do I display dialog from a service?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Another way without using an activity:

AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setMessage("Are you sure?")


Please note that you have to use this permission:

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

Solution 2

android-smspopup does exactly that.

A service receives a sms and it starts an Activity with:


EDIT: The dialog activity is started here with this code

private void notifyMessageReceived(SmsMmsMessage message) {

With getPopupIntent() declared as followed (code here):

public Intent getPopupIntent() {
    Intent popup = new Intent(context, SmsPopupActivity.class);
    return popup;

SmsPopupActivity class obviously defines the dialog activity. Its declared as followed in AndroidManifest.xml:

        android:theme="@style/DialogTheme" >

Solution 3

Material styled Dialog from a Service

From a Service, you can easily show a Material Design styled Dialog manipulating its Window type, attributes and LayoutParams.

Before we begin: AppCompat Library

This guide presumes you are using Android AppCompat libray.

Before we begin: Permissions

This method needs the SYSTEM_ALERT_WINDOW permission.
Usually a Service that wants to display a Dialog also has some Views drawn over the system UI (added using WindowManager.addView() method) so you might have already declared this permission usage inside you manifest. If not, add this line:

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

In Android 6.0 Marshmallow, the user must explicitly allow your app to “draw over other apps”.
You can programmatically start the system settings Activity that contains the switch:

protected void onResume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {

private void openOverlaySettings() {
    final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                     Uri.parse("package:" + getPackageName()));
    try {
        startActivityForResult(intent, RC_OVERLAY);
    } catch (ActivityNotFoundException e) {
        Log.e(TAG, e.getMessage());

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case RC_OVERLAY:
            final boolean overlayEnabled = Settings.canDrawOverlays(this);
            // Do something...

Create your custom Material Design Dialog theme

Inside themes.xml create this theme and customize it with your app colors:

<style name="AppTheme.MaterialDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorPrimary">@color/brand_primary</item>
    <item name="colorPrimaryDark">@color/brand_primary_dark</item>
    <item name="colorAccent">@color/brand_accent</item>

    <item name="android:windowBackground">@drawable/dialog_background_light</item>

    <item name="android:textColorPrimary">@color/primary_text_light</item>
    <item name="android:textColorSecondary">@color/secondary_text_light</item>
    <item name="android:textColorTertiary">@color/secondary_text_light</item>

Launch your Dialog

Inside your Service:

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this,;

        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

final AlertDialog dialog = dialogBuilder.create();
final Window dialogWindow = dialog.getWindow();
final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();

// Set fixed width (280dp) and WRAP_CONTENT height
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, getResources().getDisplayMetrics());
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

// Set to TYPE_SYSTEM_ALERT so that the Service can display it
dialogWindowAttributes.windowAnimations =;;

Solution 4

The documentation suggests that you should make use of notifications. Re-evaluate why you might need to use dialogs. What are you trying to achieve?

Solution 5

firstable you need to cast your Activity in Service
so in your Activity add

public static Activity mactivity;

and in On create add this

mactivity = YourActivity.this;

and when we move to your Service alse declare this :

YourActivity mact;
YourActivity act;

and in on Create service this is our casting

mact = (YourActivity) act.mactivity;

and the alert dialog will be look like this :

AlertDialog.Builder builder = new AlertDialog.Builder(mact);
        builder.setPositiveButton(getResources().getString(R.string.Ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
               your message

remember that mact is the Cast class to use in Alert Builder …that works fine for me hope it help .

Solution 6

I am calling the below, inside the service when I need to show a dialog.

public void ShowYesNoDialog() {

    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes Button Clicked

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Did the dialog display?")
    .setPositiveButton("Yes", dialogClickListener)
    .setNegativeButton("No", dialogClickListener);
    AlertDialog alertDialog = builder.create();

Make sure you have the below permission added to the manifest.

I also think that for SDK 23 and above, the user should explicitly set the “Draw over other Apps” permission from the Application manager for the App that starts this service.

Solution 7

Here is a more detailed explanation how to display AlertDialog from the service using translucent Activity, and how to avoid some issues.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply