开发者

Can I wake up my Android when it is unplugged and sleeping?

开发者 https://www.devze.com 2023-02-16 14:21 出处:网络
I have made an app that allows one to listen to the radio and have implemented an alarm so that I can have the radio play when the alarm goes off. I am using the alarmManager and RTC_开发者_开发技巧wa

I have made an app that allows one to listen to the radio and have implemented an alarm so that I can have the radio play when the alarm goes off. I am using the alarmManager and RTC_开发者_开发技巧wakeup, and it seems to work fine if the phone is plugged in or if the phone is not asleep (which kind of defeats the purpose). When the phone is unplugged and asleep, however, the alarm does not go off until I wake up the phone.

Does anyone know a solution to this?


This is how we did it and it works in both case that you describe:

PendingIntent pi = null;

private void startMonitor() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, OnTickReceiver.class);
    pi = PendingIntent.getBroadcast(this, 0, i, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + 120 * 1000, 120 * 1000, pi);
}

private void stopMonitor() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pi);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号