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.brayanarias.alarmproject.receiver.AlarmReceiver.java

private static void setAlarmCumple(Context context) {
    PendingIntent pendingIntent = null;/* w w w  .  j a  va2s  .co  m*/
    Intent intent = new Intent(context, AlarmService.class);
    intent.putExtra(Constant.alarmIdKey, 2809);
    intent.putExtra(Constant.alarmNameKey, "Feliz cumple mamita");
    pendingIntent = PendingIntent.getService(context, 2809, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = getAlarmManager(context);
    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.MONTH, 8);
    actual.set(Calendar.DAY_OF_MONTH, 28);
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);
    actual.set(Calendar.HOUR, 5);
    actual.set(Calendar.AM_PM, Calendar.AM);
    actual.set(Calendar.MINUTE, 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(actual.getTimeInMillis(),
                pendingIntent);
        alarmManager.setAlarmClock(clockInfo, pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, actual.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, actual.getTimeInMillis(), pendingIntent);
    }
}

From source file:com.manning.androidhacks.hack046.helper.NotificationHelper.java

private static PendingIntent getDeletePendingIntent(Context ctx) {
    Intent intent = new Intent(ctx, MsgService.class);
    intent.setAction(MsgService.MSG_DELETE);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return PendingIntent.getService(ctx, 0, intent, 0);
}

From source file:com.truiton.foregroundservice.ForegroundService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Start Foreground Intent ");
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Intent previousIntent = new Intent(this, ForegroundService.class);
        previousIntent.setAction(Constants.ACTION.PREV_ACTION);
        PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);

        Intent playIntent = new Intent(this, ForegroundService.class);
        playIntent.setAction(Constants.ACTION.PLAY_ACTION);
        PendingIntent pplayIntent = PendingIntent.getService(this, 0, playIntent, 0);

        Intent nextIntent = new Intent(this, ForegroundService.class);
        nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
        PendingIntent pnextIntent = PendingIntent.getService(this, 0, nextIntent, 0);

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.truiton_short);

        Notification notification = new NotificationCompat.Builder(this).setContentTitle("Truiton Music Player")
                .setTicker("Truiton Music Player").setContentText("My Music")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pendingIntent)
                .setOngoing(true).addAction(android.R.drawable.ic_media_previous, "Previous", ppreviousIntent)
                .addAction(android.R.drawable.ic_media_play, "Play", pplayIntent)
                .addAction(android.R.drawable.ic_media_next, "Next", pnextIntent).build();
        startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
    } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
        Log.i(LOG_TAG, "Clicked Previous");
    } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
        Log.i(LOG_TAG, "Clicked Play");
    } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
        Log.i(LOG_TAG, "Clicked Next");
    } else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");
        stopForeground(true);// ww  w.  j a v  a2s . c  o  m
        stopSelf();
    }
    return START_STICKY;
}

From source file:com.bluros.music.widgets.SleepModeDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    context = getActivity().getBaseContext();
    Intent action = new Intent(MusicService.SLEEP_MODE_STOP_ACTION);
    ComponentName serviceName = new ComponentName(context, MusicService.class);
    action.setComponent(serviceName);/*from w  ww  .j a  v  a  2  s.  com*/
    pendingIntent = PendingIntent.getService(context, 4, action, 0);
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.sleep_mode_time_selector, null);
    int minutes = Integer.valueOf(getString(R.string.default_interval));
    final TextView tvPopUpTime = (TextView) view.findViewById(R.id.pop_up_time);
    tvPopUpTime.setText(String.valueOf(minutes));
    final SeekBar sBar = (SeekBar) view.findViewById(R.id.seekbar);
    sBar.setProgress(minutes - 1);
    sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            tvPopUpTime.setText(String.valueOf(arg1 + 1));
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {

        }
    });
    builder.setTitle(R.string.select_quit_time);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            long timeLeft = (sBar.getProgress() + 1) * mMill;
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeLeft + System.currentTimeMillis(), pendingIntent);
            MusicPlayer.setSleepMode(true);
            Toast.makeText(context, String.format(getString(R.string.quit_warining), sBar.getProgress() + 1),
                    Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            dialog.dismiss();
        }
    });
    builder.setView(view);
    return builder.create();
}

From source file:com.jackie.notifyingUser.MainActivity.java

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

    //        // Sets an ID for the notification, so it can be updated
    //        int notifyID = 0;
    ///*from w ww .  j  a  v a2 s  .  c o  m*/
    //        Intent resultIntent = new Intent(this, ResultActivity.class);
    //        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    //        // Adds the back stack
    //        stackBuilder.addParentStack(ResultActivity.class);
    //        // Adds the Intent to the top of the stack
    //        stackBuilder.addNextIntent(resultIntent);
    //        // Gets a PendingIntent containing the entire back stack
    //        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent resultIntent = new Intent(this, PingService.class);
    resultIntent.setAction(CommonConstants.ACTION_PING);
    resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, "test ping");
    PendingIntent resultPendingIntent = PendingIntent.getService(this, 0, resultIntent, 0);

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

    // Set the small icon to use in the notification layouts.
    builder.setSmallIcon(R.mipmap.ic_launcher);
    // Set the title (first row) of the notification, in a standard notification.
    builder.setContentTitle(getString(R.string.snooze));
    // Set the text (second row) of the notification, in a standard notification.
    builder.setContentText(getString(R.string.done_snoozing));

    builder.setContentIntent(resultPendingIntent);
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

    // Get an instance of the NotificationManger service
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    notificationManager.notify(CommonConstants.NOTIFICATION_ID, builder.build());
}

From source file:com.andrewshu.android.reddit.mail.EnvelopeService.java

public static void resetAlarm(Context context, long interval) {
    // Create an IntentSender that will launch our service, to be scheduled
    // with the alarm manager.
    PendingIntent alarmSender = PendingIntent.getService(context, 0, new Intent(context, EnvelopeService.class),
            0);// www.  j  a va 2  s.co  m
    AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    am.cancel(alarmSender);
    if (interval != 0)
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), interval,
                alarmSender);
}

From source file:com.androidinspain.deskclock.data.StopwatchNotificationBuilder.java

public Notification build(Context context, NotificationModel nm, Stopwatch stopwatch) {
    @StringRes/*from w w w .  j  a v  a 2 s .  com*/
    final int eventLabel = com.androidinspain.deskclock.R.string.label_notification;

    // Intent to load the app when the notification is tapped.
    final Intent showApp = new Intent(context, StopwatchService.class)
            .setAction(StopwatchService.ACTION_SHOW_STOPWATCH).putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

    final PendingIntent pendingShowApp = PendingIntent.getService(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    // Compute some values required below.
    final boolean running = stopwatch.isRunning();
    final String pname = context.getPackageName();
    final Resources res = context.getResources();
    final long base = SystemClock.elapsedRealtime() - stopwatch.getTotalTime();

    final RemoteViews content = new RemoteViews(pname,
            com.androidinspain.deskclock.R.layout.chronometer_notif_content);
    content.setChronometer(com.androidinspain.deskclock.R.id.chronometer, base, null, running);

    final List<Action> actions = new ArrayList<>(2);

    if (running) {
        // Left button: Pause
        final Intent pause = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_PAUSE_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_pause_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_pause_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, pause);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Add Lap
        if (DataModel.getDataModel().canAddMoreLaps()) {
            final Intent lap = new Intent(context, StopwatchService.class)
                    .setAction(StopwatchService.ACTION_LAP_STOPWATCH)
                    .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

            @DrawableRes
            final int icon2 = com.androidinspain.deskclock.R.drawable.ic_sw_lap_24dp;
            final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_lap_button);
            final PendingIntent intent2 = Utils.pendingServiceIntent(context, lap);
            actions.add(new Action.Builder(icon2, title2, intent2).build());
        }

        // Show the current lap number if any laps have been recorded.
        final int lapCount = DataModel.getDataModel().getLaps().size();
        if (lapCount > 0) {
            final int lapNumber = lapCount + 1;
            final String lap = res.getString(com.androidinspain.deskclock.R.string.sw_notification_lap_number,
                    lapNumber);
            content.setTextViewText(com.androidinspain.deskclock.R.id.state, lap);
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
        } else {
            content.setViewVisibility(com.androidinspain.deskclock.R.id.state, GONE);
        }
    } else {
        // Left button: Start
        final Intent start = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_START_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon1 = com.androidinspain.deskclock.R.drawable.ic_start_24dp;
        final CharSequence title1 = res.getText(com.androidinspain.deskclock.R.string.sw_start_button);
        final PendingIntent intent1 = Utils.pendingServiceIntent(context, start);
        actions.add(new Action.Builder(icon1, title1, intent1).build());

        // Right button: Reset (dismisses notification and resets stopwatch)
        final Intent reset = new Intent(context, StopwatchService.class)
                .setAction(StopwatchService.ACTION_RESET_STOPWATCH)
                .putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);

        @DrawableRes
        final int icon2 = com.androidinspain.deskclock.R.drawable.ic_reset_24dp;
        final CharSequence title2 = res.getText(com.androidinspain.deskclock.R.string.sw_reset_button);
        final PendingIntent intent2 = Utils.pendingServiceIntent(context, reset);
        actions.add(new Action.Builder(icon2, title2, intent2).build());

        // Indicate the stopwatch is paused.
        content.setTextViewText(com.androidinspain.deskclock.R.id.state,
                res.getString(com.androidinspain.deskclock.R.string.swn_paused));
        content.setViewVisibility(com.androidinspain.deskclock.R.id.state, VISIBLE);
    }

    final Builder notification = new NotificationCompat.Builder(context).setLocalOnly(true).setOngoing(running)
            .setCustomContentView(content).setContentIntent(pendingShowApp).setAutoCancel(stopwatch.isPaused())
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_stopwatch)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setColor(ContextCompat.getColor(context, com.androidinspain.deskclock.R.color.default_background));

    if (Utils.isNOrLater()) {
        notification.setGroup(nm.getStopwatchNotificationGroupKey());
    }

    for (Action action : actions) {
        notification.addAction(action);
    }

    return notification.build();
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

private Notification createStartingOld() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    builder.setOngoing(true);//from  w  w w.  j a  v a  2s.  c om
    builder.setContentTitle(context.getResources().getString(R.string.Downloading));
    builder.setContentText(track.getArtist() + "-" + track.getTitle());
    builder.setSmallIcon(R.drawable.download_icon);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(new ComponentName(context, DownloadingService.class));

    builder.setContentIntent(
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    return builder.build();
}

From source file:com.abid_mujtaba.bitcoin.tracker.services.FetchPriceService.java

public static void stop(Context context) // Method used to stop service via AlarmManager
{
    Intent intent = new Intent(context, FetchPriceService.class); // Intent to launch service
    PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent, 0); // PendingIntent required by AlarmManager. This gives the AlarmManager permission to launch this Intent as if it were being launched by this application

    AlarmManager amgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    amgr.cancel(alarmIntent);//from  ww  w. j  a  v a2  s.c o  m

    Logd("Stopping FetchPriceService.");
}

From source file:com.amlcurran.messages.notifications.NotificationActionBuilder.java

NotificationCompat.Action buildReplyAction(Conversation conversation) {
    RemoteInput remoteInput = new RemoteInput.Builder(SmsManagerOutputPort.EXTRA_VOICE_REPLY)
            .setLabel(context.getString(R.string.reply)).build();

    Intent replyIntent = new Intent(context, SmsManagerOutputPort.class);
    replyIntent.setAction(SmsManagerOutputPort.ACTION_SEND_REQUEST);
    replyIntent.putExtra(SmsManagerOutputPort.FROM_WEAR, true);
    replyIntent.putExtra(SmsManagerOutputPort.EXTRA_NUMBER, conversation.getAddress().flatten());
    PendingIntent replyPendingIntent = PendingIntent.getService(context, 0, replyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, context.getString(R.string.reply),
            replyPendingIntent).addRemoteInput(remoteInput).build();
}