android.app.AlarmManager.setExactAndAllowWhileIdle()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中android.app.AlarmManager.setExactAndAllowWhileIdle()方法的一些代码示例,展示了AlarmManager.setExactAndAllowWhileIdle()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AlarmManager.setExactAndAllowWhileIdle()方法的具体详情如下:
包路径:android.app.AlarmManager
类名称:AlarmManager
方法名:setExactAndAllowWhileIdle

AlarmManager.setExactAndAllowWhileIdle介绍

暂无

代码示例

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M)
public void setExactAndAllowWhileIdle_shouldRegisterAlarm() {
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull();
 alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, 0,
   PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull();
}

代码示例来源:origin: evernote/android-job

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);
}

代码示例来源:origin: stackoverflow.com

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);
  }
}

代码示例来源:origin: eclipse/paho.mqtt.android

@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);
  }
}

代码示例来源:origin: hypertrack/smart-scheduler-android

alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerInMillis, pendingIntent);

代码示例来源:origin: redfish64/TinyTravelTracker

public static void setAlarm(AlarmManager alarmManager, int elapsedRealtimeWakeup, long timeToWakeFromPhoneBoot, PendingIntent sender)
  {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWakeFromPhoneBoot, sender);
  }
}

代码示例来源:origin: metinkale38/prayer-times-android

public void setExact(int type, long time, PendingIntent service) {
  if (type == AlarmManager.RTC_WAKEUP && Build.VERSION.SDK_INT >= 23) {
    alarmManager.setExactAndAllowWhileIdle(type, time, service);
  } else /*if (Build.VERSION.SDK_INT >= 19)*/ {
    alarmManager.setExact(type, time, service);
  }/* else {
    alarmManager.set(type, time, service);
  }*/
}

代码示例来源:origin: jonasbleyl/recurrence

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);
  }
}

代码示例来源:origin: tagtime/TagTime

private void setAlarm(long PING) {
  AlarmManager alarum = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent alit = new Intent(this, TPStartUp.class);
  alit.putExtra("ThisIntentIsTPStartUpClass", true);
  assert alarum != null;
  int type = AlarmManager.RTC_WAKEUP;
  long trigger = PING * 1000;
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alit, 0);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    alarum.setExactAndAllowWhileIdle(type, trigger, pendingIntent);
  } else {
    alarum.set(type, trigger, pendingIntent);
  }
}

代码示例来源:origin: antest1/kcanotify

private void setAlarm(long time, PendingIntent alarmIntent, int code, boolean delay) {
  if (time == -1) {
    time = System.currentTimeMillis();
  } else if (delay) {
    time = time - KcaAlarmService.ALARM_DELAY;
    if (time < System.currentTimeMillis()) return;
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, alarmIntent);
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, time, alarmIntent);
  } else {
    alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, time, alarmIntent);
  }
  Log.e("KCA", "Alarm set to: " + String.valueOf(time) + " " + String.valueOf(code));
}

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: stackoverflow.com

public class PollReceiver extends WakefulBroadcastReceiver{
  static final String PERIOD = "period";
  @Override
  public void onReceive(Context context, Intent intent){
     startWakefulService(context,new Intent(context,MyService.class));
     long period = intent.getLongExtra(PERIOD,-1);
     if(period>0){
     scheduleExactAlarm(context,(AlarmManager)context.getSystemService(Context.ALARM_SERVICE),period)
     }
  }

  static void scheduleExactAlarm(Context context,AlarmManager alarms, long period){
  Intent i = new Intent(context,PollReceiver.class);
  PendingIntent pi = PendingIntent.getBroadcast(context,0,i,0);
  if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1){
    alarms.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + period,pi);
  }
}

代码示例来源:origin: stackoverflow.com

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

// if you would like to fire the next alarm very precisely
// put this value in the Intent and use it in your receiver
// to calculate the next time for the alarm
long fireAt = SystemClock.elapsedRealtime() + 5000;

if(Build.VERSION.SDK_INT >= 23) {
  am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        fireAt, pendingIntent);
} else if(Build.VERSION.SDK_INT >= 19) {
  am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
} else {
  am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
}

代码示例来源:origin: jclehner/rxdroid

private void setAlarm(long triggerAtMillis, PendingIntent operation)
{
  final int mode = AlarmManager.RTC_WAKEUP;
  if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
    mAlarmMgr.set(mode, triggerAtMillis, operation);
  else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
    mAlarmMgr.setExact(mode, triggerAtMillis, operation);
  else
    mAlarmMgr.setExactAndAllowWhileIdle(mode, triggerAtMillis, operation);
}

代码示例来源:origin: stackoverflow.com

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//get the alarm manager
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 23)
{
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19)
{
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);

代码示例来源:origin: ywwynm/EverythingDone

public static void setHabitReminderAlarm(Context context, long id, long notifyTime) {
  AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, HabitReceiver.class);
  intent.putExtra(Def.Communication.KEY_ID, id);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) id, intent,
      PendingIntent.FLAG_UPDATE_CURRENT);
  if (DeviceUtil.hasMarshmallowApi()) {
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  } else if (DeviceUtil.hasKitKatApi()) {
    am.setExact(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  } else {
    am.set(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  }
}

代码示例来源:origin: ywwynm/EverythingDone

public static void setReminderAlarm(Context context, long id, long notifyTime) {
  AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, ReminderReceiver.class);
  intent.putExtra(Def.Communication.KEY_ID, id);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(
      context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  if (DeviceUtil.hasMarshmallowApi()) {
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  } else if (DeviceUtil.hasKitKatApi()) {
    am.setExact(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  } else {
    am.set(AlarmManager.RTC_WAKEUP, notifyTime, pendingIntent);
  }
}

代码示例来源:origin: jamorham/xDrip-plus

public static long wakeUpIntentOld(Context context, long delayMs, PendingIntent pendingIntent) {
  final long wakeTime = JoH.tsl() + delayMs;
  Log.d(TAG, "Scheduling wakeup intent: " + dateTimeText(wakeTime));
  final AlarmManager alarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    alarm.setExact(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
  } else
    alarm.set(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
  return wakeTime;
}

代码示例来源:origin: BolexLiu/TimeTask

/**
 * 装在定时任务
 * @param Time
 */
private void configureAlarmManager(long Time) {
  AlarmManager manager = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
  PendingIntent pendIntent = getPendingIntent();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, Time, pendIntent);
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    manager.setExact(AlarmManager.RTC_WAKEUP, Time, pendIntent);
  } else {
    manager.set(AlarmManager.RTC_WAKEUP, Time, pendIntent);
  }
}

代码示例来源:origin: henrichg/PhoneProfilesPlus

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);
}

相关文章