How to use alertDialog in background service

4.7k views Asked by At

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?

3

There are 3 answers

0
Hiren Patel On BEST ANSWER

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

0
user4989495 On

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

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

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);
}