Java Code Examples for android.app.AlarmManager#setExact()

The following examples show how to use android.app.AlarmManager#setExact() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDialogFragment.java    From nano-wallet-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 2
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 3
Source File: AlarmReceiver.java    From LibreAlarm with GNU General Public License v3.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: BaseDialogFragment.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 5
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: ApplicationUtils.java    From QPM with Apache License 2.0 6 votes vote down vote up
/**
 * 重启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());
}
 
Example 7
Source File: OBAlarmManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: AlarmHelper.java    From MissZzzReader with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
 
Example 9
Source File: WidgetUpdateService.java    From NightWatch with GNU General Public License v3.0 6 votes vote down vote up
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();
    }
}
 
Example 10
Source File: BackgroundScanner.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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);
        }
    }
}
 
Example 11
Source File: DexCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
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);
    }
}
 
Example 12
Source File: JobProxy14.java    From android-job with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: TasksUtils.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: AlarmScheduler.java    From X-Alarm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 15
Source File: AlarmUtil.java    From recurrence with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 16
Source File: Socket.java    From talk-android with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: Compatibility.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 18
Source File: SuntimesActivity.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
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);
}
 
Example 19
Source File: AlarmUtil.java    From react-native-alarm-notification with MIT License 4 votes vote down vote up
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();
}
 
Example 20
Source File: TimerData.java    From Alarmio with Apache License 2.0 2 votes vote down vote up
/**
 * 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));
}