Example usage for android.app AlarmManager RTC

List of usage examples for android.app AlarmManager RTC

Introduction

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

Prototype

int RTC

To view the source code for android.app AlarmManager RTC.

Click Source Link

Document

Alarm time in System#currentTimeMillis System.currentTimeMillis() (wall clock time in UTC).

Usage

From source file:com.svpino.longhorn.activities.DashboardActivity.java

@Override
protected void onStart() {
    super.onStart();

    DataProvider.startStockQuoteCollectorService(this, null);

    ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setInexactRepeating(AlarmManager.RTC,
            System.currentTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
            Extensions.createPendingIntent(this, Constants.SCHEDULE_AUTOMATIC));
}

From source file:com.nextgis.firereporter.ReporterService.java

public void ScheduleNextUpdate(Context context, long nMinTimeBetweenSend, boolean bEnergyEconomy) {
    if (context == null)
        return;//from ww  w .  j a v a2 s.  c  o  m

    Intent intent = new Intent(ReporterService.ACTION_START);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // The update frequency should often be user configurable.  This is not.

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + nMinTimeBetweenSend;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    if (bEnergyEconomy)
        alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
    else
        alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pendingIntent);
}

From source file:org.zywx.wbpalmstar.engine.universalex.EUExWidgetOne.java

public void reStartApp() {
    Intent oldData = ((Activity) mContext).getIntent();
    Intent intent = new Intent(oldData);
    int flag = ((Activity) mContext).getIntent().getFlags();
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, flag);
    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    long delay = System.currentTimeMillis() + 1000L;
    alarmManager.set(AlarmManager.RTC, delay, pendingIntent);
    ((EBrowserActivity) mContext).exitApp(false);
    ;//from w  w w.  j  a v a 2  s .  co  m
}

From source file:com.svpino.longhorn.MarketCollectorService.java

@Override
protected void onHandleIntent(Intent intent) {
    SharedPreferences sharedPreferences = getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE);
    long lastUpdate = sharedPreferences.getLong(Constants.PREFERENCE_COLLECTOR_LAST_UPDATE, 0);
    boolean retrying = sharedPreferences.getBoolean(Constants.PREFERENCE_COLLECTOR_RETRYING, false);
    int retries = sharedPreferences.getInt(Constants.PREFERENCE_COLLECTOR_RETRIES, 0);
    boolean wereWeWaitingForConnectivity = sharedPreferences
            .getBoolean(Constants.PREFERENCE_STATUS_WAITING_FOR_CONNECTIVITY, false);

    boolean isGlobalCollection = intent.getExtras() == null
            || (intent.getExtras() != null && !intent.getExtras().containsKey(EXTRA_TICKER));

    if (wereWeWaitingForConnectivity) {
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(Constants.PREFERENCE_STATUS_WAITING_FOR_CONNECTIVITY, false);
        editor.commit();/*from  w  w  w.j a v a  2 s.  c  om*/
    }

    if (retrying && isGlobalCollection) {
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(Constants.PREFERENCE_COLLECTOR_RETRYING, false);
        editor.putInt(Constants.PREFERENCE_COLLECTOR_RETRIES, 0);
        editor.commit();

        ((AlarmManager) getSystemService(Context.ALARM_SERVICE))
                .cancel(Extensions.createPendingIntent(this, Constants.SCHEDULE_RETRY));
    }

    long currentTime = System.currentTimeMillis();
    if (retrying || wereWeWaitingForConnectivity || !isGlobalCollection
            || (isGlobalCollection && currentTime - lastUpdate > Constants.COLLECTOR_MIN_REFRESH_INTERVAL)) {
        String[] tickers = null;

        if (isGlobalCollection) {
            Log.d(LOG_TAG, "Executing global market information collection...");
            tickers = DataProvider.getStockDataTickers(this);
        } else {
            String ticker = intent.getExtras().containsKey(EXTRA_TICKER)
                    ? intent.getExtras().getString(EXTRA_TICKER)
                    : null;

            Log.d(LOG_TAG, "Executing market information collection for ticker " + ticker + ".");

            tickers = new String[] { ticker };
        }

        try {
            collect(tickers);

            if (isGlobalCollection) {
                Editor editor = sharedPreferences.edit();
                editor.putLong(Constants.PREFERENCE_COLLECTOR_LAST_UPDATE, System.currentTimeMillis());
                editor.commit();
            }

            DataProvider.notifyDataCollectionIsFinished(this, tickers);

            Log.d(LOG_TAG, "Market information collection was successfully completed");
        } catch (Exception e) {
            Log.e(LOG_TAG, "Market information collection failed.", e);

            if (Extensions.areWeOnline(this)) {
                Log.d(LOG_TAG, "Scheduling an alarm for retrying a global market information collection...");

                retries++;

                Editor editor = sharedPreferences.edit();
                editor.putBoolean(Constants.PREFERENCE_COLLECTOR_RETRYING, true);
                editor.putInt(Constants.PREFERENCE_COLLECTOR_RETRIES, retries);
                editor.commit();

                long interval = Constants.COLLECTOR_MIN_RETRY_INTERVAL * retries;
                if (interval > Constants.COLLECTOR_MAX_REFRESH_INTERVAL) {
                    interval = Constants.COLLECTOR_MAX_REFRESH_INTERVAL;
                }

                ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).set(AlarmManager.RTC,
                        System.currentTimeMillis() + interval,
                        Extensions.createPendingIntent(this, Constants.SCHEDULE_RETRY));
            } else {
                Log.d(LOG_TAG,
                        "It appears that we are not online, so let's start listening for connectivity updates.");

                Editor editor = sharedPreferences.edit();
                editor.putBoolean(Constants.PREFERENCE_STATUS_WAITING_FOR_CONNECTIVITY, true);
                editor.commit();

                ComponentName componentName = new ComponentName(this, ConnectivityBroadcastReceiver.class);
                getPackageManager().setComponentEnabledSetting(componentName,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
            }
        }
    } else if (isGlobalCollection && currentTime - lastUpdate <= Constants.COLLECTOR_MIN_REFRESH_INTERVAL) {
        Log.d(LOG_TAG, "Global market information collection will be skipped since it was performed less than "
                + (Constants.COLLECTOR_MIN_REFRESH_INTERVAL / 60 / 1000) + " minutes ago.");
    }

    stopSelf();
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

@CalledByNative
private static boolean showNotification(int category, String idWithinCategory, String url, String title,
        String text, Bitmap image, long timeoutAtMillis) {
    if (findActiveNotification(category, idWithinCategory) != null)
        return false;

    // Post notification.
    Context context = ContextUtils.getApplicationContext();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int nextId = nextNotificationId();
    Uri uri = Uri.parse(url);// ww w . j  av  a2 s . c om
    Intent contentIntent = new Intent(context, OpenUrlReceiver.class).setData(uri)
            .putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
            .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
    Intent deleteIntent = new Intent(context, DeleteReceiver.class).setData(uri)
            .putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
            .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setAutoCancel(true)
            .setContentIntent(PendingIntent.getBroadcast(context, 0, contentIntent, 0))
            .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setContentTitle(title)
            .setContentText(text).setGroup(NOTIFICATION_TAG).setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setPriority(-1).setLargeIcon(image).setSmallIcon(R.drawable.ic_chrome);
    manager.notify(NOTIFICATION_TAG, nextId, builder.build());
    addActiveNotification(new ActiveNotification(nextId, category, idWithinCategory, uri));

    // Set timeout.
    if (timeoutAtMillis != Long.MAX_VALUE) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent timeoutIntent = new Intent(context, TimeoutReceiver.class).setData(Uri.parse(url))
                .putExtra(NOTIFICATION_ID_EXTRA, nextId).putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
                .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
        alarmManager.set(AlarmManager.RTC, timeoutAtMillis,
                PendingIntent.getBroadcast(context, 0, timeoutIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
    return true;
}

From source file:com.senior.javnav.MainActivity.java

@Override
protected void onStop() {
    //Start the service in a timely interval
    SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
    if (shared.getBoolean("notif", true) && shared.getBoolean("notifiCancelled", true)) {
        //one second * 60 seconds in a minute * 5
        int fiveMinutes = 1000 * 60 * 5;

        //Start the alarm manager service
        SharedPreferences.Editor edit = shared.edit();
        Intent service = new Intent(getApplicationContext(), BroadcastNews.class);
        PendingIntent pendingService = PendingIntent.getBroadcast(getApplicationContext(), 0, service, 0);
        AlarmManager newsUpdate = (AlarmManager) getSystemService(ALARM_SERVICE);

        //Check for the update every 5 minutes
        newsUpdate.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), fiveMinutes, pendingService);
        edit.putBoolean("notifiCancelled", false).commit();
        Log.i("Main", "Alarm set " + shared.getBoolean("notifiCancelled", true));
    }/*from   www.  j  ava  2s  .co  m*/

    super.onStop();
}

From source file:org.opensilk.fuzzyclock.FuzzyWidgetService.java

/**
 * Schedules next update with alarmManager
 * @param now//from  w ww  .j a  va 2 s. com
 * @param nextMilli
 * @param id
 */
@DebugLog
private void scheduleUpdate(long now, long nextMilli, int id) {
    PendingIntent pendingIntent = createPendingIntent(id);
    cancelUpdate(pendingIntent);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mAlarmManager.setExact(AlarmManager.RTC, now + nextMilli, pendingIntent);
    } else {
        mAlarmManager.set(AlarmManager.RTC, now + nextMilli, pendingIntent);
    }
    if (LOGV)
        Log.i(TAG, "Scheduled update for " + (nextMilli / 1000) + "s from now for widget " + id);
}

From source file:com.google.android.apps.santatracker.notification.SantaNotificationBuilder.java

/**
 * Schedule a basic notification at an approximate time.
 *///from  w  w  w.  j a v a 2 s . com
public static void ScheduleSantaNotification(Context c, long timestamp, int notificationType) {

    // Only schedule a notification if the time is in the future
    if (timestamp < System.currentTimeMillis()) {
        return;
    }

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

    Intent i = new Intent(c, NotificationBroadcastReceiver.class);
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_ID, NotificationConstants.NOTIFICATION_ID);

    // Type is "takeoff", "location", etc.
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, notificationType);

    // Generate unique pending intent
    PendingIntent pi = PendingIntent.getBroadcast(c, notificationType, i, 0);

    // Deliver next time the device is woken up
    alarm.set(AlarmManager.RTC, timestamp, pi);

}

From source file:pl.zajecia.cw3.MainService.java

/**
 * Sets the next alarm, based on wakelock options and interval value.
 *///w w  w  . j a  va 2  s.c o m
private void setNextAlarm() {
    Intent intent = new Intent(INTENT_ALARM);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    int addType;
    switch (mIntervalType) {
    case MainActivity.INTERVAL_SECONDS:
        addType = Calendar.SECOND;
        break;
    case MainActivity.INTERVAL_MINUTES:
        addType = Calendar.MINUTE;
        break;
    case MainActivity.INTERVAL_HOURS:
        addType = Calendar.HOUR;
        break;
    case MainActivity.INTERVAL_DAYS:
        addType = Calendar.DAY_OF_YEAR;
        break;
    default:
        addType = MainActivity.INTERVAL_HOURS;
    }

    Calendar time = Calendar.getInstance();
    time.setTimeInMillis(System.currentTimeMillis());
    time.add(addType, mIntervalValue);

    if (mChangeDuringSleep) {
        mAlarmM.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
    } else {
        mAlarmM.set(AlarmManager.RTC, time.getTimeInMillis(), pendingIntent);
    }
}