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:com.capstone.transit.trans_it.RouteMap.java

@Override
protected void onPause() {
    super.onPause();
    stopService(positionsServiceIntent);
    positionsServiceIntent = new Intent(getApplicationContext(), RefreshPositionsService.class);
    final PendingIntent pendingIntent = PendingIntent.getService(this, 0, positionsServiceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pendingIntent);/*from w w w . ja  va  2  s  .c o  m*/
}

From source file:com.chess.genesis.net.GenesisNotifier.java

public static void ScheduleWakeup(final Context context) {
    final Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, Pref.getInt(context, R.array.pf_notifierPolling));
    final long start = cal.getTimeInMillis();
    final long interval = start - System.currentTimeMillis();

    final Intent intent = new Intent(context, GenesisAlarm.class);
    final PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, start, interval, pintent);
}

From source file:com.android.example.alwaysonstopwatch.StopwatchActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stopwatch);

    // Enable Ambient Mode
    setAmbientEnabled();/*  ww  w. j a va  2 s . c  o m*/

    mAmbientStateAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Create pending intent
    Intent intent = new Intent(getApplicationContext(), StopwatchActivity.class);
    mAmbientStatePendingIntent = PendingIntent.getActivity(getApplicationContext(), R.id.msg_update, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Get on screen items
    mStartStopButton = (Button) findViewById(R.id.startstopbtn);
    mResetButton = (Button) findViewById(R.id.resetbtn);
    mTimeView = (TextView) findViewById(R.id.timeview);
    resetTimeView(); // initialise TimeView

    mBackground = findViewById(R.id.gridbackground);
    mClockView = (TextClock) findViewById(R.id.clock);
    mNotice = (TextView) findViewById(R.id.notice);
    mNotice.getPaint().setAntiAlias(false);
    mActiveBackgroundColor = ContextCompat.getColor(this, R.color.activeBackground);
    mActiveForegroundColor = ContextCompat.getColor(this, R.color.activeText);

    mStartStopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "Toggle start / stop state");
            toggleStartStop();
        }
    });

    mResetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "Reset time");
            mLastTick = 0L;
            mTimeSoFar = 0L;
            resetTimeView();
        }
    });

    mActiveClockUpdateHandler.sendEmptyMessage(R.id.msg_update);
}

From source file:de.incoherent.suseconferenceclient.tasks.CheckForUpdatesTask.java

@Override
protected Long doInBackground(Void... params) {
    String kUrl = "https://conference.opensuse.org/osem/api/v1/conferences/gRNyOIsTbvCfJY5ENYovBA";
    if (kUrl.length() <= 0)
        return 0l;

    String updatesUrl = mConference.getUrl() + "/updates.json";
    int lastUpdateRevision = mDb.getLastUpdateValue(mConference.getSqlId());
    int revisionLevel = lastUpdateRevision;

    try {//from w  ww  . j  a  v  a  2s  . c o  m
        JSONObject updateReply = HTTPWrapper.get(updatesUrl);
        if (updateReply == null)
            return 0l;
        int newLevel = updateReply.getInt("revision");
        if (newLevel > revisionLevel) {
            long id = mConference.getSqlId();
            // Cache favorites and alerts
            List<String> favoriteGuids = mDb.getFavoriteGuids(id);
            List<Event> alerts = mDb.getAlertEvents(id);
            List<String> alertGuids = new ArrayList<String>();
            // Now cancel all of the outstanding alerts, in case
            // a talk has been moved
            AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
            for (Event e : alerts) {
                alertGuids.add("\"" + e.getGuid() + "\"");
                Log.d("SUSEConferences", "Removing an alert for " + e.getTitle());

                Intent intent = new Intent(mContext, AlarmReceiver.class);
                intent.putExtras(ScheduleDetailsActivity.generateAlarmIntentBundle(mContext, e));
                PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
                        intent.getStringExtra("intentId").hashCode(), intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                manager.cancel(pendingIntent);
                pendingIntent.cancel();
            }

            // Now clear the DB
            mDb.clearDatabase(id);
            // Download schedule
            ConferenceCacher cacher = new ConferenceCacher(new ConferenceCacherProgressListener() {
                @Override
                public void progress(String progress) {
                    publishProgress(progress);
                }
            });

            long val = cacher.cacheConference(mConference, mDb);
            mErrorMessage = cacher.getLastError();
            if (val == -1) {
                mDb.setConferenceAsCached(id, 0);
            } else {
                mDb.setLastUpdateValue(id, newLevel);
                mDb.toggleEventsInMySchedule(favoriteGuids);
                mDb.toggleEventAlerts(alertGuids);
                alerts = mDb.getAlertEvents(id);
                // ... And re-create the alerts, if they are in the future
                Date currentDate = new Date();
                for (Event e : alerts) {
                    if (currentDate.after(e.getDate()))
                        continue;
                    Log.d("SUSEConferences", "Adding an alert for " + e.getTitle());
                    alertGuids.add("\"" + e.getGuid() + "\"");
                    Intent intent = new Intent(mContext, AlarmReceiver.class);
                    intent.putExtras(ScheduleDetailsActivity.generateAlarmIntentBundle(mContext, e));
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
                            intent.getStringExtra("intentId").hashCode(), intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    manager.set(AlarmManager.RTC_WAKEUP, e.getDate().getTime() - 300000, pendingIntent);
                }

            }
            return val;
        } else {
            return 0l;
        }
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:damo.three.ie.prepay.UpdateService.java

@Override
protected void onHandleIntent(Intent intent) {

    Context context = getApplicationContext();
    try {//from   w  w  w  .j  a  va  2 s  . c  o  m
        Log.d(Constants.TAG, "Fetching usages from service.");
        UsageFetcher usageFetcher = new UsageFetcher(context, true);
        JSONArray usages = usageFetcher.getUsages();

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences sharedUsagePref = context.getSharedPreferences("damo.three.ie.previous_usage",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedUsagePref.edit();
        editor.putLong("last_refreshed_milliseconds", new DateTime().getMillis());
        editor.putString("usage_info", usages.toString());
        editor.commit();

        // Register alarms for newly refreshed usages in background
        boolean notificationsEnabled = sharedPref.getBoolean("notification", true);
        List<UsageItem> usageItems = JSONUtils.jsonToUsageItems(usages);
        List<BasicUsageItem> basicUsageItems = UsageUtils.getAllBasicItems(usageItems);
        UsageUtils.registerInternetExpireAlarm(context, basicUsageItems, notificationsEnabled, true);

    } catch (Exception e) {
        // Try again at 7pm, unless its past 7pm. Then forget and let tomorrow's alarm do the updating.
        // Still trying to decide if I need a more robust retry mechanism.
        Log.d(Constants.TAG, "Caught exception: " + e.getLocalizedMessage());
        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.HOUR_OF_DAY) < Constants.HOUR_TO_RETRY) {
            Log.d(Constants.TAG, "Scheduling a re-try for 7pm");
            calendar.set(Calendar.HOUR_OF_DAY, Constants.HOUR_TO_RETRY);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            Intent receiver = new Intent(context, UpdateReceiver.class);
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            // Using different request code to 0 so it won't conflict with other alarm below.
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 3, receiver,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            // Keeping efficiency in mind:
            // http://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME
            am.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
        }
    } finally {
        Log.d(Constants.TAG, "Finish UpdateService");
        UpdateReceiver.completeWakefulIntent(intent);
    }
}

From source file:de.wikilab.android.friendica01.Max.java

public static void runTimer(Context c) {
    Log.i("Friendica", "try runTimer");
    if (piTimerNotifications != null)
        return;//from  w  w  w  .  j  a  v a 2 s . c  om
    AlarmManager a = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    piTimerNotifications = PendingIntent.getService(c, 1, new Intent(c, NotificationCheckerService.class), 0);
    a.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, AlarmManager.INTERVAL_FIFTEEN_MINUTES,
            piTimerNotifications);
    Toast.makeText(c, "Friendica: Notif. check timer run", Toast.LENGTH_SHORT).show();
    Log.i("Friendica", "done runTimer");
}

From source file:net.texh.cordovapluginstepcounter.StepCounterService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand");

    SharedPreferences sharedPref = getSharedPreferences(CordovaStepCounter.USER_DATA_PREF,
            Context.MODE_PRIVATE);
    Boolean pActive = CordovaStepCounter.getPedometerIsActive(sharedPref);

    //Service should not be activated (pedometer stopped by user)
    if (!pActive) {
        Log.i(TAG,//w  w w .  j a  va  2  s .  com
                "/!\\ onStartCommand Ask to stopSelf, should not be launched ! Should not even be here (maybe 4.4.2 specific bug causes a restart here)");
        stopSelf();
        return START_NOT_STICKY;
    }

    Log.i(TAG, "- Relaunch service in 1 hour (4.4.2 start_sticky bug ) : ");
    Intent newServiceIntent = new Intent(this, StepCounterService.class);
    AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent stepIntent = PendingIntent.getService(getApplicationContext(), 10, newServiceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    //PendingIntent.GetService (ApplicationContext, 10, intent2, PendingIntentFlags.UpdateCurrent);
    aManager.set(AlarmManager.RTC, java.lang.System.currentTimeMillis() + 1000 * 60 * 60, stepIntent);

    if (isRunning /* || has no step sensors */) {
        Log.i(TAG, "Not initialising sensors");
        return Service.START_STICKY;
    }

    Log.i(TAG, "Initialising sensors");
    doInit();

    isRunning = true;
    return Service.START_STICKY;
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void scheduleCMAccountPing(Context context, Intent intent) {
    if (CMAccount.DEBUG)
        Log.d(TAG,//from   w ww .  j a  va 2s .  c  o m
                "Scheduling CMAccount ping, starting = "
                        + new Timestamp(SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY)
                        + " interval (" + AlarmManager.INTERVAL_DAY + ")");
    PendingIntent reRegisterPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY,
            reRegisterPendingIntent);
}

From source file:com.cpd.receivers.LibraryRenewAlarmBroadcastReceiver.java

public void cancelAlarm(Context context) {
    Log.d(TAG, "cancelAlarm() called with: " + "context = [" + context + "]");
    Intent intent = new Intent(context, LibraryRenewAlarmBroadcastReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);/*w  w  w . j av a2 s .  c om*/
}

From source file:com.phonemetra.account.util.AccountUtils.java

public static void scheduleAccountPing(Context context, Intent intent) {
    if (Account.DEBUG)
        Log.d(TAG,//  www  . j av a2s.co m
                "Scheduling Account ping, starting = "
                        + new Timestamp(SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY)
                        + " interval (" + AlarmManager.INTERVAL_DAY + ")");
    PendingIntent reRegisterPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY,
            reRegisterPendingIntent);
}