0

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.

Kartik Sharma
  • 723
  • 8
  • 19

2 Answers2

1

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

Dinesh Raj
  • 664
  • 12
  • 30
  • Well I already had that. Problem was that under onPause(), I added finish(). I wanted to close app even when home key was pressed. – Kartik Sharma Dec 12 '13 at 15:25
0

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

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52