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.smedic.tubtub.BackgroundAudioService.java

/**
 * Builds notification panel with buttons and info on it
 *
 * @param action Action to be applied// w w  w  .ja v  a  2s  .c  om
 */

private void buildNotification(NotificationCompat.Action action) {

    final NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();

    Intent intent = new Intent(getApplicationContext(), BackgroundAudioService.class);
    intent.setAction(ACTION_STOP);
    PendingIntent stopPendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);

    Intent clickIntent = new Intent(this, MainActivity.class);
    clickIntent.setAction(Intent.ACTION_MAIN);
    clickIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent clickPendingIntent = PendingIntent.getActivity(this, 0, clickIntent, 0);

    builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle(videoItem.getTitle());
    builder.setContentInfo(videoItem.getDuration());
    builder.setShowWhen(false);
    builder.setContentIntent(clickPendingIntent);
    builder.setDeleteIntent(stopPendingIntent);
    builder.setOngoing(false);
    builder.setSubText(videoItem.getViewCount());
    builder.setStyle(style);

    //load bitmap for largeScreen
    if (videoItem.getThumbnailURL() != null && !videoItem.getThumbnailURL().isEmpty()) {
        Picasso.with(this).load(videoItem.getThumbnailURL()).into(target);
    }

    builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS));
    builder.addAction(action);
    builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT));
    style.setShowActionsInCompactView(0, 1, 2);

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

}

From source file:alaindc.crowdroid.SendIntentService.java

private void handleActionReceivedData(String response) {

    // Data got from server response
    int timeout; // sec
    double radius; // meters
    int sensor;// w  ww.jav a2s .c  o m
    double latitude, longitude;

    // Update view sending a broadcast intent
    Intent intent = new Intent(Constants.INTENT_RECEIVED_DATA);
    intent.putExtra(Constants.INTENT_RECEIVED_DATA_EXTRA_DATA, response);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    try {
        JSONArray jsonArray = new JSONArray(response);
        JSONObject jsonObject = jsonArray.getJSONObject(0);

        sensor = jsonObject.getInt("sensor");
        // For time homogeneity
        timeout = jsonObject.getInt("timeout");
        // For space homogeneity
        radius = jsonObject.getDouble("radius");
        latitude = jsonObject.getDouble("lat");
        longitude = jsonObject.getDouble("long");

    } catch (JSONException e) {
        return;
    }

    if (sensor == Constants.TYPE_TEL) {
        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(Constants.PREF_FILE,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(Constants.THROUGHPUT_TAKEN, false);
        editor.commit();
    }

    Intent geofenceIntent = new Intent(getApplicationContext(), GeofenceIntentService.class);
    geofenceIntent.putExtra(Constants.EXTRA_GEOFENCE_SENSORTYPE, sensor);
    geofenceIntent.putExtra(Constants.EXTRA_GEOFENCE_LATITUDE, latitude);
    geofenceIntent.putExtra(Constants.EXTRA_GEOFENCE_LONGITUDE, longitude);
    geofenceIntent.putExtra(Constants.EXTRA_GEOFENCE_RADIUS, String.valueOf(radius));
    geofenceIntent.putExtra(Constants.EXTRA_GEOFENCE_EXPIRE_MILLISEC, String.valueOf(timeout * 1000));
    getApplicationContext().startService(geofenceIntent);

    // Set timeout based on server response
    alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(getApplicationContext(), SendIntentService.class);
    intentAlarm.setAction(Constants.ACTION_SENDDATA + sensor);
    intentAlarm.putExtra(Constants.EXTRA_TYPE_OF_SENSOR_TO_SEND, sensor);
    alarmIntent = PendingIntent.getService(getApplicationContext(), 0, intentAlarm, 0);

    int seconds = timeout;
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + seconds * 1000,
            alarmIntent);
}

From source file:com.aware.Aware.java

@Override
public void onCreate() {
    super.onCreate();

    awareContext = getApplicationContext();

    aware_preferences = getSharedPreferences("aware_core_prefs", MODE_PRIVATE);
    if (aware_preferences.getAll().isEmpty()) {
        SharedPreferences.Editor editor = aware_preferences.edit();
        editor.putInt(PREF_FREQUENCY_WATCHDOG, CONST_FREQUENCY_WATCHDOG);
        editor.putLong(PREF_LAST_SYNC, 0);
        editor.putLong(PREF_LAST_UPDATE, 0);
        editor.commit();//from  ww  w .  j a  va 2s .co  m
    }

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    filter.addDataScheme("file");
    awareContext.registerReceiver(storage_BR, filter);

    filter = new IntentFilter();
    filter.addAction(Aware.ACTION_AWARE_CLEAR_DATA);
    filter.addAction(Aware.ACTION_AWARE_REFRESH);
    filter.addAction(Aware.ACTION_AWARE_SYNC_DATA);
    filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    awareContext.registerReceiver(aware_BR, filter);

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    awareStatusMonitor = new Intent(getApplicationContext(), Aware.class);
    repeatingIntent = PendingIntent.getService(getApplicationContext(), 0, awareStatusMonitor, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000,
            aware_preferences.getInt(PREF_FREQUENCY_WATCHDOG, 300) * 1000, repeatingIntent);

    Intent synchronise = new Intent(Aware.ACTION_AWARE_SYNC_DATA);
    webserviceUploadIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, synchronise, 0);

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        stopSelf();
    } else {
        SharedPreferences prefs = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
        if (prefs.getAll().isEmpty()
                && Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID).length() == 0) {
            PreferenceManager.setDefaultValues(getApplicationContext(), getPackageName(), Context.MODE_PRIVATE,
                    R.xml.aware_preferences, true);
            prefs.edit().commit(); //commit changes
        } else {
            PreferenceManager.setDefaultValues(getApplicationContext(), getPackageName(), Context.MODE_PRIVATE,
                    R.xml.aware_preferences, false);
        }

        Map<String, ?> defaults = prefs.getAll();
        for (Map.Entry<String, ?> entry : defaults.entrySet()) {
            if (Aware.getSetting(getApplicationContext(), entry.getKey()).length() == 0) {
                Aware.setSetting(getApplicationContext(), entry.getKey(), entry.getValue());
            }
        }

        if (Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID).length() == 0) {
            UUID uuid = UUID.randomUUID();
            Aware.setSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID, uuid.toString());
        }

        DEBUG = Aware.getSetting(awareContext, Aware_Preferences.DEBUG_FLAG).equals("true");
        TAG = Aware.getSetting(awareContext, Aware_Preferences.DEBUG_TAG).length() > 0
                ? Aware.getSetting(awareContext, Aware_Preferences.DEBUG_TAG)
                : TAG;

        get_device_info();

        if (Aware.DEBUG)
            Log.d(TAG, "AWARE framework is created!");

        //Fixed: only the client application does a ping to AWARE's server
        if (getPackageName().equals("com.aware")) {
            new AsyncPing().execute();
        }
    }
}

From source file:com.granita.tasks.notification.NotificationActionIntentService.java

private static PendingIntent getDelayActionIntent(Context context, int notificationId, Uri taskUri, long due,
        boolean delay1h, String timezone, boolean allday) {
    String action = null;/*from w  w  w . ja v  a 2 s . c om*/
    if (delay1h) {
        action = ACTION_DELAY_1H;
    } else {
        action = ACTION_DELAY_1D;
    }
    final Intent intent = new Intent(action);
    intent.setPackage(context.getPackageName());
    intent.setData(taskUri);
    intent.putExtra(EXTRA_TASK_DUE, due);
    intent.putExtra(EXTRA_TIMEZONE, timezone);
    intent.putExtra(EXTRA_ALLDAY, allday);
    intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
    final PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE_DELAY, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    return pendingIntent;
}

From source file:com.securecomcode.text.service.KeyCachingService.java

private PendingIntent buildLockIntent() {
    Intent intent = new Intent(this, KeyCachingService.class);
    intent.setAction(PASSPHRASE_EXPIRED_EVENT);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
    return pendingIntent;
}

From source file:com.farmerbb.secondscreen.service.NotificationService.java

private void setActionButton(String key, SharedPreferences prefCurrent, int code) {
    Intent customIntent;/*from w  w w . j  av  a2 s. c  o  m*/
    PendingIntent customPendingIntent = null;
    String customString = null;

    if (key.equals("turn-off")) {
        // Turn Off
        customIntent = new Intent(this, TurnOffActivity.class);
        customIntent.putExtra("notification", true);
        customPendingIntent = PendingIntent.getActivity(this, code, customIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        customString = getResources().getStringArray(R.array.pref_notification_action_list)[0];
    } else if (key.equals("lock-device")) {
        // Lock Device
        customIntent = new Intent(this, LockDeviceService.class);
        customPendingIntent = PendingIntent.getService(this, code, customIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        customString = getResources().getStringArray(R.array.pref_notification_action_list)[2];
    } else if (key.equals("quick-actions")) {
        // Quick Actions
        customIntent = new Intent(this, TaskerQuickActionsActivity.class);
        customIntent.putExtra("launched-from-app", true);
        customPendingIntent = PendingIntent.getActivity(this, code, customIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        customString = getResources().getStringArray(R.array.pref_notification_action_list)[1];
    } else if (key.startsWith("temp_")) {
        // Toggle
        customIntent = new Intent(this, TaskerQuickActionsActivity.class);
        customIntent.putExtra(U.KEY, key);
        customIntent.putExtra(U.VALUE, "Toggle");
        customPendingIntent = PendingIntent.getActivity(this, code, customIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        String onOffString;

        switch (key) {
        case "temp_backlight_off":
            if (prefCurrent.getBoolean("backlight_off", false))
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[0];
            else
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[1];

            customString = getResources().getString(R.string.quick_backlight) + onOffString;
            break;
        case "temp_chrome":
            if (prefCurrent.getBoolean("chrome", false))
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[1];
            else
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[0];

            customString = getResources().getString(R.string.desktop) + onOffString;
            break;
        case "temp_immersive":
            switch (prefCurrent.getString("immersive_new", "fallback")) {
            case "immersive-mode":
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[1];
                break;
            default:
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[0];
                break;
            }

            customString = getResources().getString(R.string.immersive) + onOffString;
            break;
        case "temp_overscan":
            if (prefCurrent.getBoolean("overscan", false))
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[1];
            else
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[0];

            customString = getResources().getString(R.string.quick_overscan) + onOffString;
            break;
        case "temp_vibration_off":
            if (prefCurrent.getBoolean("vibration_off", false))
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[0];
            else
                onOffString = " " + getResources().getStringArray(R.array.pref_quick_actions)[1];

            customString = getResources().getString(R.string.quick_vibration) + onOffString;
            break;
        }
    }

    // Add action to notification builder
    mBuilder.addAction(0, customString, customPendingIntent);
}

From source file:com.nextgis.firereporter.GetFiresService.java

protected void Prepare() {
    mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mnCurrentExec = 0;/*from w w w  .j  a  v a  2s .c o  m*/
    mmoFires = new HashMap<Long, FireItem>();

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nUserCount = 0;
    nNasaCount = 0;
    nScanexCount = 0;
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
    //stackBuilder.addNextIntent(new Intent(this, ScanexNotificationsActivity.class));
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_fire_small)
            .setContentTitle(getString(R.string.stNewFireNotifications));
    mBuilder.setContentIntent(resultPendingIntent);

    Intent delIntent = new Intent(this, GetFiresService.class);
    delIntent.putExtra(COMMAND, SERVICE_NOTIFY_DISMISSED);
    PendingIntent deletePendingIntent = PendingIntent.getService(this, 0, delIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setDeleteIntent(deletePendingIntent);

    mInboxStyle = new NotificationCompat.InboxStyle();
    mInboxStyle.setBigContentTitle(getString(R.string.stNewFireNotificationDetailes));

    LoadScanexData();

    mSanextCookieTime = new Time();
    mSanextCookieTime.setToNow();
    msScanexLoginCookie = new String("not_set");

    mFillDataHandler = new Handler() {
        public void handleMessage(Message msg) {

            mnCurrentExec--;

            Bundle resultData = msg.getData();
            boolean bHaveErr = resultData.getBoolean(ERROR);
            if (bHaveErr) {
                SendError(resultData.getString(ERR_MSG));
            } else {
                int nType = resultData.getInt(SOURCE);
                String sData = resultData.getString(JSON);
                switch (nType) {
                case 3:
                    FillScanexData(nType, sData);
                    break;
                case 4:
                    msScanexLoginCookie = sData;
                    mSanextCookieTime.setToNow();
                    GetScanexData(false);
                    break;
                default:
                    FillData(nType, sData);
                    break;
                }
            }
            GetDataStoped();
        };
    };
}

From source file:com.android.settings.cyanogenmod.LtoService.java

private PendingIntent scheduleNextDownload(long lastDownload) {
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, LtoService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    long nextLtoDownload = lastDownload + LongTermOrbits.getDownloadInterval();
    am.set(AlarmManager.RTC, nextLtoDownload, pi);
    return pi;//from   ww w. j a  v  a 2 s . c  o  m
}

From source file:com.peptrack.gps.locationupdatesforegroundservice.LocationUpdatesService.java

/**
 * Returns the {@link NotificationCompat} used as part of the foreground service.
 *///from w w w .j  ava 2s.  c  o m
private Notification getNotification() {
    Intent intent = new Intent(this, LocationUpdatesService.class);

    CharSequence text = Utils.getLocationText(mLocation);

    // Extra to help us figure out if we arrived in onStartCommand via the notification or not.
    intent.putExtra(EXTRA_STARTED_FROM_NOTIFICATION, true);

    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // The PendingIntent to launch activity.
    PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .addAction(R.drawable.ic_launch, getString(R.string.launch_activity), activityPendingIntent)
            .addAction(R.drawable.ic_cancel, getString(R.string.remove_location_updates), servicePendingIntent)
            .setContentText(text).setContentTitle(Utils.getLocationTitle(this)).setOngoing(true)
            .setPriority(Notification.PRIORITY_HIGH).setSmallIcon(R.mipmap.ic_launcher).setTicker(text)
            .setWhen(System.currentTimeMillis());

    // Set the Channel ID for Android O.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(CHANNEL_ID); // Channel ID
    }

    return builder.build();
}

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

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

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context).setShowWhen(false)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(context.getString(com.wizardsofm.deskclock.R.string.alarm_alert_snooze_until,
                    AlarmUtils.getFormattedTime(context, instance.getAlarmTime())))
            .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background))
            .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_alarm).setAutoCancel(false)
            .setSortKey(createSortKey(instance)).setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setLocalOnly(true);/*from ww w. ja v a  2  s .co m*/

    if (Utils.isNOrLater()) {
        notification.setGroup(UPCOMING_GROUP_KEY);
    }

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.DISMISSED_STATE);
    notification.addAction(com.wizardsofm.deskclock.R.drawable.ic_alarm_off_24dp,
            context.getString(com.wizardsofm.deskclock.R.string.alarm_alert_dismiss_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());
    updateAlarmGroupNotification(context);
}