How to show dialog even if the screen is locked?

2019-01-23 09:06发布

I have implemented the push notification using GCM and when i receive the notification i want to show in a dialog for which i have created a custom dialog.

Now, i want my dialog to appear even if the device is locked whether a pattern match or PIN.

I have made following tries, but no positive result.

public void onAttachedToWindow() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    }

and also

public void onAttachedToWindow() {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        }

permissions in manifest:

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

and also added the

android:showOnLockScreen="true"

for the activity to which i want to be shown when screen is locked.

Please help.

2条回答
Ridiculous、
2楼-- · 2019-01-23 09:44

You should use the KeyGuardManager to unlock the device automatically and then acquire your Wake Lock.

KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("Your Activity/Service name");

if(isKeyguardUp){
kgl.disableKeyguard();
isKeyguardUp = false;
}

wl.acquire(); //use your wake lock once keyguard is down.

you should add this permission to manifest

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

and this

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
查看更多
贼婆χ
3楼-- · 2019-01-23 09:47

As refer to link Show dialog with touch events over lockscreen in Android 2.3

I do not think you can display dialog when device is locked without binding your application as admin privileged app programatically.

So you have to bind your application with Device administrator. You can download device administrator sample from https://github.com/marakana/DevicePolicyDemo.

After binding your application through Device administrator once you receive the push notification first unlock device by

DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName  demoDeviceAdmin = new ComponentName(context, DemoDeviceAdminReceiver.class);
    devicePolicyManager.setMaximumTimeToLock(demoDeviceAdmin, 0);

then launch your activity in which you can display you dialog in onattach window like this

@Override
public void onAttachedToWindow() 
{
    super.onAttachedToWindow();

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    new AlertDialog.Builder(this).setMessage("Dialog Displaying").setNeutralButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    }).show();
}

Please let me know if it help.

查看更多
登录 后发表回答