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.kubotaku.android.code4kyoto5374.util.AlarmService.java

/**
 * ?//from  ww  w . j  av  a  2  s . co m
 *
 * @param context     
 * @param garbageType ??
 * @param ignoreToday true?????????
 */
private static void setupAlarm(Context context, final int garbageType, final boolean ignoreToday) {

    final Intent intent = createIntent(context, garbageType);
    final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final Alarm alarm = Prefs.loadAlarm(context, garbageType);
    if (!alarm.enable) {
        return;
    }

    final HomePlace homePlace = Prefs.loadHomePlace(context);
    final int masterID = homePlace.areaMasterID;
    final String areaName = homePlace.areaName;

    Realm realm = Realm.getDefaultInstance();
    final RealmResults<AreaDays> results = realm.where(AreaDays.class).equalTo("masterAreaID", masterID)
            .equalTo("areaName", areaName).findAll();

    if (results.size() != 1) {
        realm.close();
        return; // error
    }

    final AreaDays areaDays = results.first();
    final List<GarbageCollectDay.GarbageDaysForViews> garbageDays = GarbageCollectDay.GarbageDaysForViews
            .newList(areaDays.getTargetGarbageDays(garbageType));
    if (garbageDays == null) {
        realm.close();
        return; // error
    }

    final int nextDay = AppUtil.calcNearestDaysAfter(garbageDays, alarm.hour, alarm.minute, ignoreToday);

    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.DATE, nextDay);
    calendar.set(Calendar.HOUR_OF_DAY, alarm.hour);
    calendar.set(Calendar.MINUTE, alarm.minute);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
    final String time = sdf.format(calendar.getTime());
    Log.i(TAG, String.format(Locale.getDefault(), "Set up alarm at %s", time));

    final long triggerMillis = calendar.getTimeInMillis();
    setupAlarm(context, pendingIntent, triggerMillis);

    realm.close();
}

From source file:com.oryx.notifications.NotificationService.java

private void issueNotification(Intent intent) {

    Intent pauseIntent = new Intent(this, NotificationService.class);
    pauseIntent.setAction("PAUSE");
    PendingIntent piPause = PendingIntent.getService(this, 0, pauseIntent, 0);

    Intent stopIntent = new Intent(this, NotificationService.class);
    stopIntent.setAction("STOP");
    PendingIntent piStop = PendingIntent.getService(this, 0, stopIntent, 0);

    Notification n = new NotificationCompat.Builder(this).setContentTitle("Text To Speech")
            .setContentText("Reading the content").setContent(getNotiView(piPause, piStop))
            .setSmallIcon(R.drawable.noti_icon).build();

    mNotificationManager.notify(0, n);//  w w  w  .  ja v a 2  s  . c  o m
}

From source file:com.pyamsoft.zaptorch.service.VolumeServiceInteractorImpl.java

VolumeServiceInteractorImpl(@NonNull Context context, @NonNull ZapTorchPreferences preferences,
        @NonNull Class<? extends IntentService> torchOffServiceClass) {
    this.appContext = context.getApplicationContext();
    this.preferences = preferences;
    notificationManagerCompat = NotificationManagerCompat.from(appContext);

    // KLUDGE duplication of values between preferences and java code
    cameraApiOld = 0;/*from w  w w .  jav  a2  s. co  m*/
    cameraApiLollipop = 1;
    cameraApiMarshmallow = 2;

    final Intent intent = new Intent(appContext, torchOffServiceClass);
    notification = new NotificationCompat.Builder(appContext)
            .setContentIntent(PendingIntent.getService(appContext, NOTIFICATION_RC, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("Torch is On").setContentText("Click to turn off")
            .setSmallIcon(R.drawable.ic_light_notification).setAutoCancel(true)
            .setColor(ContextCompat.getColor(appContext, R.color.purple500)).setWhen(0).setOngoing(false)
            .build();
}

From source file:com.bluelinelabs.logansquare.typeconverters.PendingIntentConverter.java

@Override
public PendingIntent parse(JsonParser jsonParser) throws IOException {
    SimplePendingIntent simplePendingIntent = SimplePendingIntent$$JsonObjectMapper._parse(jsonParser);

    PendingIntent pendingIntent = null;/*  w ww  .  ja v  a 2s  .  c o m*/

    if (simplePendingIntent.getActivity != null && simplePendingIntent.getActivity) {
        android.util.Log.d("json2notification", "getActivity:" + simplePendingIntent.getActivity);
        pendingIntent = PendingIntent.getActivity(context,
                simplePendingIntent.requestCode == null ? 0 : simplePendingIntent.requestCode,
                simplePendingIntent.intent,
                simplePendingIntent.flags == null ? PendingIntent.FLAG_UPDATE_CURRENT
                        : simplePendingIntent.flags);
    } else if (simplePendingIntent.getService != null && simplePendingIntent.getService) {
        android.util.Log.d("json2notification", "getService:" + simplePendingIntent.getService);
        pendingIntent = PendingIntent.getService(context,
                simplePendingIntent.requestCode == null ? 0 : simplePendingIntent.requestCode,
                simplePendingIntent.intent,
                simplePendingIntent.flags == null ? PendingIntent.FLAG_UPDATE_CURRENT
                        : simplePendingIntent.flags);
    }
    android.util.Log.d("json2notification", "intent:" + simplePendingIntent.intent);
    android.util.Log.d("json2notification", "requestCode:" + simplePendingIntent.requestCode);
    android.util.Log.d("json2notification", "flags:" + simplePendingIntent.flags);
    return pendingIntent;
}

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

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

From source file:de.hero.vertretungsplan.CheckForAppUpdate.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    boolean setTimer = intent.getBooleanExtra("setTimer", false);
    boolean checkNow = intent.getBooleanExtra("checkNow", false);

    Intent i = new Intent(this, de.hero.vertretungsplan.CheckForAppUpdate.class);
    i.putExtra("checkNow", true);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    am.cancel(pi);/*from   w  ww .j a v a2 s  .  co m*/
    Log.d("CheckForAppUpdate", "Alarm canceled");
    if (setTimer) {

        long lngInterval = AlarmManager.INTERVAL_DAY * 7;
        Log.d("CheckForAppUpdate", "Alarm set");
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY * 6, lngInterval, pi);
        //For debugging: after 10 seconds
        //am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 25000 , AlarmManager.INTERVAL_HOUR , pi);

    }

    if (checkNow) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            try {
                new HtmlWorkAppUpdate(this).execute(new URI(getString(R.string.htmlAdresseAktuelleVersion)));
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return START_NOT_STICKY;
}

From source file:com.med_presc_pat.Utilities.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*from  ww  w.  jav  a 2 s  .co m*/
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, NotificationActionService.class).setAction(ACTION_1);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle("Medical")
            .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri)
            .addAction(R.drawable.common_google_signin_btn_icon_dark, "Accept", pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.sleekcoder.weardemo.NotificationActivity.java

private void buildLocalOnlyNotification(String title, String content, int notificationId,
        boolean withDismissal) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title).setContentText(content).setLocalOnly(true)
            .setSmallIcon(R.drawable.ic_launcher);

    if (withDismissal) {
        Intent dismissIntent = new Intent(Constants.ACTION_DISMISS);
        dismissIntent.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.BOTH_ID);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, dismissIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setDeleteIntent(pendingIntent);
    }/*from   w  w w.jav  a  2  s.  co  m*/
    NotificationManagerCompat.from(this).notify(notificationId, builder.build());
}

From source file:com.daiv.android.twitter.services.TrimDataService.java

public void setNextTrim(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    long now = new Date().getTime();
    long alarm = now + AlarmManager.INTERVAL_DAY;

    Log.v("alarm_date", "auto trim " + new Date(alarm).toString());

    PendingIntent pendingIntent = PendingIntent.getService(context, TRIM_ID,
            new Intent(context, TrimDataService.class), 0);

    am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
}

From source file:codes.simen.l50notifications.ReminderService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final String action = intent.getAction();
    if (ACTION_REMIND.equals(action)) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        final int reminderDelay = preferences.getInt("reminder_delay", 5000);

        final Bundle extras = intent.getExtras();

        reminders.put(System.currentTimeMillis() + reminderDelay, extras);

        handler.postDelayed(displayReminder, reminderDelay);

        builder = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.ic_reminder)
                .setContentTitle(String.format("%d reminders", reminders.size()))
                .setContentText("Tap to cancel all reminders").setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_MIN)
                .setContentIntent(PendingIntent.getService(getApplicationContext(), 0,
                        new Intent(getApplicationContext(), ReminderService.class).setAction(ACTION_STOP),
                        PendingIntent.FLAG_UPDATE_CURRENT));

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

    } else if (ACTION_STOP.equals(action)) {
        stopSelf();/*from w ww .  j a  v  a  2s  .  co  m*/
        Toast.makeText(getApplicationContext(), "All reminders cancelled", Toast.LENGTH_SHORT).show();
    }
    return START_NOT_STICKY;
}