android.app.AlarmManager类的使用及代码示例

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

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

AlarmManager介绍

暂无

代码示例

代码示例来源:origin: Marswin/MarsDaemon

private void initAlarm(Context context, String serviceName){
  if(mAlarmManager == null){
    mAlarmManager = ((AlarmManager)context.getSystemService(Context.ALARM_SERVICE));
  }
  if(mPendingIntent == null){
    Intent intent = new Intent();
    ComponentName component = new ComponentName(context.getPackageName(), serviceName);
    intent.setComponent(component);
    intent.setFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
    mPendingIntent = PendingIntent.getService(context, 0, intent, 0);
  }
  mAlarmManager.cancel(mPendingIntent);
}

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

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);

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

Intent i = new Intent(this, MyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
  SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
  AlarmManager.INTERVAL_DAY, 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: 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

public void scheduleAlarm(View v)
{
  // The time at which the alarm will be scheduled. Here the alarm is scheduled for 1 day from the current time. 
  // We fetch the current time in milliseconds and add 1 day's time
  // i.e. 24*60*60*1000 = 86,400,000 milliseconds in a day.       
  Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;

  // Create an Intent and set the class that will execute when the Alarm triggers. Here we have
  // specified AlarmReceiver in the Intent. The onReceive() method of this class will execute when the broadcast from your alarm is received.
  Intent intentAlarm = new Intent(this, AlarmReceiver.class);

  // Get the Alarm Service.
  AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

  // Set the alarm for a particular time.
  alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
  Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();       
}

代码示例来源: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: SecUSo/privacy-friendly-pedometer

public static void startHardwareStepCounter(Context context){
  Intent hardwareStepCounterServiceIntent = new Intent(context, HardwareStepCountReceiver.class);
  PendingIntent sender = PendingIntent.getBroadcast(context, 2, hardwareStepCounterServiceIntent, 0);
  AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(new Date());
  calendar.add(Calendar.MINUTE, 5);
  // Set inexact repeating alarm
  am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTime().getTime(), AlarmManager.INTERVAL_HOUR, sender);
  Log.i(LOG_CLASS, "Scheduled hardware step counter alert at start time " + calendar.toString());
}

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

public static void scheduleAlarm(Context context) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  calendar.add(Calendar.DAY_OF_MONTH, 1);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context,
      AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
  AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  am.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

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

getApplicationContext(),
    (int) mDeck.getLong("id"),
    new Intent(getApplicationContext(), ReminderService.class).putExtra
        (ReminderService.EXTRA_DECK_ID, mDeck.getLong("id")),
alarmManager.cancel(reminderIntent);
if ((Boolean) value) {
  final Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.HOUR_OF_DAY, reminder.getJSONArray("time").getInt(0));
  calendar.set(Calendar.MINUTE, reminder.getJSONArray("time").getInt(1));
  calendar.set(Calendar.SECOND, 0);
  alarmManager.setRepeating(
      AlarmManager.RTC_WAKEUP,
      calendar.getTimeInMillis(),
alarmManager.cancel(reminderIntent);
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(
    AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(),

代码示例来源:origin: mathisdt/trackworktime

private void schedulePeriodicIntents() {
  AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  Intent intentToSchedule = new Intent(context, Watchdog.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentToSchedule,
    PendingIntent.FLAG_CANCEL_CURRENT);
  Calendar cal = Calendar.getInstance();
  // start one minute after boot completed
  cal.add(Calendar.MINUTE, 1);
  // schedule once every minute
  service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), Constants.REPEAT_TIME,
    pendingIntent);
}

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

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: hypertrack/smart-scheduler-android

bundle.putInt(SmartScheduler.ALARM_JOB_ID_KEY, job.getJobId());
Intent intent = new Intent(mContext, SmartSchedulerAlarmReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, job.getJobId(), intent,
    PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
  alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
      job.getIntervalMillis(),
      pendingIntent);
  long triggerInMillis = calendar.getTimeInMillis() + job.getIntervalMillis();
    alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerInMillis, pendingIntent);
    alarm.setExact(AlarmManager.RTC_WAKEUP, triggerInMillis, pendingIntent);
    alarm.set(AlarmManager.RTC_WAKEUP, triggerInMillis, pendingIntent);

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

private void startAlarm() {
 AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
 Calendar calendar =  Calendar.getInstance();
 calendar.set(int year, int month, int date, int hour, int minute, int second);
 long when = calendar.getTimeInMillis();         // notification time
     Intent intent = new Intent(this, ReminderService.class);
     PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
     alarmManager.set(AlarmManager.RTC, when, 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

public class AlarmMonitorReceiver extends BroadcastReceiver{
public void onReceive(Context context,Intent intent) { 
 if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
  AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
  Intent intentAlarm = new Intent(context, ExampleReceiver.class); 
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarm, 0);
  Calendar time = Calendar.getInstance();
  time.setTimeInMillis(System.currentTimeMillis());
  time.add(Calendar.SECOND, 10);
  alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),10000,pendingIntent);         
 }  
}

相关文章