Example usage for android.app PendingIntent getService

List of usage examples for android.app PendingIntent getService

Introduction

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

Prototype

public static PendingIntent getService(Context context, int requestCode, @NonNull Intent intent,
        @Flags int flags) 

Source Link

Document

Retrieve a PendingIntent that will start a service, like calling Context#startService Context.startService() .

Usage

From source file:com.zion.music.NotificationHelper.java

/**
 * Returns a {@link android.app.PendingIntent} for the giver {@link com.zion.music.MediaPlayerService} {@code action}
 * @param action the action to perform in the {@code service}
 * @return the {@code PendingIntent} associated to the {@code action}
 *//*  w w w  .j  a va2 s  . c  o  m*/
private PendingIntent retreivePlaybackActions(int action) {
    Intent intent = new Intent(this.service, MediaPlayerService.class);
    PendingIntent pendingIntent;
    switch (action) {
    case NotificationHelper.ACTION_TOGGLE_PLAY_PAUSE:
        intent.setAction(MediaPlayerService.ACTION_TOGGLE_PLAY_PAUSE);
        break;
    case NotificationHelper.ACTION_NEXT_TRACK:
        intent.setAction(MediaPlayerService.ACTION_NEXT_TRACK);
        break;
    case NotificationHelper.ACTION_PREVIOUS_TRACK:
        intent.setAction(MediaPlayerService.ACTION_PREVIOUS_TRACK);
        break;
    case NotificationHelper.ACTION_CLOSE_NOTIFICATION:
        intent.setAction(MediaPlayerService.ACTION_STOP);
        break;
    default:
        return null;
    }
    pendingIntent = PendingIntent.getService(this.service, action, intent, 0);
    return pendingIntent;
}

From source file:com.example.activitydemo.app.service.GameService.java

public void registerActivityUpdates() {
    if (mRecognitionPendingIntent == null) {
        mRecognitionPendingIntent = PendingIntent.getService(this, 0,
                new Intent(this, RecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    }//from w w  w .j  av a  2  s.com
    ActivityRecognition.ActivityRecognitionApi
            .requestActivityUpdates(mClient, ACTIVITY_INTERVAL, mRecognitionPendingIntent)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        Log.d(TAG, "Activities coming!");
                    } else {
                        Log.e(TAG, "No activity coming!");
                    }
                }
            });
}

From source file:com.teocci.utubinbg.BackgroundAudioService.java

/**
 * Builds notification panel with buttons and info on it
 *
 * @param action Action to be applied/*from  w  ww .j  av  a  2  s . co m*/
 */

private void buildNotification(NotificationCompat.Action action) {

    final NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();

    Intent intent = new Intent(getApplicationContext(), BackgroundAudioService.class);
    intent.setAction(ACTION_STOP);
    PendingIntent stopPendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);

    Intent clickIntent = new Intent(this, MainActivity.class);
    clickIntent.setAction(Intent.ACTION_MAIN);
    clickIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent clickPendingIntent = PendingIntent.getActivity(this, 0, clickIntent, 0);

    builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.utubinbg_icon);
    builder.setContentTitle(videoItem.getTitle());
    builder.setContentInfo(videoItem.getDuration());
    builder.setShowWhen(false);
    builder.setContentIntent(clickPendingIntent);
    builder.setDeleteIntent(stopPendingIntent);
    builder.setOngoing(false);
    builder.setSubText(videoItem.getViewCount());
    builder.setStyle(style);

    //load bitmap for largeScreen
    if (videoItem.getThumbnailURL() != null && !videoItem.getThumbnailURL().isEmpty()) {
        Picasso.with(this).load(videoItem.getThumbnailURL()).into(target);
    }

    builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS));
    builder.addAction(action);
    builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT));
    style.setShowActionsInCompactView(0, 1, 2);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

}

From source file:com.google.android.gms.nearby.messages.samples.hellobeacons.MainActivity.java

private PendingIntent getPendingIntent() {
    return PendingIntent.getService(this, 0, getBackgroundSubscribeServiceIntent(),
            PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.granita.tasks.notification.NotificationActionIntentService.java

private static PendingIntent getCompleteActionIntent(Context context, int notificationId, Uri taskUri) {
    final Intent intent = new Intent(NotificationActionIntentService.ACTION_COMPLETE);

    intent.setPackage(context.getPackageName());
    intent.setData(taskUri);//  w w w.  ja  v  a 2s .c  o m
    intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
    final PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE_COMPLETE, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    return pendingIntent;
}

From source file:com.android.python27.ScriptService.java

@Override
protected Notification createNotification() {
    Notification notification = new Notification(R.drawable.icon, this.getString(R.string.loading),
            System.currentTimeMillis());
    // This contentIntent is a noop.
    PendingIntent contentIntent = PendingIntent.getService(this, 0, new Intent(), 0);
    notification.setLatestEventInfo(this, this.getString(R.string.app_name), this.getString(R.string.loading),
            contentIntent);// w  w w  .ja  v  a  2  s.co  m
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    return notification;
}

From source file:com.example.android.wearable.wear.wearnotifications.StandaloneMainActivity.java

private void generateBigTextStyleNotification() {

    Log.d(TAG, "generateBigTextStyleNotification()");

    // Main steps for building a BIG_TEXT_STYLE notification:
    //      0. Get your data
    //      1. Build the BIG_TEXT_STYLE
    //      2. Set up main Intent for notification
    //      3. Create additional Actions for the Notification
    //      4. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.BigTextStyleReminderAppData bigTextStyleReminderAppData = MockDatabase.getBigTextStyleData();

    // 1. Build the BIG_TEXT_STYLE
    BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
            // Overrides ContentText in the big form of the template
            .bigText(bigTextStyleReminderAppData.getBigText())
            // Overrides ContentTitle in the big form of the template
            .setBigContentTitle(bigTextStyleReminderAppData.getBigContentTitle())
            // Summary line after the detail section in the big form of the template
            // Note: To improve readability, don't overload the user with info. If Summary Text
            // doesn't add critical information, you should skip it.
            .setSummaryText(bigTextStyleReminderAppData.getSummaryText());

    // 2. Set up main Intent for notification
    Intent mainIntent = new Intent(this, BigTextMainActivity.class);

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 3. Create additional Actions (Intents) for the Notification

    // In our case, we create two additional actions: a Snooze action and a Dismiss action.

    // Snooze Action
    Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
    snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);

    PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
    NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_alarm_white_48dp, "Snooze", snoozePendingIntent).build();

    // Dismiss Action
    Intent dismissIntent = new Intent(this, BigTextIntentService.class);
    dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);

    PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
    NotificationCompat.Action dismissAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_cancel_white_48dp, "Dismiss", dismissPendingIntent).build();

    // 4. Build and issue the notification

    // Because we want this to be a new notification (not updating a previous notification), we
    // create a new Builder. Later, we use the same global builder to get back the notification
    // we built here for the snooze action, that is, canceling the notification and relaunching
    // it several seconds later.

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());/*from   www .j a va 2s  .  c  o  m*/

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    notificationCompatBuilder
            // BIG_TEXT_STYLE sets title and content
            .setStyle(bigTextStyle).setContentTitle(bigTextStyleReminderAppData.getContentTitle())
            .setContentText(bigTextStyleReminderAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_alarm_white_48dp))
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

            .setCategory(Notification.CATEGORY_REMINDER).setPriority(Notification.PRIORITY_HIGH)

            // Shows content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PUBLIC)

            // Adds additional actions specified above
            .addAction(snoozeAction).addAction(dismissAction);

    /* REPLICATE_NOTIFICATION_STYLE_CODE:
     * You can replicate Notification Style functionality on Wear 2.0 (24+) by not setting the
     * main content intent, that is, skipping the call setContentIntent(). However, you need to
     * still allow the user to open the native Wear app from the Notification itself, so you
     * add an action to launch the app.
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        // Enables launching app in Wear 2.0 while keeping the old Notification Style behavior.
        NotificationCompat.Action mainAction = new NotificationCompat.Action.Builder(R.drawable.ic_launcher,
                "Open", mainPendingIntent).build();

        notificationCompatBuilder.addAction(mainAction);

    } else {
        // Wear 1.+ still functions the same, so we set the main content intent.
        notificationCompatBuilder.setContentIntent(mainPendingIntent);
    }

    Notification notification = notificationCompatBuilder.build();

    mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);

    // Close app to demonstrate notification in steam.
    finish();
}

From source file:com.uphyca.idobata.android.service.IdobataService.java

private PendingIntent buildPendingStartServiceIntent() {
    final Intent intent = new Intent(IdobataService.this, IdobataService.class);
    return PendingIntent.getService(IdobataService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService.java

/**
 * Returns the {@link NotificationCompat} used as part of the foreground service.
 *//*from  w ww  . java2  s.c  o  m*/
private Notification getNotification() {
    Intent intent = new Intent(this, LocationUpdatesService.class);

    CharSequence text = Utils.getLocationText(mLocation);

    // Extra to help us figure out if we arrived in onStartCommand via the notification or not.
    intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);

    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // The PendingIntent to launch activity.
    PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    return new NotificationCompat.Builder(this)
            .addAction(R.drawable.ic_launch, getString(R.string.launch_activity), activityPendingIntent)
            .addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates), servicePendingIntent)
            .setContentText(text).setContentTitle(Utils.getLocationTitle(this)).setOngoing(true)
            .setPriority(Notification.PRIORITY_HIGH).setSmallIcon(R.mipmap.ic_launcher).setTicker(text)
            .setWhen(System.currentTimeMillis()).build();
}

From source file:ca.zadrox.dota2esportticker.service.UpdateMatchService.java

private void scheduleAutoUpdates() {

    if (!PrefUtils.shouldAutoUpdate(this)) {
        return;//w w w. ja va2s  .  co  m
    }

    final long currentTime = TimeUtils.getUTCTime();

    final Intent updateMatchIntent = new Intent(UpdateMatchService.ACTION_AUTO_UPDATE_MATCHES, null, this,
            UpdateMatchService.class);

    final Intent updateResultIntent = new Intent(UpdateMatchService.ACTION_UPDATE_RESULTS, null, this,
            UpdateMatchService.class);

    final long matchUpdateTime = currentTime + (60000 * 60 * 12);
    final long resultUpdateTime = currentTime + (60000 * 60);

    PendingIntent umi = PendingIntent.getService(this.getApplicationContext(), 0, updateMatchIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent uri = PendingIntent.getService(this.getApplicationContext(), 0, updateResultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    am.cancel(umi);
    am.cancel(uri);

    am.setInexactRepeating(AlarmManager.RTC, matchUpdateTime, AlarmManager.INTERVAL_HALF_DAY, umi);

    am.setInexactRepeating(AlarmManager.RTC, resultUpdateTime, AlarmManager.INTERVAL_HOUR, uri);
}