Example usage for android.app PendingIntent FLAG_NO_CREATE

List of usage examples for android.app PendingIntent FLAG_NO_CREATE

Introduction

In this page you can find the example usage for android.app PendingIntent FLAG_NO_CREATE.

Prototype

int FLAG_NO_CREATE

To view the source code for android.app PendingIntent FLAG_NO_CREATE.

Click Source Link

Document

Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.

Usage

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

public static void updateSyncAlarm(Context context, boolean reCreateAlarm) {
    boolean mIsCalendarSyncEnabled = false;
    boolean mIsKusssSyncEnable = false;
    boolean mIsMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();

    if (mIsMasterSyncEnabled) {
        final Account mAccount = getAccount(context);
        if (mAccount != null) {
            mIsCalendarSyncEnabled = ContentResolver.getSyncAutomatically(mAccount,
                    CalendarContractWrapper.AUTHORITY());
            mIsKusssSyncEnable = ContentResolver.getSyncAutomatically(mAccount, KusssContentContract.AUTHORITY);
        }/*from   w  ww . j a  va  2s  .  c  om*/
    }

    Log.d(TAG, String.format("MasterSync=%b, CalendarSync=%b, KusssSync=%b", mIsMasterSyncEnabled,
            mIsCalendarSyncEnabled, mIsKusssSyncEnable));

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, SyncAlarmService.class);
    intent.putExtra(Consts.ARG_UPDATE_CAL, !mIsCalendarSyncEnabled);
    intent.putExtra(Consts.ARG_UPDATE_KUSSS, !mIsKusssSyncEnable);
    intent.putExtra(Consts.ARG_RECREATE_SYNC_ALARM, true);
    intent.putExtra(Consts.SYNC_SHOW_PROGRESS, true);

    // check if pending intent exists
    reCreateAlarm = reCreateAlarm
            || (PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_NO_CREATE) == null);

    // new pending intent
    PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    if (!mIsMasterSyncEnabled || !mIsCalendarSyncEnabled || !mIsKusssSyncEnable) {
        if (reCreateAlarm) {
            long interval = PreferenceWrapper.getSyncInterval(context) * DateUtils.HOUR_IN_MILLIS;

            // synchronize in half an hour
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis() + AlarmManager.INTERVAL_HALF_HOUR, interval, alarmIntent);
        }
    } else {
        am.cancel(alarmIntent);
    }
}

From source file:com.ubergeek42.WeechatAndroid.service.RelayServiceBackbone.java

@Override
public void onDisconnect() {
    if (DEBUG)/*ww w  . j  a v a  2s  . c o  m*/
        logger.debug("onDisconnect()");
    connection_status = DISCONNECTED;

    // Only do the disconnect handler once
    if (disconnected)
        return;
    disconnected = true;

    Intent intent = new Intent(PingActionReceiver.PING_ACTION);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE);
    alarmMgr.cancel(alarmIntent);

    // automatically attempt reconnection if enabled (and if we aren't shutting down)
    if (mustAutoConnect()) {
        startThreadedConnectLoop(true);
    } else {
        String tmp = getString(R.string.notification_disconnected);
        showNotification(tmp, tmp);
    }

    for (RelayConnectionHandler rch : connectionHandlers)
        rch.onDisconnect();
}

From source file:com.android.deskclock.data.TimerModel.java

/**
 * Updates the notification controlling unexpired timers. This notification is only displayed
 * when the application is not open.// w  ww .j  a  va 2s .  com
 */
void updateNotification() {
    // Notifications should be hidden if the app is open.
    if (mNotificationModel.isApplicationInForeground()) {
        mNotificationManager.cancel(mNotificationModel.getUnexpiredTimerNotificationId());
        return;
    }

    // Filter the timers to just include unexpired ones.
    final List<Timer> unexpired = new ArrayList<>();
    for (Timer timer : getMutableTimers()) {
        if (timer.isRunning() || timer.isPaused()) {
            unexpired.add(timer);
        }
    }

    // If no unexpired timers exist, cancel the notification.
    if (unexpired.isEmpty()) {
        mNotificationManager.cancel(mNotificationModel.getUnexpiredTimerNotificationId());
        return;
    }

    // Sort the unexpired timers to locate the next one scheduled to expire.
    Collections.sort(unexpired, Timer.EXPIRY_COMPARATOR);
    final Timer timer = unexpired.get(0);
    final long remainingTime = timer.getRemainingTime();

    // Generate some descriptive text, a title, and some actions based on timer states.
    final String contentText;
    final String contentTitle;
    @DrawableRes
    int firstActionIconId, secondActionIconId = 0;
    @StringRes
    int firstActionTitleId, secondActionTitleId = 0;
    Intent firstActionIntent, secondActionIntent = null;

    if (unexpired.size() == 1) {
        contentText = formatElapsedTimeUntilExpiry(remainingTime);

        if (timer.isRunning()) {
            // Single timer is running.
            if (TextUtils.isEmpty(timer.getLabel())) {
                contentTitle = mContext.getString(R.string.timer_notification_label);
            } else {
                contentTitle = timer.getLabel();
            }

            firstActionIconId = R.drawable.ic_pause_24dp;
            firstActionTitleId = R.string.timer_pause;
            firstActionIntent = new Intent(mContext, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_add_24dp;
            secondActionTitleId = R.string.timer_plus_1_min;
            secondActionIntent = new Intent(mContext, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        } else {
            // Single timer is paused.
            contentTitle = mContext.getString(R.string.timer_paused);

            firstActionIconId = R.drawable.ic_start_24dp;
            firstActionTitleId = R.string.sw_resume_button;
            firstActionIntent = new Intent(mContext, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_reset_24dp;
            secondActionTitleId = R.string.sw_reset_button;
            secondActionIntent = new Intent(mContext, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        }
    } else {
        if (timer.isRunning()) {
            // At least one timer is running.
            final String timeRemaining = formatElapsedTimeUntilExpiry(remainingTime);
            contentText = mContext.getString(R.string.next_timer_notif, timeRemaining);
            contentTitle = mContext.getString(R.string.timers_in_use, unexpired.size());
        } else {
            // All timers are paused.
            contentText = mContext.getString(R.string.all_timers_stopped_notif);
            contentTitle = mContext.getString(R.string.timers_stopped, unexpired.size());
        }

        firstActionIconId = R.drawable.ic_reset_24dp;
        firstActionTitleId = R.string.timer_reset_all;
        firstActionIntent = TimerService.createResetUnexpiredTimersIntent(mContext);
    }

    // Intent to load the app and show the timer when the notification is tapped.
    final Intent showApp = new Intent(mContext, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
            .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(mContext, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext).setOngoing(true)
            .setLocalOnly(true).setShowWhen(false).setAutoCancel(false).setContentText(contentText)
            .setContentTitle(contentTitle).setContentIntent(pendingShowApp)
            .setSmallIcon(R.drawable.stat_notify_timer).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    final PendingIntent firstAction = PendingIntent.getService(mContext, 0, firstActionIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    final String firstActionTitle = mContext.getString(firstActionTitleId);
    builder.addAction(firstActionIconId, firstActionTitle, firstAction);

    if (secondActionIntent != null) {
        final PendingIntent secondAction = PendingIntent.getService(mContext, 0, secondActionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        final String secondActionTitle = mContext.getString(secondActionTitleId);
        builder.addAction(secondActionIconId, secondActionTitle, secondAction);
    }

    // Update the notification.
    final Notification notification = builder.build();
    final int notificationId = mNotificationModel.getUnexpiredTimerNotificationId();
    mNotificationManager.notify(notificationId, notification);

    final Intent updateNotification = TimerService.createUpdateNotificationIntent(mContext);
    if (timer.isRunning() && remainingTime > MINUTE_IN_MILLIS) {
        // Schedule a callback to update the time-sensitive information of the running timer.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final long nextMinuteChange = remainingTime % MINUTE_IN_MILLIS;
        final long triggerTime = SystemClock.elapsedRealtime() + nextMinuteChange;

        schedulePendingIntent(triggerTime, pi);
    } else {
        // Cancel the update notification callback.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            mAlarmManager.cancel(pi);
            pi.cancel();
        }
    }
}

From source file:org.ohmage.reminders.types.location.LocTrigService.java

private void cancelSamplingAlarm(String action) {
    Intent i = new Intent(action);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_NO_CREATE);
    if (pi != null) {
        AlarmManager alarmMan = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmMan.cancel(pi);//from w  w w.  j  a  v  a 2  s  . c  om
        pi.cancel();
    }
}