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

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

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

AlarmManager.setRepeating介绍

暂无

代码示例

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

Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 
                   intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent);

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

static void scheduleAlarms(Context ctxt) {
  AlarmManager mgr=
    (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
  Intent i=new Intent(ctxt, PollReceiver.class);
  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + INITIAL_DELAY,
           PERIOD, pi);

 }
}

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

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = getStartPendingIntent(context);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    getTriggerAt(new Date()),
    NOTIFICATIONS_INTERVAL_IN_HOURS * AlarmManager.INTERVAL_HOUR,
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
return calendar.getTimeInMillis();

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

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
      new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pi);

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

static void scheduleAlarms(Context ctxt) {
  AlarmManager mgr=
    (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
  Intent i=new Intent(ctxt, PollReceiver.class);
  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + INITIAL_DELAY,
           PERIOD, pi);

 }
}

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

Intent myIntent = new Intent(ThisApp.this , myService.class);     
   AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
   pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
   Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, 12);
   calendar.set(Calendar.MINUTE, 00);
   calendar.set(Calendar.SECOND, 00);
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

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

static void scheduleAlarms(Context ctxt) {
  AlarmManager mgr=
    (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
  Intent i=new Intent(ctxt, PollReceiver.class);
  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + INITIAL_DELAY,
           PERIOD, pi);

 }
}

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

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

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

static void scheduleAlarms(Context ctxt) {
  AlarmManager mgr=
    (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
  Intent i=new Intent(ctxt, PollReceiver.class);
  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + INITIAL_DELAY,
           PERIOD, pi);

 }
}

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

Intent myIntent = new Intent(Current.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);  //set repeating every 24 hours

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

static void scheduleAlarms(Context ctxt) {
  AlarmManager mgr=
    (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
  Thingy thingy=
   new Thingy(mgr.getClass().getCanonicalName(), mgr.hashCode());
  Intent i=
   new Intent(ctxt, PollReceiver.class)
   .putExtra(EXTRA_THINGY, Parcelables.toByteArray(thingy));
  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

  mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
           SystemClock.elapsedRealtime() + INITIAL_DELAY,
           PERIOD, pi);
 }
}

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

Intent myIntent = new Intent(this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);

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

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mgr=(AlarmManager)getSystemService(ALARM_SERVICE);
 pi=createPendingResult(ALARM_ID, new Intent(), 0);
 mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
          SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
}

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

Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("packagename.ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
      0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

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

@Test
public void setRepeating_shouldReplaceDuplicates() {
 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR,
   PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR,
   PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
 assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1);
}

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

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 32);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

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

@Test
public void setRepeating_shouldRegisterAlarm() {
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull();
 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 0, INTERVAL_HOUR,
   PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0));
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNotNull();
}

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

Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();

if(intendedTime >= currentTime){ 
  // you can add buffer time too here to ignore some small differences in milliseconds
  // set from today
  alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
} else{
  // set from next day
  // you might consider using calendar.add() for adding one day to the current day
  firingCal.add(Calendar.DAY_OF_MONTH, 1);
  intendedTime = firingCal.getTimeInMillis();

  alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
}

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

@Test
public void getNextScheduledAlarm_shouldReturnRepeatingAlarms() {
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull();
 long now = new Date().getTime();
 Intent intent = new Intent(activity, activity.getClass());
 PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, now, INTERVAL_HOUR, pendingIntent);
 ShadowAlarmManager.ScheduledAlarm scheduledAlarm = shadowAlarmManager.getNextScheduledAlarm();
 assertThat(shadowAlarmManager.getNextScheduledAlarm()).isNull();
 assertRepeatingScheduledAlarm(now, INTERVAL_HOUR, pendingIntent, scheduledAlarm);
}

代码示例来源:origin: ankidroid/Anki-Android

public static void scheduleNotification(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    if (Integer.parseInt(sp.getString("minimumCardsDueForNotification", "1000001")) <= 1000000) {
      return;
    }

    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, sp.getInt("dayOffset", 0));
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    final PendingIntent notificationIntent =
        PendingIntent.getBroadcast(context, 0, new Intent(context, NotificationService.class), 0);
    alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY,
        notificationIntent
    );
  }
}

相关文章