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

The following examples show how to use android.app.AlarmManager#setWindow() . 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: McsService.java    From android_packages_apps_GmsCore with Apache License 2.0 8 votes vote down vote up
public void scheduleHeartbeat(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

    int heartbeatMs = GcmPrefs.get(this).getHeartbeatMsFor(activeNetworkPref, false);
    if (heartbeatMs < 0) {
        closeAll();
    }
    logd("Scheduling heartbeat in " + heartbeatMs / 1000 + " seconds...");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // This is supposed to work even when running in idle and without battery optimization disabled
        alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // With KitKat, the alarms become inexact by default, but with the newly available setWindow we can get inexact alarms with guarantees.
        // Schedule the alarm to fire within the interval [heartbeatMs/3*4, heartbeatMs]
        alarmManager.setWindow(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs / 4 * 3, heartbeatMs / 4,
                heartbeatIntent);
    } else {
        alarmManager.set(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
    }

}
 
Example 2
Source File: Whisper.java    From talk-android with MIT License 7 votes vote down vote up
public static void init(final Context context, boolean debugMode) {
    Intent serviceIntent = PushMsgService.startIntent(context, PushMsgService.ACTION_START_SERVICE);
    serviceIntent.putExtra(PushMsgService.EXTRA_DEBUG, debugMode);
    context.startService(serviceIntent);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Daemon.run(context, PushMsgService.class, Daemon.INTERVAL_ONE_MINUTE * 2);
        }
    }, 3000);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(AlarmReceiver.ACTION_ALARM);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (Build.VERSION.SDK_INT >= 19) {
        alarmManager.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                ALARM_REPEAT_INTERVAL, pendingIntent);
    } else {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                ALARM_REPEAT_INTERVAL, pendingIntent);
    }
}
 
Example 3
Source File: SuntimesWidget0.java    From SuntimesWidget with GNU General Public License v3.0 7 votes vote down vote up
/**
 * Start widget updates; register an alarm (inexactRepeating) that does not wake the device.
 * @param context the context
 */
protected void setUpdateAlarm( Context context, int alarmID )
{
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null)
    {
        PendingIntent alarmIntent = getUpdateIntent(context, alarmID);
        long updateTime = getUpdateTimeMillis(context, alarmID);
        if (updateTime > 0)
        {
            if (Build.VERSION.SDK_INT < 19) {
                alarmManager.set(AlarmManager.RTC, updateTime, alarmIntent);
            } else {
                alarmManager.setWindow(AlarmManager.RTC, updateTime, 5 * 1000, alarmIntent);
            }
            Log.d(TAG, "setUpdateAlarm: " + utils.calendarDateTimeDisplayString(context, updateTime).toString() + " --> " + getUpdateIntentFilter() + "(" + alarmID + ") :: " + utils.timeDeltaLongDisplayString(getUpdateInterval(), true) );
        } else Log.d(TAG, "setUpdateAlarm: skipping " + alarmID);
    }
}
 
Example 4
Source File: IdleReceiver.java    From JobSchedulerCompat with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void compatSetWindow(AlarmManager am, int type, long windowStartMillis, long windowLengthMillis, PendingIntent pendingIntent) {
    // Samsung devices have a bug where setWindow() may run before the start time.
    // https://code.google.com/p/android/issues/detail?id=69525
    boolean isShittySamsungDevice = Build.MANUFACTURER.equalsIgnoreCase("samsung");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && !isShittySamsungDevice) {
        am.setWindow(type, windowStartMillis, windowLengthMillis, pendingIntent);
    } else {
        am.set(type, windowStartMillis, pendingIntent);
    }
}
 
Example 5
Source File: AlarmManagerUtil.java    From Android-AlarmManagerClock with Apache License 2.0 5 votes vote down vote up
public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0),
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }
}
 
Example 6
Source File: AlarmManagerUtil.java    From Android-AlarmManagerClock with Apache License 2.0 5 votes vote down vote up
/**
 * @param flag            周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2
 *                        表示按周每周提醒的闹钟(一周的周期性时间间隔)
 * @param hour            时
 * @param minute          分
 * @param id              闹钟的id
 * @param week            week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
 * @param tips            闹钟提示信息
 * @param soundOrVibrator 2表示声音和震动都执行,1表示只有铃声提醒,0表示只有震动提醒
 */
public static void setAlarm(Context context, int flag, int hour, int minute, int id, int
        week, String tips, int soundOrVibrator) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    long intervalMillis = 0;
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get
            (Calendar.DAY_OF_MONTH), hour, minute, 10);
    if (flag == 0) {
        intervalMillis = 0;
    } else if (flag == 1) {
        intervalMillis = 24 * 3600 * 1000;
    } else if (flag == 2) {
        intervalMillis = 24 * 3600 * 1000 * 7;
    }
    Intent intent = new Intent(ALARM_ACTION);
    intent.putExtra("intervalMillis", intervalMillis);
    intent.putExtra("msg", tips);
    intent.putExtra("id", id);
    intent.putExtra("soundOrVibrator", soundOrVibrator);
    PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent
            .FLAG_CANCEL_CURRENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, calMethod(week, calendar.getTimeInMillis()),
                intervalMillis, sender);
    } else {
        if (flag == 0) {
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
        } else {
            am.setRepeating(AlarmManager.RTC_WAKEUP, calMethod(week, calendar.getTimeInMillis
                    ()), intervalMillis, sender);
        }
    }
}
 
Example 7
Source File: AlarmManagerUtil.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0),
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }
}
 
Example 8
Source File: AlarmManagerUtil.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
/**
 * @param flag            周期性时间间隔的标志,flag = 0 表示一次性的闹钟, flag = 1 表示每天提醒的闹钟(1天的时间间隔),flag = 2
 *                        表示按周每周提醒的闹钟(一周的周期性时间间隔)
 * @param hour            时
 * @param minute          分
 * @param id              闹钟的id
 * @param week            week=0表示一次性闹钟或者按天的周期性闹钟,非0 的情况下是几就代表以周为周期性的周几的闹钟
 * @param tips            闹钟提示信息
 * @param soundOrVibrator 2表示声音和震动都执行,1表示只有铃声提醒,0表示只有震动提醒
 */
public static void setAlarm(Context context, int flag, int hour, int minute, int id, int
        week, String tips, int soundOrVibrator) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    long intervalMillis = 0;
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get
            (Calendar.DAY_OF_MONTH), hour, minute, 10);
    if (flag == 0) {
        intervalMillis = 0;
    } else if (flag == 1) {
        intervalMillis = 24 * 3600 * 1000;
    } else if (flag == 2) {
        intervalMillis = 24 * 3600 * 1000 * 7;
    }
    Intent intent = new Intent(ALARM_ACTION);
    intent.putExtra("intervalMillis", intervalMillis);
    intent.putExtra("msg", tips);
    intent.putExtra("id", id);
    intent.putExtra("soundOrVibrator", soundOrVibrator);
    PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent
            .FLAG_CANCEL_CURRENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, calMethod(week, calendar.getTimeInMillis()),
                intervalMillis, sender);
    } else {
        if (flag == 0) {
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
        } else {
            am.setRepeating(AlarmManager.RTC_WAKEUP, calMethod(week, calendar.getTimeInMillis
                    ()), intervalMillis, sender);
        }
    }
}
 
Example 9
Source File: JobProxy19.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Override
protected void plantOneOffInexact(JobRequest request, AlarmManager alarmManager, PendingIntent pendingIntent) {
    long currentTime = System.currentTimeMillis();
    long startMs = currentTime + Common.getStartMs(request);
    long lengthMs = Common.getEndMs(request) - Common.getStartMs(request);

    alarmManager.setWindow(AlarmManager.RTC, startMs, lengthMs, pendingIntent);

    mCat.d("Schedule alarm, %s, start %s, end %s", request,
            JobUtil.timeToString(Common.getStartMs(request)), JobUtil.timeToString(Common.getEndMs(request)));
}
 
Example 10
Source File: JobProxy19.java    From android-job with Apache License 2.0 5 votes vote down vote up
@Override
protected void plantOneOffFlexSupport(JobRequest request, AlarmManager alarmManager, PendingIntent pendingIntent) {
    long currentTime = System.currentTimeMillis();
    long startMs = currentTime + Common.getStartMsSupportFlex(request);
    long lengthMs = Common.getEndMsSupportFlex(request) - Common.getStartMsSupportFlex(request);

    alarmManager.setWindow(AlarmManager.RTC, startMs, lengthMs, pendingIntent);

    mCat.d("Scheduled repeating alarm (flex support), %s, start %s, end %s, flex %s", request,
            JobUtil.timeToString(Common.getStartMsSupportFlex(request)), JobUtil.timeToString(Common.getEndMsSupportFlex(request)),
            JobUtil.timeToString(request.getFlexMs()));
}