static void scheduleExactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) { Intent i=new Intent(ctxt, PollReceiver.class) .putExtra(EXTRA_PERIOD, period) .putExtra(EXTRA_IS_DOWNLOAD, isDownload); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+period, pi); }
protected void plantOneOffExact(JobRequest request, AlarmManager alarmManager, PendingIntent pendingIntent) { long triggerAtMillis = getTriggerAtMillis(request); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(getType(true), triggerAtMillis, pendingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(getType(true), triggerAtMillis, pendingIntent); } else { alarmManager.set(getType(true), triggerAtMillis, pendingIntent); } logScheduled(request); }
private void registerExactAlarm(PendingIntent sender, long delayInMillis) { final int SDK_INT = Build.VERSION.SDK_INT; AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); long timeInMillis = (System.currentTimeMillis() + delayInMillis) / 1000 * 1000; //> example if (SDK_INT < Build.VERSION_CODES.KITKAT) { am.set(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) { am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } else if (SDK_INT >= Build.VERSION_CODES.M) { am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender); } }
static void scheduleExactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) { Intent i=new Intent(ctxt, PollReceiver.class) .putExtra(EXTRA_PERIOD, period) .putExtra(EXTRA_IS_DOWNLOAD, isDownload); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+period, pi); }
@Override public void schedule(long delayInMilliseconds) { long nextAlarmInMilliseconds = System.currentTimeMillis() + delayInMilliseconds; Log.d(TAG, "Schedule next alarm at " + nextAlarmInMilliseconds); AlarmManager alarmManager = (AlarmManager) service .getSystemService(Service.ALARM_SERVICE); if(Build.VERSION.SDK_INT >= 23){ // In SDK 23 and above, dosing will prevent setExact, setExactAndAllowWhileIdle will force // the device to run this task whilst dosing. Log.d(TAG, "Alarm scheule using setExactAndAllowWhileIdle, next: " + delayInMilliseconds); alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent); } else if (Build.VERSION.SDK_INT >= 19) { Log.d(TAG, "Alarm scheule using setExact, delay: " + delayInMilliseconds); alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarmInMilliseconds, pendingIntent); } }
public static void persistBundle(@NonNull Context context, @NonNull JobRequest request) { Intent intent = PlatformAlarmServiceExact.createIntent(context, request.getJobId(), request.getTransientExtras()); PendingIntent pendingIntent = PendingIntent.getService(context, request.getJobId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); long when = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1000); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC, when, pendingIntent); }
alarm.setExact(AlarmManager.RTC_WAKEUP, triggerInMillis, pendingIntent);
@Test @Config(minSdk = KITKAT) public void setExact_shouldRegisterAlarm_forApi19() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, 0, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); int ALARM_TYPE = AlarmManager.RTC_WAKEUP; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) am.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent); else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent); else am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
@Test @Config(minSdk = N) public void setExact_shouldRegisterAlarm_forApi124() { assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull(); OnAlarmListener listener = () -> {}; alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, 0, "tag", listener, null); assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull(); }
public static void setAlarm(Context context, Intent intent, int notificationId, Calendar calendar) { intent.putExtra("NOTIFICATION_ID", notificationId); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } }
Intent intent = new Intent(ctx, ReminderBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE); // time of of next reminder. Unix time. long timeMs =... if (Build.VERSION.SDK_INT < 19) { am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent); } else { am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent); }
void scheduleAlarm(Context context) { AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent yourIntent = new Intent(); //TODO configure your intent PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, yourIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION"); notificationIntent.addCategory("android.intent.category.DEFAULT"); PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 15); alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast); }
PendingIntent pi=PendingIntent.getBroadcast(context,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi); } else { alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi); }
PendingIntent pi=PendingIntent.getBroadcast(context,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { alarmManager. setExact(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi); } else { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi); }
@TargetApi(Build.VERSION_CODES.KITKAT) public static void startActivity(Class<?> cls) { Intent intent = new Intent(context, cls); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); long now = Calendar.getInstance().getTimeInMillis(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) alarmManager.setExact(AlarmManager.RTC_WAKEUP, now, pendingIntent); else alarmManager.set(AlarmManager.RTC_WAKEUP, now, pendingIntent); }
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE); Intent i1 = new Intent(this, ReceiveAlarmActivity.class); i1.putExtra("Key", "Value"); PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i1, PendingIntent.FLAG_ONE_SHOT); if (Build.VERSION.SDK_INT >= 19){ alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation); }else{ alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation); }
private static void scheduleNextUpdate(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); long now = new Date().getTime(); long nextUpdate = now + Long.parseLong(new Prefs(context).getTime()); Log.i(TAG, "Next widget update: " + android.text.format.DateFormat.getTimeFormat(context).format(new Date(nextUpdate))); if (Build.VERSION.SDK_INT >= 19) { alarmManager.setExact(AlarmManager.RTC, nextUpdate, getTimeIntent(context)); } else { alarmManager.set(AlarmManager.RTC, nextUpdate, getTimeIntent(context)); } }
/** * Schedule a time for the alert to ring at. * * @param context An active context instance. * @param manager The AlarmManager to schedule the alert on. */ public void setAlarm(Context context, AlarmManager manager) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) manager.setExact(AlarmManager.RTC_WAKEUP, endTime, getIntent(context)); else manager.set(AlarmManager.RTC_WAKEUP, endTime, getIntent(context)); }