Moh'd Qassrawi
Moh'd Qassrawi

Reputation: 28

Alarm Manager doesn't work on time

I have this method which should schedule alarms but when the time arrives it doesn't start the pendingintent ??

public void setAlarm(String name, long time) {
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent dialog = new Intent(this, SubActivity.class);
    dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(this, 0, dialog, 0);

    if (Build.VERSION.SDK_INT >= 19) {
        if (System.currentTimeMillis() < time) {
            am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
        }else{
            time+=(AlarmManager.INTERVAL_DAY*7);
            am.setExact(AlarmManager.RTC_WAKEUP, time, pi);
        }
    } else {
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, AlarmManager.INTERVAL_DAY * 7, pi);
    }
}

Upvotes: 0

Views: 292

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

The problem is you are using a PendingIntent for an Activity, which will not necessarily keep the device awake long enough for the Activity to get started. You'll have to use a PendingIntent for a BroadcastReceiver which leverages a wake lock to keep the device awake until your app code can run. WakefulBrodcastReceiver is a good choice, or you can roll your own as needed. See this article for an explanation and sample of how to use alarms to wake the device: http://po.st/7UpipA

Upvotes: 2

Related Questions