Usman Riaz
Usman Riaz

Reputation: 3020

How To wake screen in android

Hi i am making an Alarm Application. When Alarm Time Comes i Am showing user a Dialog. But the problem is i want to acquire the wake lock when dialog appears. just like when an sms received the screen just wakes.

i have try this one but is not working

public class Alarm extends Activity{
    PowerManager pm;
    WakeLock wl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pm = (PowerManager) getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FlashActivity");
wl.acquire()
        showAlarmDialog();
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        wl.release();
    }
}

I have added the wakelock permission too. Help Would be appriciated :-)

Upvotes: 7

Views: 8042

Answers (3)

Lahiru Chandima
Lahiru Chandima

Reputation: 24068

You can add few flags to your activity to unlock and wake screen when your activity is started.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

Upvotes: 1

Usman Riaz
Usman Riaz

Reputation: 3020

I was able to Turn the screen on this way:

wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();

Hope This help. It Worked For me Though :-) Cheers

Upvotes: 4

Abhi
Abhi

Reputation: 9005

You can acquire wake lock by two methods

wl.acquire(); or wl.acquire(timeout)

Try some thing like this in onResume():

PowerManager pm;
WakeLock wl;

pm = (PowerManager) getSystemService(POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "FlashActivity");
wl.acquire(); Or wl.acquire(timeout)

And you are realeasing in onPause(). That is good.

Upvotes: 3

Related Questions