I encountered a problem,

  • MyActivity is under the LockScreen, in this case, I need to launch MyActivity from framework by clicking volume button;
  • MyActivity is on the top of Lockscreen, in this case, I do nothing.

But I don't know how to distinguish this two situations in framework. I know how to get the top activity, however, LockScreen isn't a activity.

I have a temporary solution, if MyActivity has LayoutParams.FLAG_SHOW_WHEN_LOCKED property, set a custom system property, so I know whether launch MyActivity by this custom system property, but I don't think this is good.

I want to know whether there is a better solution.

1

There are 1 best solutions below

1
Vivek Pratap Singh On

Register BroadcastReceiver "android.intent.action.USER_PRESENT" in CameraActivity and detect whenever screen unlock event happens. Than Based on your requirement you can manage your code.

USER_PRESENT Receiver

in your MainActivity inside oncreate you can check whether screen is locked or not

KeyguardManager keyguardManager = (KeyguardManager) 
context.getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager .inKeyguardRestrictedInputMode()) {
 //it is locked
} else {
 //it is not locked
}