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.irateam.vkplayer.notifications.DownloadNotification.java

public static Notification create(Context context, Audio audio, int progress, int audioLeftCount,
        boolean isSync) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Intent intent = new Intent(context, DownloadService.class);
    intent.setAction(DownloadService.STOP_DOWNLOADING);
    builder.setContentTitle(audio.getArtist() + " - " + audio.getTitle())
            .setSmallIcon(isSync ? R.drawable.ic_notification_sync_white_24dp
                    : R.drawable.ic_statusbar_download_white_18dp)
            .setContentText(isSync ? context.getString(R.string.notification_sync)
                    : context.getString(R.string.notification_download));

    if (audioLeftCount > 0) {
        builder.setContentInfo(context.getString(R.string.notification_audio_count_left) + audioLeftCount);
    }/* www .  jav  a2  s .  co m*/

    builder.setProgress(100, progress, false).addAction(R.drawable.ic_notification_cancel_white_24dp,
            context.getString(R.string.notification_cancel), PendingIntent.getService(context, 0, intent, 0));
    return builder.build();
}

From source file:com.google.android.apps.muzei.TaskQueueService.java

static PendingIntent getArtworkDownloadRetryPendingIntent(Context context) {
    return PendingIntent.getService(context, 0, getDownloadCurrentArtworkIntent(context),
            PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:ca.hoogit.powerhour.Notifications.Foreground.java

public static Notification build(Context context, GameModel game) {
    // Intent for the notification
    Intent intentMain = new Intent(context, MainActivity.class);
    intentMain.setAction(Constants.ACTION.MAIN);
    intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingMain = PendingIntent.getActivity(context, 0, intentMain, 0);

    // Pending intent action to pause the game
    Intent intentPause = new Intent(context, Engine.class);
    intentPause.setAction(Constants.ACTION.PAUSE_GAME);
    PendingIntent pendingPause = PendingIntent.getService(context, 0, intentPause, 0);

    // Pending intent action to pause the game
    Intent intentResume = new Intent(context, Engine.class);
    intentResume.setAction(Constants.ACTION.RESUME_GAME);
    PendingIntent pendingResume = PendingIntent.getService(context, 0, intentResume, 0);

    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);

    String contentText = game.minutesRemaining();
    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_stat_av_pause, "Pause",
            pendingPause);/* w w w .j ava 2s  .co m*/
    if (game.is(State.ACTIVE)) {
        contentText = "Active with " + contentText;
    } else if (game.is(State.PAUSED)) {
        contentText = "Paused with " + contentText;
        action = new NotificationCompat.Action(R.drawable.ic_stat_av_play_arrow, "Resume", pendingResume);
    }

    Notification notification = new NotificationCompat.Builder(context)
            .setContentTitle(game.options().getTitle() + " - Round " + game.currentRound())
            .setTicker(game.options().getTitle() + " - Round " + game.currentRound())
            .setContentText(contentText).setSmallIcon(R.drawable.ic_stat_beer)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pendingMain)
            .setOngoing(true).addAction(action).build();

    if (game.is(State.ACTIVE) && !game.canPause()) {
        notification.actions[0] = new Notification.Action(R.drawable.ic_stat_action_flip_to_front, "Open",
                pendingMain);
    }
    return notification;
}

From source file:com.android.deskclock.alarms.AlarmNotifications.java

public static void showLowPriorityNotification(Context context, AlarmInstance instance) {
    LogUtils.v("Displaying low priority notification for alarm instance: " + instance.mId);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.alarm_alert_predismiss_title))
            .setContentText(AlarmUtils.getAlarmText(context, instance, true /* includeLabel */))
            .setSmallIcon(R.drawable.stat_notify_alarm).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT).setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setLocalOnly(true);

    // Setup up hide notification
    Intent hideIntent = AlarmStateManager.createStateChangeIntent(context, AlarmStateManager.ALARM_DELETE_TAG,
            instance, AlarmInstance.HIDE_NOTIFICATION_STATE);
    notification.setDeleteIntent(PendingIntent.getService(context, instance.hashCode(), hideIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.PREDISMISSED_STATE);
    notification.addAction(R.drawable.ic_alarm_off_24dp,
            context.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getService(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    Intent viewAlarmIntent = createViewAlarmIntent(context, instance);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.notify(instance.hashCode(), notification.build());
}

From source file:ch.fixme.status.Widget.java

public void onReceive(Context ctxt, Intent intent) {
    String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        // Remove widget alarm
        int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        PendingIntent pi = PendingIntent.getService(ctxt, widgetId, getIntent(ctxt, widgetId), 0);
        AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pi);//  ww  w  . j a v a  2  s  .  co  m

        // remove preference
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
        Editor edit = prefs.edit();
        edit.remove(Main.PREF_API_URL_WIDGET + widgetId);
        edit.remove(Main.PREF_INIT_WIDGET + widgetId);
        edit.remove(Main.PREF_LAST_WIDGET + widgetId);
        edit.remove(Main.PREF_FORCE_WIDGET + widgetId);
        edit.commit();

        // Log.i(Main.TAG, "Remove widget alarm for id=" + widgetId);
    } else if (intent.hasExtra(WIDGET_IDS) && AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
        int[] ids = intent.getExtras().getIntArray(WIDGET_IDS);
        onUpdate(ctxt, AppWidgetManager.getInstance(ctxt), ids);
    } else
        super.onReceive(ctxt, intent);
}

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

public static void start(Context context) // Method used to start 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.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), INTERVAL, alarmIntent);
}

From source file:com.meiste.greg.ptw.QuestionAlarm.java

@SuppressLint("NewApi")
public static void set(final Context context) {
    // Get next points race: allow in progress
    Race race = Race.getNext(context, false, true);
    if (race == null)
        return;//from w ww.j a v a2s.c o  m

    // Check if user was already reminded of in progress race
    if (Util.getState(context).getInt(LAST_REMIND, -1) >= race.getId()) {
        // Get next points race: do not allow in progress
        race = Race.getNext(context, false, false);
        if (race == null)
            return;
    }

    if (!alarm_set) {
        Util.log("Setting question alarm for race " + race.getId());

        final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent intent = new Intent(context, QuestionAlarm.class);
        intent.putExtra(RACE_ID, race.getId());
        final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            am.setExact(AlarmManager.RTC_WAKEUP, race.getQuestionTimestamp(), pendingIntent);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, race.getQuestionTimestamp(), pendingIntent);
        }

        alarm_set = true;
    } else {
        Util.log("Not setting question alarm: alarm_set=" + alarm_set);
    }
}

From source file:com.packpublishing.asynchronousandroid.chapter6.SMSDispatchActivity.java

void launchService(String phoneMumber, String text, long time) {

    Intent intent = new Intent(this, SMSDispatcherIntentService.class);
    intent.putExtra(SMSDispatcherIntentService.TO_KEY, phoneMumber);
    intent.putExtra(SMSDispatcherIntentService.TEXT_KEY, text);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    PendingIntent service = PendingIntent.getService(getBaseContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, time, service);
}

From source file:com.actinarium.nagbox.service.NotificationHelper.java

private static void fireForSingleTask(Context context, Task task) {
    long currentTime = System.currentTimeMillis();

    // Notification action to stop the task
    Intent stopAction = new Intent(context, NagboxService.class);
    stopAction.setAction(NagboxService.ACTION_ON_NOTIFICATION_ACTION_STOP_TASK);
    stopAction.putExtra(NagboxService.EXTRA_TASK_ID, task.id);
    stopAction.putExtra(NagboxService.EXTRA_CANCEL_NOTIFICATION_ID, NAG_NOTIFICATION_ID);
    PendingIntent stopActionPI = PendingIntent.getService(context, 0, stopAction,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Create public notification
    Notification publicNotification = makeCommonBuilder(context, currentTime, task.id)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(/*from w w  w  . j  a v  a 2s  . c om*/
                    context.getResources().getQuantityString(R.plurals.notification_stacked_header, 1, 1))
            .build();

    // Create private notification
    Notification privateNotification = makeCommonBuilder(context, currentTime, task.id)
            .setPublicVersion(publicNotification).setContentTitle(task.title)
            .setContentText(DateUtils.prettyPrintNagDuration(context, task.lastStartedAt, currentTime))
            .addAction(R.drawable.ic_cancel, context.getString(R.string.notification_action_stop), stopActionPI)
            .build();

    // Fire!
    NotificationManagerCompat.from(context).notify(NAG_NOTIFICATION_ID, privateNotification);
}

From source file:com.yeldi.yeldibazaar.UpdateService.java

public static void schedule(Context ctx) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String sint = prefs.getString("updateInterval", "0");
    int interval = Integer.parseInt(sint);

    Intent intent = new Intent(ctx, UpdateService.class);
    PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0);

    AlarmManager alarm = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);/*from  w  w  w.  j a va2 s .  c  o m*/
    if (interval > 0) {
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000,
                AlarmManager.INTERVAL_HOUR, pending);
        Log.d("FDroid", "Update scheduler alarm set");
    } else {
        Log.d("FDroid", "Update scheduler alarm not set");
    }

}