下面列出了android.app.AlarmManager#setExact ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setRetryTimer() {
if (CollectionServiceStarter.isBteWixelorWifiandBtWixel(getApplicationContext())
|| CollectionServiceStarter.isDexbridgeWixelorWifiandDexbridgeWixel(getApplicationContext())) {
long retry_in;
if(CollectionServiceStarter.isDexbridgeWixelorWifiandDexbridgeWixel(getApplicationContext())) {
retry_in = (1000 * 25);
}else {
retry_in = (1000*65);
}
Log.d(TAG, "setRetryTimer: Restarting in: " + (retry_in / 1000) + " seconds");
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
long wakeTime = calendar.getTimeInMillis() + retry_in;
PendingIntent serviceIntent = PendingIntent.getService(this, 0, new Intent(this, this.getClass()), 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, wakeTime, serviceIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarm.setExact(AlarmManager.RTC_WAKEUP, wakeTime, serviceIntent);
} else
alarm.set(AlarmManager.RTC_WAKEUP, wakeTime, serviceIntent);
}
}
/**
* Set alarm for 2 minutes to clear the clipboard
*/
protected void setClearClipboardAlarm() {
// create pending intent
AlarmManager alarmMgr = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getContext(), ClipboardAlarmReceiver.class);
intent.setAction("co.nano.nanowallet.alarm");
PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
// set a two minute alarm to start the pending intent
if (alarmMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120 * 1000, alarmIntent);
} else {
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120 * 1000, alarmIntent);
}
}
}
private void scheduleNextScan(Context context) {
Preferences prefs = new Preferences(context);
//int scanInterval = Integer.parseInt(settings.getString("pref_scaninterval", "30")) * 1000;
int scanInterval = prefs.getBackgroundScanInterval() * 1000;
if (scanInterval < 15 * 1000) scanInterval = 15 * 1000;
boolean batterySaving = prefs.getBatterySaverEnabled();
Intent intent = new Intent(context, BackgroundScanner.class);
PendingIntent sender = PendingIntent.getBroadcast(context, BackgroundScanner.REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager) context
.getSystemService(ALARM_SERVICE);
if (!batterySaving) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + scanInterval, sender);
}
else {
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + scanInterval, sender);
}
}
}
public void setFailoverTimer() { //Keep it alive!
if(AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NightWatchWidget.class)).length > 0
|| AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NightWatchWidgetWide.class)).length > 0) {
long retry_in = (1000 * 60 * 5);
Log.d(TAG, "Fallover Restarting in: " + (retry_in / (60 * 1000)) + " minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Adrian: do we even need to handle non-awake systems? Updating views on non-awake systems seems pointless.
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, WidgetUpdateService.class), 0));
} else if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarm.setExact(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, WidgetUpdateService.class), 0));
} else {
alarm.set(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, WidgetUpdateService.class), 0));
}
} else {
stopSelf();
}
}
public static void addAlarm(Context context, long time, int id){
// Date date = new Date();
/* if(time <= date.getTime()){
return;
}*/
Intent intent = new Intent(AntiHijackingActicon);
intent.setFlags(FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,new Date().getTime() + time,pi);
}else {
alarmManager.set(AlarmManager.RTC_WAKEUP,new Date().getTime() + time,pi);
}
}
public static PendingIntent scheduleRepeatingAlarm(long triggerAtMillis, long intervalMillis, int requestCode)
{
if (MainActivity.mainActivity != null)
{
Intent alarmIntent = new Intent(MainActivity.mainActivity, OBAlarmReceiver.class);
alarmIntent.putExtra(OBAlarmReceiver.EXTRA_ALARMTIME, triggerAtMillis);
alarmIntent.putExtra(OBAlarmReceiver.EXTRA_INTERVAL, intervalMillis);
alarmIntent.putExtra(OBAlarmReceiver.EXTRA_REQUESTCODE, requestCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.mainActivity, requestCode, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) MainActivity.mainActivity.getSystemService(Context.ALARM_SERVICE);
cancelAlarm(pendingIntent);
alarmManager.setExact(AlarmManager.RTC, triggerAtMillis, pendingIntent);
return pendingIntent;
}
return null;
}
/**
* 重启App
*
* @param context application
* @param delay 当前App被杀死之后,延迟多久重新启动。
*/
public static void restartApplication(Context context, int delay) {
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
PendingIntent restartIntent = PendingIntent.getActivity(context.getApplicationContext(), 0
, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long triggerTime = SystemClock.elapsedRealtime() + delay;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mgr.setExact(type, triggerTime, restartIntent);
} else {
mgr.set(type, triggerTime, restartIntent);
}
Process.killProcess(Process.myPid());
}
public static long wakeUpIntent(Context context, long delayMs, PendingIntent pendingIntent) {
final long wakeTime = JoH.tsl() + delayMs;
if (pendingIntent != null) {
Log.d(TAG, "Scheduling wakeup intent: " + dateTimeText(wakeTime));
final AlarmManager alarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
try {
alarm.cancel(pendingIntent);
} catch (Exception e) {
Log.e(TAG, "Exception cancelling alarm in wakeUpIntent: " + e);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (buggy_samsung && Pref.getBoolean("allow_samsung_workaround", true)) {
alarm.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeTime, null), pendingIntent);
} else {
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);
} else {
Log.e(TAG, "wakeUpIntent - pending intent was null!");
}
return wakeTime;
}
/**
* Set alarm for 2 minutes to clear the clipboard
*/
protected void setClearClipboardAlarm() {
// create pending intent
AlarmManager alarmMgr = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getContext(), ClipboardAlarmReceiver.class);
intent.setAction("co.banano.natriumwallet.alarm");
PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
// set a two minute alarm to start the pending intent
if (alarmMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120 * 1000, alarmIntent);
} else {
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120 * 1000, alarmIntent);
}
}
}
public static void post(Context context) {
long delay = Integer.valueOf(PreferencesUtil.getCheckGlucoseInterval(context)) * 60000 + 120000;
Log.i(TAG, "set next check: " + delay + " (" + new SimpleDateFormat("HH:mm:ss")
.format(new Date(delay + System.currentTimeMillis())) + ") "
+ new Exception().getStackTrace()[1]);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent intent = getAlarmReceiverIntent(context);
alarmManager.cancel(intent);
if (android.os.Build.VERSION.SDK_INT <= 18) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delay, intent);
} else {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + delay, intent);
}
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putLong("next_check", System.currentTimeMillis() + delay).apply();
}
public void setFailoverTimer() { //Sometimes it gets stuck in limbo on 4.4, this should make it try again
if (CollectionServiceStarter.isBTShare(getApplicationContext())) {
long retry_in = (1000 * 60 * 5);
Log.d(TAG, "Fallover Restarting in: " + (retry_in / (60 * 1000)) + " minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
if (pendingIntent != null)
alarm.cancel(pendingIntent);
long wakeTime = calendar.getTimeInMillis() + retry_in;
pendingIntent = PendingIntent.getService(this, 0, new Intent(this, this.getClass()), 0);
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);
} else {
stopSelf();
}
}
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);
}
private static void setTask(AlarmManager alarmManager, long triggerAtMillis, PendingIntent operation) {//RTC
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);
}
}
/**
* Set Alarm via AlarmManager
*
* Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift
* alarms in order to minimize wakeups and battery use. There are new APIs to
* support applications which need strict delivery guarantees
*
* @param context
* @param time
* @param pendingIntent
*/
private static void setAlarm(Context context, long time, PendingIntent pendingIntent) {
AlarmManager alarmManager =
(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}
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);
}
}
private void schedulePingTimeoutAlarm(long timeout) {
Intent intentAlarm = new Intent();
intentAlarm.setAction(ACTION_PING_TIMEOUT_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, HEARTBEAT_TIMEOUT_ALARM, intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
}
}
/**
* Wrapper to set alarm at exact time
* @see AlarmManager#setExact(int, long, PendingIntent)
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void setExactAlarm(AlarmManager alarmManager, int alarmType, long firstTime,
PendingIntent pendingIntent) {
if(isCompatible(Build.VERSION_CODES.KITKAT)) {
alarmManager.setExact(alarmType, firstTime, pendingIntent);
}else {
alarmManager.set(alarmType, firstTime, pendingIntent);
}
}
protected void setUpdateAlarm(@NonNull AlarmManager alarmManager, Calendar updateTime, PendingIntent alarmIntent)
{
if (Build.VERSION.SDK_INT >= 19)
alarmManager.setExact(AlarmManager.RTC, updateTime.getTimeInMillis(), alarmIntent);
else alarmManager.set(AlarmManager.RTC, updateTime.getTimeInMillis(), alarmIntent);
}
void setAlarm(AlarmModel alarm) {
alarm.setActive(1);
getAlarmDB().update(alarm);
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour());
calendar.set(Calendar.MINUTE, alarm.getMinute());
calendar.set(Calendar.SECOND, alarm.getSecond());
calendar.set(Calendar.DAY_OF_MONTH, alarm.getDay());
calendar.set(Calendar.MONTH, alarm.getMonth() - 1);
calendar.set(Calendar.YEAR, alarm.getYear());
Log.e(TAG, alarm.getAlarmId() + " - " + calendar.getTime().toString());
int alarmId = alarm.getAlarmId();
Intent intent = new Intent(mContext, AlarmReceiver.class);
intent.putExtra("intentType", ADD_INTENT);
intent.putExtra("PendingId", alarm.getId());
PendingIntent alarmIntent = PendingIntent.getBroadcast(mContext, alarmId, intent, 0);
AlarmManager alarmManager = this.getAlarmManager();
String scheduleType = alarm.getScheduleType();
if (scheduleType.equals("once")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
} else if (scheduleType.equals("repeat")) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarm.getInterval() * 1000, alarmIntent);
} else {
Log.d(TAG, "Schedule type should either be once or repeat");
return;
}
this.setBootReceiver();
}
/**
* 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));
}