Alert dialog from Android service

How do I display dialog from a service?

  • How to tell if Proguard has done its job
  • New warning when building android app with gradle
  • programmatic click in Android WebView
  • ParseInstaller.getCurrentInstallation().saveInBackground() is freezing my application
  • TextInputLayout hint overlap issue
  • NotificationCompat 4.1 SetSmallIcon and SetLargeIcon
  • Related posts:

    Android Studio compile error : enum constant INSTANT_RUN_REPLACEMENT does not exist in class
    Android sdk cut/trim video file
    Java annotation execute a method within the annotation declaration(usage for android)
    How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?
    Programmatically pairing with a BLE device on Android 4.4+
    Why use ContentProvider.getType() to get MIME type?
  • Android - Display a video thumbnail from a URL
  • how to get object from listview in setOnItemClickListener in android?
  • FusedLocationApi.getLastLocation always null
  • Pinch to zoom with Osmdroid
  • Prevent clickable div from being highlighted in Android WebView
  • Change Android Actionbar subtitle colour
  • 7 Solutions collect form web for “Alert dialog from Android service”

    android-smspopup does exactly that.

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


    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" />

    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 =;;

    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?

    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 = (youActivity) 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 .

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.