Example usage for android.app AlarmManager cancel

List of usage examples for android.app AlarmManager cancel

Introduction

In this page you can find the example usage for android.app AlarmManager cancel.

Prototype

public void cancel(OnAlarmListener listener) 

Source Link

Document

Remove any alarm scheduled to be delivered to the given OnAlarmListener .

Usage

From source file:org.ohmage.reminders.notif.Notifier.java

private static void cancelAllAlarms(Context context, int trigId) {
    AlarmManager alarmMan = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(context, NotifReceiver.class).setAction(ACTION_EXPIRE_ALM);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_NO_CREATE);

    if (pi != null) {
        alarmMan.cancel(pi);
        pi.cancel();/* w w  w . j a  v a2  s  . c  o m*/
    }

    i = new Intent(context, NotifReceiver.class).setAction(ACTION_REPEAT_ALM);
    pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_NO_CREATE);

    if (pi != null) {
        alarmMan.cancel(pi);
        pi.cancel();
    }
}

From source file:de.langerhans.wallet.WalletApplication.java

public static void scheduleStartBlockchainService(@Nonnull final Context context) {
    final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context));
    final long lastUsedAgo = config.getLastUsedAgo();

    // apply some backoff
    final long alarmInterval;
    if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_JUST_MS)
        alarmInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    else if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_RECENTLY_MS)
        alarmInterval = AlarmManager.INTERVAL_HALF_DAY;
    else//from w  ww  .jav a  2  s.co m
        alarmInterval = AlarmManager.INTERVAL_DAY;

    log.info("last used {} minutes ago, rescheduling blockchain sync in roughly {} minutes",
            lastUsedAgo / DateUtils.MINUTE_IN_MILLIS, alarmInterval / DateUtils.MINUTE_IN_MILLIS);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent startIntent = new Intent(context, AutosyncReceiver.class);
    startIntent.setAction("de.langerhans.wallet.AUTOSYNC_ACTION");
    final PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
    alarmManager.cancel(alarmIntent);

    // workaround for no inexact set() before KitKat
    final long now = System.currentTimeMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now + alarmInterval, AlarmManager.INTERVAL_DAY,
            alarmIntent);
}

From source file:org.videolan.vlc.gui.dialogs.AdvOptionsDialog.java

public static void setSleep(Calendar time) {
    AlarmManager alarmMgr = (AlarmManager) VLCApplication.getAppContext()
            .getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
    PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (time != null) {
        alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sleepPendingIntent);
    } else {/*  w ww  .j  a va2s.  c o m*/
        alarmMgr.cancel(sleepPendingIntent);
    }
    VLCApplication.sPlayerSleepTime = time;
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void deleteAlarm(Context context, Lecture lecture) {
    AlarmsDBOpenHelper alarmDB = new AlarmsDBOpenHelper(context);
    SQLiteDatabase db = alarmDB.getWritableDatabase();
    Cursor cursor;//from   ww  w .j  a  v a 2  s.co m

    try {
        cursor = db.query(AlarmsTable.NAME, AlarmsDBOpenHelper.allcolumns, AlarmsTable.Columns.EVENT_ID + "=?",
                new String[] { lecture.lecture_id }, null, null, null);
    } catch (SQLiteException e) {
        e.printStackTrace();
        MyApp.LogDebug("delete alarm", "failure on alarm query");
        db.close();
        return;
    }

    if (cursor.getCount() == 0) {
        db.close();
        cursor.close();
        MyApp.LogDebug("delete_alarm", "alarm for " + lecture.lecture_id + " not found");
        return;
    }

    cursor.moveToFirst();

    Intent intent = new Intent(context, AlarmReceiver.class);

    String lecture_id = cursor.getString(cursor.getColumnIndex(AlarmsTable.Columns.EVENT_ID));
    intent.putExtra(BundleKeys.ALARM_LECTURE_ID, lecture_id);
    int day = cursor.getInt(cursor.getColumnIndex(AlarmsTable.Columns.DAY));
    intent.putExtra(BundleKeys.ALARM_DAY, day);
    String title = cursor.getString(cursor.getColumnIndex(AlarmsTable.Columns.EVENT_TITLE));
    intent.putExtra(BundleKeys.ALARM_TITLE, title);
    long startTime = cursor.getLong(cursor.getColumnIndex(AlarmsTable.Columns.TIME));
    intent.putExtra(BundleKeys.ALARM_START_TIME, startTime);

    // delete any previous alarms of this lecture
    db.delete(AlarmsTable.NAME, AlarmsTable.Columns.EVENT_ID + "=?", new String[] { lecture.lecture_id });
    db.close();

    intent.setAction("de.machtnix.fahrplan.ALARM");
    intent.setData(Uri.parse("alarm://" + lecture.lecture_id));

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingintent = PendingIntent.getBroadcast(context, Integer.parseInt(lecture.lecture_id),
            intent, 0);

    // Cancel any existing alarms for this lecture
    alarmManager.cancel(pendingintent);

    lecture.has_alarm = false;
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void addAlarm(Context context, Lecture lecture, int alarmTimesIndex) {
    int[] alarm_times = { 0, 5, 10, 15, 30, 45, 60 };
    long when;/*from w w  w .j  a  v a2s.c  om*/
    Time time;
    long startTime;
    long startTimeInSeconds = lecture.dateUTC;

    if (startTimeInSeconds > 0) {
        when = startTimeInSeconds;
        startTime = startTimeInSeconds;
        time = new Time();
    } else {
        time = lecture.getTime();
        startTime = time.normalize(true);
        when = time.normalize(true);
    }
    long alarmTimeDiffInSeconds = alarm_times[alarmTimesIndex] * 60 * 1000;
    when -= alarmTimeDiffInSeconds;

    // DEBUG
    // when = System.currentTimeMillis() + (30 * 1000);

    time.set(when);
    MyApp.LogDebug("addAlarm", "Alarm time: " + time.format("%Y-%m-%dT%H:%M:%S%z") + ", in seconds: " + when);

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(BundleKeys.ALARM_LECTURE_ID, lecture.lecture_id);
    intent.putExtra(BundleKeys.ALARM_DAY, lecture.day);
    intent.putExtra(BundleKeys.ALARM_TITLE, lecture.title);
    intent.putExtra(BundleKeys.ALARM_START_TIME, startTime);
    intent.setAction(AlarmReceiver.ALARM_LECTURE);

    intent.setData(Uri.parse("alarm://" + lecture.lecture_id));

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingintent = PendingIntent.getBroadcast(context, Integer.parseInt(lecture.lecture_id),
            intent, 0);

    // Cancel any existing alarms for this lecture
    alarmManager.cancel(pendingintent);

    // Set new alarm
    alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingintent);

    int alarmTimeInMin = alarm_times[alarmTimesIndex];

    // write to DB

    AlarmsDBOpenHelper alarmDB = new AlarmsDBOpenHelper(context);

    SQLiteDatabase db = alarmDB.getWritableDatabase();

    // delete any previous alarms of this lecture
    try {
        db.beginTransaction();
        db.delete(AlarmsTable.NAME, AlarmsTable.Columns.EVENT_ID + "=?", new String[] { lecture.lecture_id });

        ContentValues values = new ContentValues();

        values.put(AlarmsTable.Columns.EVENT_ID, Integer.parseInt(lecture.lecture_id));
        values.put(AlarmsTable.Columns.EVENT_TITLE, lecture.title);
        values.put(AlarmsTable.Columns.ALARM_TIME_IN_MIN, alarmTimeInMin);
        values.put(AlarmsTable.Columns.TIME, when);
        DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
        values.put(AlarmsTable.Columns.TIME_TEXT, df.format(new Date(when)));
        values.put(AlarmsTable.Columns.DISPLAY_TIME, startTime);
        values.put(AlarmsTable.Columns.DAY, lecture.day);

        db.insert(AlarmsTable.NAME, null, values);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
    } finally {
        db.endTransaction();
        db.close();
    }

    lecture.has_alarm = true;
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void clearUpdateAlarm(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent alarmintent = new Intent(context, AlarmReceiver.class);
    alarmintent.setAction(AlarmReceiver.ALARM_UPDATE);

    PendingIntent pendingintent = PendingIntent.getBroadcast(context, 0, alarmintent, 0);

    MyApp.LogDebug(LOG_TAG, "clear update alarm");

    alarmManager.cancel(pendingintent);
}

From source file:me.acristoffers.tracker.AlarmReceiver.java

public static void setAlarm(final Context context) {
    try {/*from   w  w  w .jav a2  s.co m*/
        final Intent intent = new Intent(context, AlarmReceiver.class);
        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        final long fiveMinutes = 5 * 60 * 1000;

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        final String minutes = sharedPref.getString("sync_interval", "15");

        long repeatMinutes;
        try {
            repeatMinutes = Long.parseLong(minutes) * 60 * 1000;
        } catch (NumberFormatException e) {
            repeatMinutes = 15 * 60 * 1000;
        }

        final boolean active = sharedPref.getBoolean("autosync", true);

        if (active) {
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, fiveMinutes, repeatMinutes,
                    pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

From source file:org.sipdroid.sipua.ui.Receiver.java

public static void alarm(int renew_time, Class<?> cls) {
    if (!Sipdroid.release)
        Log.i("SipUA:", "alarm " + renew_time);
    Intent intent = new Intent(mContext, cls);
    PendingIntent sender = PendingIntent.getBroadcast(mContext, 0, intent, 0);
    AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    am.cancel(sender);
    if (renew_time > 0)
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + renew_time * 1000, sender);
}

From source file:info.papdt.blacklight.support.Utility.java

public static void stopServiceAlarm(Context context, Class<?> service) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, service);
    PendingIntent p = PendingIntent.getService(context, REQUEST_CODE, i, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(p);
}

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * Cancels the undo timeout for a notification action. This should be called if the undo
 * notification is clicked (to prevent the action from being performed anyway) or cleared (since
 * we have already performed the action).
 *//*ww w . j ava  2 s  . com*/
public static void cancelUndoTimeout(final Context context, final NotificationAction notificationAction) {
    LogUtils.i(LOG_TAG, "cancelUndoTimeout for %s", notificationAction.getNotificationActionType());

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    final PendingIntent pendingIntent = createUndoTimeoutPendingIntent(context, notificationAction);

    alarmManager.cancel(pendingIntent);
}