Leon Leung
Leon Leung

Reputation: 43

How to use alertDialog in background service

I need to prompt a alertDialog in a background service, but it crashed by the bad context, how can I handle it, where I can get the right Context?

Upvotes: 2

Views: 4906

Answers (3)

SaadurRehman
SaadurRehman

Reputation: 660

The accepted answer would not work in Oreo+ Please use the flag TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE or SYSTEM_ALERT in WindowManager.LayoutParams: use

WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

instead

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

still the overlay dont show? then check if your app has the permission

Settings.canDrawOverlays() if this is false then.

User should have this permission too! This was introduced in Marshmallow

// Check if Android M or higher

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

// Show alert dialog to the user saying a separate permission is 
 needed
    // Launch the settings activity if the user prefers
    Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    startActivity(myIntent);
}

Upvotes: 3

Hiren Patel
Hiren Patel

Reputation: 52800

Code inside Service class to open AlertDialog

private void showAlertDialog() {

        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
            kl.disableKeyguard();

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK| PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
            wakeLock.acquire();


        final CharSequence[] items = { getString(R.string.test1), getString(R.string.tes2), getString(R.string.test3), getString(R.string.cancel) };
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
        builder.setTitle("Title");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals(getString(R.string.test1))) {
                    dialog.dismiss();

                } else if (items[item].equals(getString(R.string.test2))) {
                    dialog.dismiss();

                } else if (items[item].equals(getString(R.string.test3))) {

                }else if (items[item].equals(getString(R.string.cancel))) {
                    dialog.dismiss();


                }
            }
        });

        alert = builder.create();
        alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);


        alert.show();

        alert.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface arg0) {

            }
        });


        alert.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {

            }
        });


    }

You have to add permissions in Manifest file

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

You can do customize on alert dialog as your requirements.

Done

Upvotes: 3

user4989495
user4989495

Reputation: 1

1、you must set:mDialog.getWindow().setType( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 2、 set permission:

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

Upvotes: 0

Related Questions