如何解锁btn按下并在解锁屏幕出现之前启动活动?

Moh*_*ada 9 android broadcastreceiver screen-lock

我想做如下:

用户按下硬解锁按钮.按下解锁按钮后,我的活动开始了.关闭我的活动后,使用屏幕上的关闭按钮用户提示进入模式锁定(或锁定).进入右图案锁定主屏幕后出现.

我想要下面的场景:

按电源/解锁按钮 - >启动我的活动 - >点击活动关闭按钮 - >提示进入解锁模式 - >进入模式 - >显示主屏幕

目前完成如下:

使用广播接收器ACTION_USER_PRESENT我在用户进入模式和设备解锁后获得了活动

使用广播接收器ACTION_SCREEN_ON,按下解锁按钮后,我在日志上获得了消息,但是在用户输入模式和设备解锁后,活动开始.

我尝试使用广播接收器接收ACTION_SCREEN_ON和的事件ACTION_USER_PRESENT.

我的代码如下:

广播接收器 -

public class BrodcastReceiver extends BroadcastReceiver 
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("receiver", "main");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
        {
            // do whatever you need to do here
            Log.d("receiver", "screen off");
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
        {
            // and do whatever you need to do here
            Log.d("receiver", "screen on");
            context.startActivity(new Intent(context,
                    unlock_image.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    .setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            Log.d("receiver", "aft activity");
        }
        else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) 
        {
            Log.d("receiver", "unlock");

            context.startActivity(new Intent(context,
                    unlock_image.class)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }     
    }
Run Code Online (Sandbox Code Playgroud)

注册广播列表 -

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        BroadcastReceiver mReceiver = new BrodcastReceiver();
        registerReceiver(mReceiver, filter);  
Run Code Online (Sandbox Code Playgroud)

我尝试了很多,但我无法得到我想要的东西.如果任何人有想法,如何获得我想要的预期结果,任何帮助表示赞赏.

Oas*_*eng 2

尝试添加WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED要启动的活动,只要您的活动位于所有其他窗口之上,这就会暂时禁用键盘保护/键盘锁。