Android AlarmManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android AlarmManager class.

This code example shows how to use the following methods:cancel, set
 
    public static void scheduleReengagementAlarm(Context context) {
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(BROADCAST_SHOW_REENGAGEMENT);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
        am.cancel(pendingIntent);
 
        long time = getNextReminderTime();
        am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    } 
 
    private static long getNextReminderTime() { 
        int reengagementReminders = Preferences.getInt(PREF_REENGAGEMENT_COUNT, 1);
        int days;
        if (DateUtilities.now() - Preferences.getLong(AstridPreferences.P_FIRST_LAUNCH, 0) > DateUtilities.ONE_DAY * 30) { // Installed longer than 30 days
            // Sequence: every 6, 8, 10 days 
            days = Math.min(10, 4 + 2 * reengagementReminders);
        } else { 
            // Sequence: every 2, 3, 4, 5 days 
            days = Math.min(5, 1 + reengagementReminders);
9
CodeRank
This code example shows how to use the following methods:cancel
     
    unregisterReceiver(onNotice);
  } 
   
  public void cancelAlarm(View v) {
    mgr.cancel(pi);
  } 
   
  private BroadcastReceiver onNotice=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent i) { 
      notice.setText(new Date().toString()); 
      abortBroadcast(); 
    } 
  }; 
} 
9
CodeRank
This code example shows how to use the following methods:cancel, set
    } else { 
      Log.w("DirectoryUpdateReceiver", "Scheduling for time found in preferences: " +
            scheduledTime);
    } 
 
    am.cancel(sender);
    am.set(AlarmManager.RTC_WAKEUP, scheduledTime, sender);
 
    Log.w("DirectoryUpdateReceiver", "Scheduled for: " + scheduledTime);
  } 
} 
9
CodeRank
This code example shows how to use the following methods:cancel
        mAlarmService.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggetRealTimeMillis,
                mUpdateIdleMaintenanceStatePendingIntent);
    } 
 
    private void unscheduleUpdateIdleMaintenanceState() { 
        mAlarmService.cancel(mUpdateIdleMaintenanceStatePendingIntent);
    } 
 
    private void updateIdleMaintenanceState() { 
        if (mIdleMaintenanceStarted) {
            // Idle maintenance can be interrupted by user activity, or duration 
            // time out, or low battery. 
            if (!lastUserActivityPermitsIdleMaintenanceRunning() 
                    || !batteryLevelAndMaintenanceTimeoutPermitsIdleMaintenanceRunning()) { 
                unscheduleUpdateIdleMaintenanceState(); 
                mIdleMaintenanceStarted = false;
                EventLogTags.writeIdleMaintenanceWindowFinish(SystemClock.elapsedRealtime(),
                        mLastUserActivityElapsedTimeMillis, mBatteryService.getBatteryLevel(),
                        isBatteryCharging() ? 1 : 0); 
                sendIdleMaintenanceEndIntent(); 
8
CodeRank
This code example shows how to use the following methods:setRepeating
This code example shows how to use the following methods:cancel, set
        AlarmManager alarmManager =
              (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
 
        if (pending != null) {
            // Cancel any previous alarms that do the same thing. 
            alarmManager.cancel(pending);
            Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm");
        } 
        pending = PendingIntent.getBroadcast(
            context, 0, intent, 0);
 
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
    } 
 
    static void cancelDiscoverableAlarm(Context context) {
        Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
 
        Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
        intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(
9
CodeRank
This code example shows how to use the following methods:cancel
                     SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
  } 
 
  @Override 
  public void onDestroy() { 
    mgr.cancel(pi);
 
    super.onDestroy(); 
  } 
 
  @Override 
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == ALARM_ID) {
      Toast.makeText(this, R.string.toast, Toast.LENGTH_SHORT).show();
    } 
  } 
} 
8
CodeRank
This code example shows how to use the following methods:set
            // postpone notification service launch a minute, 
            // we don't want to slow down booting 
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent i = new Intent(context, OnAlarmReceiver.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1
                    * DateUtils.MINUTE_IN_MILLIS, pi);
            Log.d("OnAlarmReceiver", "Postponing service launch");
        } else { 
            // run the notification service right away 
            Log.d("OnAlarmReceiver", "Run notifications service right away");
            Utils.runNotificationService(context);
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:cancel, setRepeating
 
        Log.i("Astrid", "Autosync set for " + offset / 1000 //$NON-NLS-1$ //$NON-NLS-2$
            + " seconds repeating every " + syncFrequencySeconds); //$NON-NLS-1$
 
        // cancel all existing 
        am.cancel(pendingIntent);
 
        // schedule new 
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + offset,
                interval, pendingIntent);
    } 
 
 
    /** 
     * Removes repeating alarm for auto-synchronization 
     */ 
    private void unscheduleService(Context context) {
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(context, getSyncUtilities().getSyncIntervalKey(),
                createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT);
7
CodeRank
This code example shows how to use the following methods:cancel, setRepeating
    public static void resetAlarm(Context context, long interval) {
        // Create an IntentSender that will launch our service, to be scheduled 
        // with the alarm manager. 
        PendingIntent alarmSender = PendingIntent.getService(context, 0, new Intent(context, EnvelopeService.class), 0);
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        am.cancel(alarmSender);
        if (interval != 0)
        	am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), interval, alarmSender);
    } 
} 
 
7
CodeRank

Related Android AlarmManager Questions & Answers:

See Code Examples for Android AlarmManager Methods: