MercurieVV
MercurieVV

Reputation: 404

Android device doesn't wake up from Activity postponed action (FLAG_TURN_SCREEN_ON set but nothing happens)

My app get wakelock, but cannot wake up device from Activity

Here's my code:

public void onCreate(){
    super.onCreate();
    //acquire wakelock logic here
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            mHandler.obtainMessage().sendToTarget();
        }
    }, 30000);
.....
}
public Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        turnScreenOn();
    }
}

private void turnScreenOn() {
    Log.i("MainActivity", "Turn screen on");
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    );
}

In logs I see that turnScreenOn is executed but screen is still off. When I executing this method from onCreate() and Activity from Service - everything is Ok, but I want to execute it from Activity. I don't find any info that this is forbidden, I don't see any error in logs.

Is it possible somehow to wake up activity/application without starting it from service? If it's not possible - is it documented somewhere?

Thanks in advance

Upvotes: 1

Views: 288

Answers (1)

Mr_and_Mrs_D
Mr_and_Mrs_D

Reputation: 34026

I guess you should go for the AlarmManager - you should register a PendingIntent with the alarm manager and have a receiver receive the alarm - this is guaranteed to wake up the phone. Still you may need a service (WakefullIntentService) to manage the wakelocks for you. See PowerManager wakelock not waking device up from service for code.

Btw, TimerTasks are an old way of doing things anyway - scheduled pool executor is what you 'd use today IIRC.

Upvotes: 1

Related Questions