Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android AlarmManager not waking phone up

I want an activity to be displayed at a certain time. For this, I am using AlarmManager. It works fine when the device is awake, but it doesn't wake it up if it's asleep.

My code for setting the alarm:

Calendar alarmTime = Calendar.getInstance(); alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour); alarmTime.set(Calendar.MINUTE, alarm.minute); alarmTime.set(Calendar.SECOND, 0);  if (alarmTime.before(now))     alarmTime.add(Calendar.DAY_OF_MONTH, 1);  Intent intent = new Intent(ctxt, AlarmReceiver.class); intent.putExtra("alarm", alarm); PendingIntent sender = PendingIntent.getBroadcast(ctxt, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), sender); 

My broadcast receiver:

@Override public void onReceive(Context context, Intent intent) {     try {  Bundle bundle = intent.getExtras(); final Alarm alarm = (Alarm) bundle.getSerializable("alarm");  Intent newIntent; if (alarm.type.equals("regular")) {     newIntent = new Intent(context, RegularAlarmActivity.class); } else if (alarm.type.equals("password")) {     newIntent = new Intent(context, PasswordAlarmActivity.class); } else if (alarm.type.equals("movement")) {     newIntent = new Intent(context, MovementAlarmActivity.class); } else {     throw new Exception("Unknown alarm type"); }     newIntent.putExtra("alarm", alarm);     newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     context.startActivity(newIntent);  } catch (Exception e) {     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();     Log.e("AlarmReceiver", Log.getStackTraceString(e)); } } 

This code doesn't wake the device on. However, when I turn it back again, they are displayed. I need to make them turn the screen on. Can you help me with this problem?

like image 408
Gabriel Avatar asked Jul 28 '11 19:07

Gabriel


People also ask

How AlarmManager works in Android?

AlarmManager is a bridge between application and Android system alarm service. It can send a broadcast to your app (which can be completely terminated by user) at a scheduled time and your app can then perform any task accordingly.

What is RTC alarm in Android?

There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time.

What is Android permission SCHEDULE EXACT alarm?

What is this permission? The new exact alarm permission ( SCHEDULE_EXACT_ALARM ) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.


2 Answers

I had a similar problem and the solution was to use WakeLocker. That should be done (preferably as the 1st thing in the receiver), or the device will wake up when the alarm is received, but will fall asleep again before context.startActivity(newIntent); is called. (I have also observed behavior when that does not happen, so it seems to be a bit arbitrary) So the easy and quick answer: Make a new class called WakeLocker with this source code:

package mypackage.test;  import android.content.Context; import android.os.PowerManager;  public abstract class WakeLocker {     private static PowerManager.WakeLock wakeLock;      public static void acquire(Context ctx) {         if (wakeLock != null) wakeLock.release();          PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);         wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |                 PowerManager.ACQUIRE_CAUSES_WAKEUP |                 PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);         wakeLock.acquire();     }      public static void release() {         if (wakeLock != null) wakeLock.release(); wakeLock = null;     } } 

and in your receiver call WakeLocker.acquire(context); as the 1st thing. Extra: it would also be neat to call WakeLocker.release(); once your alarm has done its thing.

like image 158
user859593 Avatar answered Sep 24 '22 22:09

user859593


Most likely, the alarm is waking up the device. However, AlarmManager broadcasts won't turn the screen on, and the device may well fall back asleep before your activity starts up.

You will need to acquire a WakeLock in onReceive() before calling startActivity(), and release that WakeLock after the user responds to your activity.

like image 40
CommonsWare Avatar answered Sep 23 '22 22:09

CommonsWare