转载请注明出处:http://blog.csdn.net/droyon/article/details/21277799
oneShort
Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 30); // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);执行完一次就关闭。
repeat start service
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 15*1000; // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender);stop Service
Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0); // And cancel the alarm. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender);