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.mishiranu.dashchan.content.service.AudioPlayerService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void refreshPlaybackNotification(boolean recreate) {
    Notification.Builder builder = this.builder;
    if (builder == null || recreate) {
        builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_audiotrack_white_24dp);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(this, AudioPlayerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
        TypedArray typedArray = context.obtainStyledAttributes(ICON_ATTRS);
        PendingIntent toggleIntent = PendingIntent.getService(context, 0, obtainIntent(this, ACTION_TOGGLE),
                PendingIntent.FLAG_UPDATE_CURRENT);
        boolean playing = mediaPlayer.isPlaying();
        ViewUtils.addNotificationAction(builder, context, typedArray, playing ? 3 : 2,
                playing ? R.string.action_pause : R.string.action_play, toggleIntent);
        PendingIntent cancelIntent = PendingIntent.getService(context, 0, obtainIntent(this, ACTION_CANCEL),
                PendingIntent.FLAG_UPDATE_CURRENT);
        ViewUtils.addNotificationAction(builder, context, typedArray, 1, R.string.action_stop, cancelIntent);
        typedArray.recycle();//  w  ww.j a v  a 2s.c  om
        if (C.API_LOLLIPOP) {
            builder.setColor(notificationColor);
        }
        this.builder = builder;
        builder.setContentTitle(getString(R.string.message_file_playback));
        builder.setContentText(getString(R.string.message_download_name_format, fileName));
    }
    startForeground(C.NOTIFICATION_ID_AUDIO_PLAYER, builder.build());
}

From source file:com.licenta.android.licenseapp.location.MapFragment.java

private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    //        if (mGeofencePendingIntent != null) {
    //            return mGeofencePendingIntent;
    //        }// w w  w.j  a  v a 2s .  co  m
    Intent intent = new Intent(getActivity(), GeofenceTransitionsService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
    // calling addGeofences() and removeGeofences().
    return PendingIntent.getService(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.nextgis.maplibui.service.TrackerService.java

private void addNotification() {
    String name = "";

    String selection = TrackLayer.FIELD_ID + " = ?";
    Cursor currentTrack = getContentResolver().query(mContentUriTracks, new String[] { TrackLayer.FIELD_NAME },
            selection, new String[] { mTrackId }, null);
    if (null != currentTrack) {
        if (currentTrack.moveToFirst())
            name = currentTrack.getString(0);
        currentTrack.close();/*w w w .ja  v a 2 s. c  o  m*/
    }

    String title = String.format(getString(R.string.tracks_title), name);
    Bitmap largeIcon = NotificationHelper.getLargeIcon(R.drawable.ic_action_maps_directions_walk,
            getResources());

    Intent intentStop = new Intent(this, TrackerService.class);
    intentStop.setAction(ACTION_STOP);
    PendingIntent stopService = PendingIntent.getService(this, 0, intentStop,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContentIntent(mOpenActivity).setSmallIcon(mSmallIcon).setLargeIcon(largeIcon).setTicker(mTicker)
            .setWhen(System.currentTimeMillis()).setAutoCancel(false).setContentTitle(title)
            .setContentText(mTicker).setOngoing(true);

    builder.addAction(R.drawable.ic_location, getString(R.string.tracks_open), mOpenActivity);
    builder.addAction(R.drawable.ic_action_cancel_dark, getString(R.string.tracks_stop), stopService);

    mNotificationManager.notify(TRACK_NOTIFICATION_ID, builder.build());
    startForeground(TRACK_NOTIFICATION_ID, builder.build());

    Toast.makeText(this, title, Toast.LENGTH_SHORT).show();
}

From source file:com.orpheusdroid.screenrecorder.RecorderService.java

private NotificationCompat.Builder createNotification(NotificationCompat.Action action) {
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    Intent recordStopIntent = new Intent(this, RecorderService.class);
    recordStopIntent.setAction(Const.SCREEN_RECORDING_STOP);
    PendingIntent precordStopIntent = PendingIntent.getService(this, 0, recordStopIntent, 0);

    Intent UIIntent = new Intent(this, MainActivity.class);
    PendingIntent notificationContentIntent = PendingIntent.getActivity(this, 0, UIIntent, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setContentTitle(getResources().getString(R.string.screen_recording_notification_title))
            .setTicker(getResources().getString(R.string.screen_recording_notification_title))
            .setSmallIcon(R.drawable.ic_notification)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setUsesChronometer(true)
            .setOngoing(true).setContentIntent(notificationContentIntent).setPriority(Notification.PRIORITY_MAX)
            .addAction(R.drawable.ic_notification_stop,
                    getResources().getString(R.string.screen_recording_notification_action_stop),
                    precordStopIntent);/*from   www  .j a v a 2  s  .  c o m*/
    if (action != null)
        notification.addAction(action);
    return notification;
}

From source file:com.somexapps.wyre.services.MediaService.java

private void buildNotification(android.support.v4.app.NotificationCompat.Action action) {
    // Initialize style
    NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();

    // Build intent/pending intent
    Intent intent = new Intent(getApplicationContext(), MediaService.class);
    intent.setAction(ACTION_STOP);/*from ww w . j av  a2s .  c o  m*/
    // TODO: Should this have 0 as the flag?
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
            MEDIA_NOTIFICATION_REQUEST_CODE, intent, 0);

    // Get notification manager
    final NotificationManagerCompat managerCompat = NotificationManagerCompat.from(getApplicationContext());

    // Create media notification
    final android.support.v4.app.NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_album_white_48dp))
            .setContentTitle(playingSong.getTitle()).setContentText(playingSong.getArtist())
            .setDeleteIntent(pendingIntent).setStyle(style);

    // Check to see if we have a next or previous song to play
    boolean previousDisabled = false;
    boolean nextDisabled = false;

    if (songsList.size() == 1) {
        previousDisabled = true;
        nextDisabled = true;
    } else if (playingSongIndex == 0) {
        previousDisabled = true;
    } else if (playingSongIndex == songsList.size() - 1) {
        nextDisabled = true;
    }

    // Add previous action if there is previous media
    if (!previousDisabled) {
        builder.addAction(generateAction(R.drawable.ic_skip_previous_white_36dp,
                getString(R.string.media_service_notification_prev), ACTION_PREVIOUS));
    }

    // Add play/pause action
    builder.addAction(action);

    // Add next action if there is next media
    if (!nextDisabled) {
        builder.addAction(generateAction(R.drawable.ic_skip_next_white_36dp,
                getString(R.string.media_service_notification_next), ACTION_NEXT));
    }

    // Show buttons based on state
    if (previousDisabled && nextDisabled) {
        style.setShowActionsInCompactView(0);
    } else if (previousDisabled || nextDisabled) {
        style.setShowActionsInCompactView(0, 1);
    } else {
        style.setShowActionsInCompactView(0, 1, 2);
    }

    /**
     * Check if we are going to a play state by checking to see if the middle
     * action is getting set to Pause
     */
    if (action.getTitle().equals(getString(R.string.media_service_notification_pause))) {
        builder.setOngoing(true);
    } else {
        builder.setOngoing(false);
    }

    // Check if we have a image path
    if (playingSong.getAlbumArtPath() != null && playingSong.getAlbumArtPath().startsWith("http")) {
        // Set up image target
        Target iconTarget = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                // Set icon
                builder.setLargeIcon(bitmap);

                // Update notification
                managerCompat.notify(MEDIA_NOTIFICATION_REQUEST_CODE, builder.build());
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                // do nothing
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                // do nothing
            }
        };

        // Start the image load
        Picasso.with(getApplicationContext()).load(Uri.parse(playingSong.getAlbumArtPath())).into(iconTarget);
    }

    // Update the notification
    managerCompat.notify(MEDIA_NOTIFICATION_REQUEST_CODE, builder.build());
}

From source file:com.jjcamera.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from w w w.jav a 2s  . com
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.jjcamera.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.google.android.gms.location.sample.activityrecognition.MainActivity.java

/**
 * Gets a PendingIntent to be sent for each activity detection.
 *//*  w w w. jav a 2  s. c  o m*/
private PendingIntent getActivityDetectionPendingIntent() {
    Intent intent = new Intent(this, DetectedActivitiesIntentService.class);

    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // requestActivityUpdates() and removeActivityUpdates().
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.janacare.walkmeter.MainActivity.java

/**
 * Respond to "Start" button by requesting activity recognition
 * updates./*from  w  ww . j  a  v a2s . c  o m*/
 * @param view The view that triggered this method.
 */
public void onStopUpdates(View view) {

    // Check for Google Play services
    if (!servicesConnected()) {

        return;
    }

    /*
     * Set the request type. If a connection error occurs, and Google Play services can
     * handle it, then onActivityResult will use the request type to retry the request
     */
    mRequestType = ActivityUtils.REQUEST_TYPE.REMOVE;

    PendingIntent pendingIntent = mDetectionRequester.getRequestPendingIntent();

    if (pendingIntent == null) {
        Intent intent = new Intent(getApplicationContext(), ActivityRecognitionIntentService.class);
        pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

    if (pendingIntent != null) {
        // Pass the remove request to the remover object
        mDetectionRemover.removeUpdates(pendingIntent);

        /*
         * Cancel the PendingIntent. Even if the removal request fails, canceling the PendingIntent
         * will stop the updates.
         */
        pendingIntent.cancel();
    }

    // TODO: Update toggle button to "Start pedometer"
}

From source file:com.meetingcpp.sched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);/*  w  ww.  j a v  a 2s.co  m*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(
            new Uri.Builder().authority("com.meetingcpp.sched").path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.ncode.android.apps.schedo.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);/*from  w  ww  .  j  a  v a2s  .c om*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.ncode.android.apps.schedo")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}