简体   繁体   中英

Start Activity from LockScreen

I have created an app that uses a Broadcast Receiver. The Receiver needs to open another activity via intent.

The program works when phone in unlocked / not sleeping.

But when the screen of device is locked, the activity is not displayed.

I want to run the activity every time a SMS is received even when phone is in sleep or password locked.

use below code above setContentView(R.layout.main);

 final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                  | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                  | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

now your activity will display even when your device is locked

I guess your activity is paused when the phone sleeps, so you don't receive the Broadcast.

You could try to launch a Service. It will be able to run even in sleep mode (like your media player is able to play music even when the phone is locked)

Put your receiver in a Serive an start your activity from there. It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM