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.ibm.mf.geofence.MFGeofencingManager.java

/**
 * Get a pending intent for the specified callback.
 * @param geofenceCallbackUuid the uuid of an internally mapped callback.
 * @return a <code>PendingIntent</code> instance.
 *//*  ww w  . j a va  2  s.  com*/
private PendingIntent getPendingIntent(String geofenceCallbackUuid) {
    if (mPendingIntent == null) {
        Intent intent = new Intent(mContext, GeofenceTransitionsService.class);
        //intent.setPackage(context.getPackageName());
        ServiceConfig config = new ServiceConfig().fromGeofencingManager(this);
        config.populateFromSettings(mSettings);
        config.toIntent(intent);
        intent.putExtra(INTENT_ID, geofenceCallbackUuid);
        intent.setClass(mContext, GeofenceTransitionsService.class);
        mPendingIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    return mPendingIntent;
}

From source file:edu.mit.media.funf.probe.Probe.java

/**
 * Resets all of the run data information, and removes all requests
 *//*from  www.j a  v  a  2s  . c  o  m*/
public void reset() {
    getHistoryPrefs().edit().clear().commit();
    if (requestsIntent != null) {
        requestsIntent.removeExtra(INTERNAL_REQUESTS_KEY);
    }
    PendingIntent selfLaunchingIntent = PendingIntent.getService(this, 0, getRequestsIntent(),
            PendingIntent.FLAG_NO_CREATE);
    if (selfLaunchingIntent != null) {
        selfLaunchingIntent.cancel();
    }
}

From source file:com.prod.intelligent7.engineautostart.ConnectDaemonService.java

public void startServerHearBeatAlarm() {
    if (heartBitIntent != null) {
        alarmManager.cancel(heartBitIntent);
        //return;
    }// w  w w  .j a va2 s  .c  om
    heartBitIntent = null;
    //serverHeartBit=60*1000;
    //if (!mDaemon.notUrgent) serverHeartBit=10*1000;
    Intent jIntent = new Intent(this, ConnectDaemonService.class);
    //M1-00 (cool) or M1-01 (warm)
    jIntent.putExtra(ConnectDaemonService.ALARM_DONE, ALARM_BIT);
    heartBitIntent = PendingIntent.getService(this, ++heartBitRequestId % 100 + 1000, jIntent,
            PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + serverHeartBit, heartBitIntent);
    Log.i("ALARM_SET", "restart new alarm set for " + serverHeartBit / 1000 + " seconds");
    //mDaemon.hasCommand=true;
    //mDaemon.interrupt();
}

From source file:com.irccloud.android.data.collection.NotificationsList.java

private void showOtherNotifications() {
    String title = "";
    String text = "";
    String ticker;//  w  w  w  .  j  ava  2s  .  co  m
    NotificationCompat.Action action = null;
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext());
    final List<Notification> notifications = getOtherNotifications();

    int notify_type = Integer.parseInt(prefs.getString("notify_type", "1"));
    boolean notify = false;
    if (notify_type == 1 || (notify_type == 2 && NetworkConnection.getInstance().isVisible()))
        notify = true;

    if (notifications.size() > 0 && notify) {
        for (Notification n : notifications) {
            if (!n.shown) {
                Crashlytics.log(Log.DEBUG, "IRCCloud", "Posting notification for type " + n.message_type);
                if (n.message_type.equals("callerid")) {
                    title = n.network;
                    text = n.nick + " is trying to contact you";
                    ticker = n.nick + " is trying to contact you on " + n.network;

                    Intent i = new Intent(RemoteInputService.ACTION_REPLY);
                    i.setComponent(new ComponentName(
                            IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
                            RemoteInputService.class.getName()));
                    i.putExtra("cid", n.cid);
                    i.putExtra("eid", n.eid);
                    i.putExtra("chan", n.chan);
                    i.putExtra("buffer_type", n.buffer_type);
                    i.putExtra("network", n.network);
                    i.putExtra("to", n.nick);
                    i.putExtra("reply", "/accept " + n.nick);
                    action = new NotificationCompat.Action(R.drawable.ic_wearable_add, "Accept",
                            PendingIntent.getService(IRCCloudApplication.getInstance().getApplicationContext(),
                                    (int) (n.eid / 1000), i, PendingIntent.FLAG_UPDATE_CURRENT));
                } else if (n.message_type.equals("callerid_success")) {
                    title = n.network;
                    text = n.nick + " has been added to your accept list";
                    ticker = n.nick + " has been added to your accept list on " + n.network;
                    Intent i = new Intent(RemoteInputService.ACTION_REPLY);
                    i.setComponent(new ComponentName(
                            IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
                            RemoteInputService.class.getName()));
                    i.putExtra("cid", n.cid);
                    i.putExtra("eid", n.eid);
                    i.putExtra("chan", n.chan);
                    i.putExtra("buffer_type", n.buffer_type);
                    i.putExtra("network", n.network);
                    i.putExtra("to", n.nick);
                    action = new NotificationCompat.Action.Builder(R.drawable.ic_wearable_reply, "Message",
                            PendingIntent.getService(IRCCloudApplication.getInstance().getApplicationContext(),
                                    (int) (n.eid / 1000), i, PendingIntent.FLAG_UPDATE_CURRENT))
                                            .setAllowGeneratedReplies(true)
                                            .addRemoteInput(new RemoteInput.Builder("extra_reply")
                                                    .setLabel("Message to " + n.nick).build())
                                            .build();
                } else if (n.message_type.equals("channel_invite")) {
                    title = n.network;
                    text = n.nick + " invited you to join " + n.chan;
                    ticker = text;
                    try {
                        Intent i = new Intent();
                        i.setComponent(new ComponentName(
                                IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
                                "com.irccloud.android.MainActivity"));
                        i.setData(Uri.parse(IRCCloudApplication.getInstance().getResources()
                                .getString(R.string.IRCCLOUD_SCHEME) + "://cid/" + n.cid + "/"
                                + URLEncoder.encode(n.chan, "UTF-8")));
                        i.putExtra("eid", n.eid);
                        action = new NotificationCompat.Action(R.drawable.ic_wearable_add, "Join",
                                PendingIntent.getActivity(
                                        IRCCloudApplication.getInstance().getApplicationContext(),
                                        (int) (n.eid / 1000), i, PendingIntent.FLAG_UPDATE_CURRENT));
                    } catch (Exception e) {
                        action = null;
                    }
                } else {
                    title = n.nick;
                    text = n.message;
                    ticker = n.message;
                    action = null;
                }
                if (title != null && text != null)
                    NotificationManagerCompat.from(IRCCloudApplication.getInstance().getApplicationContext())
                            .notify((int) (n.eid / 1000), buildNotification(ticker, n.cid, n.bid,
                                    new long[] { n.eid }, title, text, 1, null, n.network, null, action,
                                    AvatarsList.getInstance().getAvatar(n.cid, n.nick).getBitmap(false,
                                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 64,
                                                    IRCCloudApplication.getInstance().getApplicationContext()
                                                            .getResources().getDisplayMetrics()),
                                            false, Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP),
                                    AvatarsList.getInstance().getAvatar(n.cid, n.nick).getBitmap(false, 400,
                                            false, false)));
            }
        }
        TransactionManager.transact(IRCCloudDatabase.NAME, new Runnable() {
            @Override
            public void run() {
                for (Notification n : notifications) {
                    n.delete();
                }
            }
        });
    }
}

From source file:com.adkdevelopment.earthquakesurvival.ui.PagerActivity.java

private PendingIntent getGeofencePendingIntent() {
    Intent intent = new Intent(this, GeofenceService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling addgeoFences()
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.mendhak.gpslogger.GpsLoggingService.java

/**
 * Shows a notification icon in the status bar for GPS Logger
 *//*from   w  w w. j ava2s. c  om*/
private void showNotification() {

    Intent stopLoggingIntent = new Intent(this, GpsLoggingService.class);
    stopLoggingIntent.setAction("NotificationButton_STOP");
    stopLoggingIntent.putExtra(IntentConstants.IMMEDIATE_STOP, true);
    PendingIntent piStop = PendingIntent.getService(this, 0, stopLoggingIntent, 0);

    Intent annotateIntent = new Intent(this, NotificationAnnotationActivity.class);
    annotateIntent.setAction("com.mendhak.gpslogger.NOTIFICATION_BUTTON");
    PendingIntent piAnnotate = PendingIntent.getActivity(this, 0, annotateIntent, 0);

    // What happens when the notification item is clicked
    Intent contentIntent = new Intent(this, GpsMainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(contentIntent);

    PendingIntent pending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NumberFormat nf = new DecimalFormat("###.#####");

    String contentText = getString(R.string.gpslogger_still_running);
    long notificationTime = System.currentTimeMillis();

    if (Session.hasValidLocation()) {
        contentText = getString(R.string.txt_latitude_short) + ": " + nf.format(Session.getCurrentLatitude())
                + ", " + getString(R.string.txt_longitude_short) + ": "
                + nf.format(Session.getCurrentLongitude());

        notificationTime = Session.getCurrentLocationInfo().getTime();
    }

    if (nfc == null) {
        nfc = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.notification)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.gpsloggericon3))
                .setPriority(Notification.PRIORITY_MAX).setContentTitle(contentText).setOngoing(true)
                .setContentIntent(pending);

        if (!preferenceHelper.shouldHideNotificationButtons()) {
            nfc.addAction(R.drawable.annotate2, getString(R.string.menu_annotate), piAnnotate).addAction(
                    android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.shortcut_stop), piStop);
        }
    }

    nfc.setContentTitle(contentText);
    nfc.setContentText(getString(R.string.app_name));
    nfc.setWhen(notificationTime);

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

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * Creates a {@link PendingIntent} for the specified Wear notification action.
 *//*  w w  w  . ja  v a2s  . co m*/
private static PendingIntent getWearNotificationActionPendingIntent(final Context context,
        final Account account, final Conversation conversation, final Message message, final Folder folder,
        final Intent notificationIntent, final NotificationActionType action, final int notificationId,
        final long when) {
    final Uri messageUri = message.uri;

    final NotificationAction notificationAction = new NotificationAction(action, account, conversation, message,
            folder, conversation.id, message.serverId, message.id, when, NotificationAction.SOURCE_REMOTE,
            notificationId);

    switch (action) {
    case REPLY:
    case REPLY_ALL: {
        // Build a task stack that forces the conversation view on the stack before the
        // reply activity.
        final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

        final Intent intent = createReplyIntent(context, account, messageUri,
                (action == NotificationActionType.REPLY_ALL));
        intent.setPackage(context.getPackageName());
        intent.setData(buildWearUri(conversation.uri));
        intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);
        intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_CONVERSATION, conversation.uri);

        taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

        return taskStackBuilder.getPendingIntent(notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    case ARCHIVE_REMOVE_LABEL:
    case DELETE: {
        final String intentAction = (action == NotificationActionType.ARCHIVE_REMOVE_LABEL)
                ? NotificationActionIntentService.ACTION_ARCHIVE_REMOVE_LABEL
                : NotificationActionIntentService.ACTION_DELETE;

        final Intent intent = new Intent(intentAction);
        intent.setPackage(context.getPackageName());
        intent.setData(buildWearUri(conversation.uri));
        putNotificationActionExtra(intent, notificationAction);

        return PendingIntent.getService(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    }

    throw new IllegalArgumentException("Invalid NotificationActionType");
}

From source file:com.android.deskclock.data.TimerModel.java

/**
 * Updates the callback given to this application from the {@link AlarmManager} that signals the
 * expiration of the next timer. If no timers are currently set to expire (i.e. no running
 * timers exist) then this method clears the expiration callback from AlarmManager.
 *///from  ww  w  .  j  av  a 2  s .c o m
private void updateAlarmManager() {
    // Locate the next firing timer if one exists.
    Timer nextExpiringTimer = null;
    for (Timer timer : getMutableTimers()) {
        if (timer.isRunning()) {
            if (nextExpiringTimer == null) {
                nextExpiringTimer = timer;
            } else if (timer.getExpirationTime() < nextExpiringTimer.getExpirationTime()) {
                nextExpiringTimer = timer;
            }
        }
    }

    // Build the intent that signals the timer expiration.
    final Intent intent = TimerService.createTimerExpiredIntent(mContext, nextExpiringTimer);

    if (nextExpiringTimer == null) {
        // Cancel the existing timer expiration callback.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            mAlarmManager.cancel(pi);
            pi.cancel();
        }
    } else {
        // Update the existing timer expiration callback.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
        schedulePendingIntent(nextExpiringTimer.getExpirationTime(), pi);
    }
}

From source file:com.crearo.gpslogger.GpsLoggingService.java

/**
 * Shows a notification icon in the status bar for GPS Logger
 *//*  w  w w.  ja  v a2  s. c  om*/
private void showNotification() {

    Intent stopLoggingIntent = new Intent(this, GpsLoggingService.class);
    stopLoggingIntent.setAction("NotificationButton_STOP");
    stopLoggingIntent.putExtra(IntentConstants.IMMEDIATE_STOP, true);
    PendingIntent piStop = PendingIntent.getService(this, 0, stopLoggingIntent, 0);

    Intent annotateIntent = new Intent(this, NotificationAnnotationActivity.class);
    annotateIntent.setAction("com.mendhak.gpslogger.NOTIFICATION_BUTTON");
    PendingIntent piAnnotate = PendingIntent.getActivity(this, 0, annotateIntent, 0);

    // What happens when the notification item is clicked
    Intent contentIntent = new Intent(this, SimpleMainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(contentIntent);

    PendingIntent pending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NumberFormat nf = new DecimalFormat("###.#####");

    String contentText = getString(R.string.gpslogger_still_running);
    long notificationTime = System.currentTimeMillis();

    if (Session.hasValidLocation()) {
        contentText = getString(R.string.txt_latitude_short) + ": " + nf.format(Session.getCurrentLatitude())
                + ", " + getString(R.string.txt_longitude_short) + ": "
                + nf.format(Session.getCurrentLongitude());

        notificationTime = Session.getCurrentLocationInfo().getTime();
    }

    if (nfc == null) {
        nfc = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.notification)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.gpsloggericon3))
                .setPriority(Notification.PRIORITY_MAX).setContentTitle(contentText).setOngoing(true)
                .setContentIntent(pending);

        if (!preferenceHelper.shouldHideNotificationButtons()) {
            nfc.addAction(R.drawable.annotate2, getString(R.string.menu_annotate), piAnnotate).addAction(
                    android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.shortcut_stop), piStop);
        }
    }

    nfc.setContentTitle(contentText);
    nfc.setContentText(getString(R.string.app_name));
    nfc.setWhen(notificationTime);

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

From source file:com.example.bluetooth.RFduinoService.java

private NotificationCompat.Builder buildServiceNotification() {
    Intent notificationIntent = new Intent(RFduinoService.this, BluetoothActivity.class);
    notificationIntent.setAction("RFduinoTest_CallToMain");
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(RFduinoService.this, 0, notificationIntent, 0);

    Intent discoIntent = new Intent(RFduinoService.this, RFduinoService.class);
    discoIntent.setAction("ACTION_DISCONNECT");
    PendingIntent pDiscoIntent = PendingIntent.getService(RFduinoService.this, 0, discoIntent, 0);

    Intent connIntent = new Intent(RFduinoService.this, RFduinoService.class);
    connIntent.setAction("ACTION_CONNECT");
    PendingIntent pConnIntent = PendingIntent.getService(RFduinoService.this, 0, connIntent, 0);

    Intent stopIntent = new Intent(RFduinoService.this, RFduinoService.class);
    stopIntent.setAction("RFduinoService_Stop");
    PendingIntent pStopIntent = PendingIntent.getService(RFduinoService.this, 0, stopIntent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(RFduinoService.this)
            .setContentTitle("Bluetooth Connection running").setTicker("BTLE Ticker")
            .setContentText("RFDuino connected").setSmallIcon(R.drawable.ic_launcher)
            //                    .setLargeIcon(
            //                          Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setContentIntent(pendingIntent).setOngoing(true) // maybe disable to allow closing with x-button?
            .addAction(android.R.drawable.ic_media_pause, "Disconnect", pDiscoIntent)
            .addAction(android.R.drawable.ic_media_play, "Connect", pConnIntent)
            .addAction(android.R.drawable.ic_delete, "Stop", pStopIntent);
    return mBuilder;
}