How do I get android lockscreen package name

核能气质少年 提交于 2019-12-01 09:26:26

You can detect if the lockscreen is shown with:

((KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE)).inKeyguardRestrictedInputMode();

And you can get the current top activity with:

(ActivityManager) getSystemService(Service.ACTIVITY_SERVICE).getRunningTasks(1).get(0).topActivity;

This will usually be the lockscreen activity, but it is possible that for example the phone app is shown while the lockscreen is active: in this case it will be the phone activity.

You will need the android.permission.GET_TASKS permission for this to work.

Really a Simple solution.

KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);

if( km.inKeyguardRestrictedInputMode()) {
               //it is locked

                  }

Found a native solution. After you build the Alert dialog and before showing it, apply this:

AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        alertDialog.show();

This will show the dialog on top of the lock screen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!