how to wakeup android phone from sleep?
Asked Answered
B

3

10

How to wakeup android phone from sleep (suspend to mem) programmably? I don't want to acquire any wakelock, which means the phone goes into "real" sleep with the cpu disabled. I guess I can use some kind of RTC (real time clock) mechanism?

Does anyone have any examples?

Thanks.

Brigandine answered 14/4, 2014 at 0:32 Comment(3)
When exactly do you want it to wake up? After a certain timeout? On a certain date and time?Loquacity
Did you find a solution? Do you find any of the answers useful?Leukemia
Please if you find the answer to your question accept one from below, or modify your question so I can can help you further more.Leukemia
A
8

In order to let the Activity wake up the device and not require a password/swipe, you only need to add a few flags. To get that, include to your code:

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
    WindowManager.LayoutParams.FLAG_FULLSCREEN |
    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

This will wake up your App activity.

Anthrax answered 14/4, 2014 at 1:26 Comment(0)
L
6

I just wrote an application which can do this, here is some example code: First, I create an AlarmManager and set an alarm for a specific time:

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
// if the time is before now then add one day to it
if(calendar.getTimeInMillis() < System.currentTimeMillis())
   calendar.setTimeInMillis(calendar.getTimeInMillis()+86400000);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0);

I need a BroadcastReciever to recieve this alarm. For this I have to put into my manifest:

<application ...>
    <receiver android:name="hu.bendaf.example.AlarmReceiver"/>
...
</application>

and I also have the AlarmReciever class, which starts my main Activity on recieve:

public class AlarmReceiver extends BroadcastReceiver {
    public static final String WAKE = "Wake up";
    @Override
    public void onReceive(Context context, Intent intent) {
        //Starting MainActivity
        Intent myAct = new Intent(context, MainActivity.class);
        myAct.putExtra(WAKE, true);
        myAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myAct);
    }
}

and in my Activity's onCreate function I have:

// Wake up phone if needed
if(getIntent().hasExtra(AlarmReceiver.WAKE) && getIntent().getExtras().getBoolean(AlarmReceiver.WAKE)){
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

This code wakes up my phone at next 15:30:00 (either it is today or tomorrow).

Leukemia answered 13/8, 2015 at 18:50 Comment(2)
Does alarm wakeup from standby only, or also from suspend mode ?Odonto
As this answer says https://mcmap.net/q/355058/-android-sleep-standby-mode it's guaranteed to work in sleep mode also. That was what my tests confirmed also, the alarm was waking app my phone in every situation, except it was turned off.Leukemia
C
0

Use AlarmManager to broadcast an Intent at the time you want to do some work and have the device wake up. In the BroadcastReceiver, either do the work you need to do (if it is short), or obtain a WakeLock (probably via a singleton), start a service, have the service do the work, then have the service release the WakeLock.

You can read about that here: https://groups.google.com/forum/#!topic/android-developers/5--QRAPlFL0

Checkup answered 14/4, 2014 at 0:40 Comment(4)
hmmm..., I tried AlarmManager and it seemed it won't wakeup the phone if the phone is in "real" sleep (no wakelock).Brigandine
How to wake up the phone when its in the sleep mode.Commune
@Brigandine AlarmManager will work use setAndAllowWhileIdle() or setExactAndAllowWhileIdle() instead of set(). Alarms set in this manner will be bradcasted even when the device is asleep.Telluric
Does alarm wakeup from standby only, or also from suspend mode ?Odonto

© 2022 - 2024 — McMap. All rights reserved.