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

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

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

AlarmManager.setExact介绍

暂无

代码示例

代码示例来源:origin: commonsguy/cw-omnibus

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

代码示例来源: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: commonsguy/cw-omnibus

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

代码示例来源: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: evernote/android-job

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

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

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

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

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

代码示例来源: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: robolectric/robolectric

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

代码示例来源: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: stackoverflow.com

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

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

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

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

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

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

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

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

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

代码示例来源:origin: fg607/RelaxFinger

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

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

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

代码示例来源:origin: Sparker0i/Weather

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

代码示例来源:origin: fennifith/Alarmio

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

相关文章