Example usage for android.content Context ALARM_SERVICE

List of usage examples for android.content Context ALARM_SERVICE

Introduction

In this page you can find the example usage for android.content Context ALARM_SERVICE.

Prototype

String ALARM_SERVICE

To view the source code for android.content Context ALARM_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AlarmManager for receiving intents at a time of your choosing.

Usage

From source file:Main.java

public static void cancel(Context context, Intent intent) {
    PendingIntent p = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarm.cancel(p);/*from  w  w w . j a v  a 2  s . c o  m*/
    Log.d("petsitter-alarm", "Alarme cancelado.");
}

From source file:Main.java

public static void cancelAlarm(Context context, String action, int id) {
    Intent intent = new Intent(action);
    PendingIntent pi = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);//from w ww . j a va 2s.c  o  m
}

From source file:Main.java

public static void scheduleRepeat(Context context, Intent intent, Long triggerAtMillis, Long intervalMillis) {
    PendingIntent p = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, p);

    Log.d("petsitter-alarm", "Alarme agendado com sucesso com repeat.");
}

From source file:Main.java

public static void exitAndRestart(Context context, Class<?> activityCls) {
    Intent intent = new Intent(context, activityCls);
    PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(1, System.currentTimeMillis() + 1000L, restartIntent);
}

From source file:Main.java

/**
 * Restart the app./*from w w  w .j  a  v a2  s .c  om*/
 * @param context
 */
public static void restartApplication(Context context) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent p = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, p);

    //kill the application
    System.exit(0);
}

From source file:Main.java

public static PendingIntent scheduleLocalNotification(Context context, int slot, Long alertTime,
        String titleText, String subtitleText, String messageBodyText, String tickerText) {
    Log.i(TAG, "Scheduling local notification");
    Intent alertIntent = new Intent(getNotificationName(slot));
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, slot, alertIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
    }/*from ww  w .j av  a 2 s .  c o  m*/
    return pendingIntent;
}

From source file:Main.java

public static PendingIntent scheduleLocalNotification(Context context, int slot, String title, String message,
        Long alertTime, int repeat) {
    Log.i(TAG, "Scheduling local notification");
    Intent alertIntent = new Intent(getNotificationName(slot));

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, slot, alertIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        if (repeat == 0) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
        } else {/*from  ww  w. ja va 2 s  .  co  m*/
            Calendar alarmStartTime = Calendar.getInstance();
            alarmStartTime.add(Calendar.MINUTE, 1);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, repeat, pendingIntent);
        }
    }
    return pendingIntent;
}

From source file:Main.java

public static synchronized void cancelServiceAlarm(Context context, Intent intent, int identifier) {

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent pi = PendingIntent.getService(context, identifier, intent,
            PendingIntent.FLAG_NO_CREATE);
    if (pi != null) {
        am.cancel(pi);/*  w w  w.  j  av a2s  .c o  m*/
        pi.cancel();
    }

}

From source file:com.licenta.android.licenseapp.alarm.AlarmReceiver.java

public static void setAlarm(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    /*//from  w ww.  ja  v a  2 s.  com
     * If you don't have precise time requirements, use an inexact repeating alarm
     * the minimize the drain on the device battery.
     *
     * The call below specifies the alarm type, the trigger time, the interval at
     * which the alarm is fired, and the alarm's associated PendingIntent.
     * It uses the alarm type RTC_WAKEUP ("Real Time Clock" wake up), which wakes up
     * the device and triggers the alarm according to the time of the device's clock.
     *
     * Alternatively, you can use the alarm type ELAPSED_REALTIME_WAKEUP to trigger
     * an alarm based on how much time has elapsed since the device was booted. This
     * is the preferred choice if your alarm is based on elapsed time--for example, if
     * you simply want your alarm to fire every 60 minutes. You only need to use
     * RTC_WAKEUP if you want your alarm to fire at a particular date/time. Remember
     * that clock-based time may not translate well to other locales, and that your
     * app's behavior could be affected by the user changing the device's time setting.
     *
     */

    // Wake up the device to fire the alarm in 30 minutes, and every 30 minutes
    // after that.
    long intervalMillis;
    String intervalVal = prefs.getString("repeat_interval", "0");
    switch (intervalVal) {
    case "15":
        intervalMillis = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
        break;
    case "30":
        intervalMillis = AlarmManager.INTERVAL_HALF_HOUR;
        break;
    case "60":
        intervalMillis = AlarmManager.INTERVAL_HOUR;
        break;
    default:
        intervalMillis = 0;
        break;
    }

    // for testing
    intervalMillis = 6000;

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + intervalMillis, intervalMillis, alarmIntent);

    prefs.edit().putBoolean(Constants.PREF_KEY_IS_ALARM_ON, true).apply();
    Log.d(context.getClass().getName(), "Alarm started");

}

From source file:Main.java

public static synchronized long setServiceAlarm(Context context, Intent intent, long timeInterval,
        int identifier) {

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent pi = PendingIntent.getService(context, identifier, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    return setUpAlarm(am, pi, timeInterval);
}