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.google.samples.apps.sergio.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from w w w.  ja  v  a  2  s  . c  o m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.google.samples.apps.sergio")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java

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

    String label = instance.mLabel;
    String alarmTime = AlarmUtils.getFormattedTime(context, instance.getAlarmTime());
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context).setShowWhen(false)
            .setContentTitle(context.getString(R.string.alarm_missed_title))
            .setContentText(instance.mLabel.isEmpty() ? alarmTime
                    : context.getString(R.string.alarm_missed_text, alarmTime, label))
            .setColor(ContextCompat.getColor(context, R.color.default_background))
            .setSortKey(createSortKey(instance)).setSmallIcon(R.drawable.stat_notify_alarm)
            .setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setLocalOnly(true);

    if (Utils.isNOrLater()) {
        notification.setGroup(MISSED_GROUP_KEY);
    }/*from  w  ww . j  ava 2s  . co m*/

    final int hashCode = instance.hashCode();

    // Setup dismiss intent
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.DISMISSED_STATE);
    notification.setDeleteIntent(
            PendingIntent.getService(context, hashCode, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content intent
    Intent showAndDismiss = AlarmInstance.createIntent(context, AlarmStateManager.class, instance.mId);
    showAndDismiss.putExtra(EXTRA_NOTIFICATION_ID, hashCode);
    showAndDismiss.setAction(AlarmStateManager.SHOW_AND_DISMISS_ALARM_ACTION);
    notification.setContentIntent(
            PendingIntent.getBroadcast(context, hashCode, showAndDismiss, PendingIntent.FLAG_UPDATE_CURRENT));

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

From source file:com.google.samples.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);/*from   w w  w.java2 s  .  co m*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.google.samples.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.razza.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);/* w  ww .j av  a2 s  . c o  m*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LogUtils.LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LogUtils.LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(
            new Uri.Builder().authority("com.razza.apps.iosched").path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LogUtils.LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LogUtils.LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LogUtils.LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LogUtils.LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:de.androidbytes.adbconnect.presentation.services.WirelessAdbManagingService.java

private void showNotification(ConnectionInformation connectionInformation) {

    if (PreferenceUtility.shouldShowNotification(getApplicationContext())) {

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

        builder.setSmallIcon(R.drawable.ic_adb_white_24dp);
        builder.setColor(ContextCompat.getColor(this.getApplicationContext(), R.color.colorAccent));

        Intent intent = new Intent(this.getApplicationContext(), MainScreenActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(pendingIntent);

        builder.setContentTitle(getResources().getString(R.string.app_name));

        if (isAdbOverWifiActive()) {
            builder.setContentText(getResources().getString(
                    R.string.adb_over_wifi_enabled_execution_commands_information_simple,
                    connectionInformation.getIpAddress(), connectionInformation.getAdbPort()));
        } else {// w w w .  j  a  va 2 s.  co  m
            if (PreferenceUtility.canUseRootPermissions(getApplicationContext())) {
                builder.setContentText(getResources().getString(
                        R.string.adb_over_wifi_disabled_execution_commands_information_notification));
            } else {
                String port = String.valueOf(PreferenceUtility.listenOnDynamicPort(getApplicationContext()));
                builder.setContentText(getResources().getString(
                        R.string.adb_over_wifi_disabled_no_root_execution_commands_information_simple, port));
            }
        }

        if (PreferenceUtility.canUseRootPermissions(getApplicationContext())) {

            int actionIconResId;
            String serviceActionLabel;

            if (isAdbOverWifiActive()) {
                actionIconResId = R.drawable.ic_phonelink_off_white_24dp;
                serviceActionLabel = getApplicationContext().getResources()
                        .getString(R.string.service_action_stop_adb_over_wifi_label);
            } else {
                actionIconResId = R.drawable.ic_phonelink_white_24dp;
                serviceActionLabel = getApplicationContext().getResources()
                        .getString(R.string.service_action_start_adb_over_wifi_label);
            }

            Intent serviceIntent = ServiceUtility.getToggleIntent(getApplicationContext(),
                    !isAdbOverWifiActive());
            PendingIntent serviceActionIntent = PendingIntent.getService(getApplicationContext(), 0,
                    serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.addAction(actionIconResId, serviceActionLabel, serviceActionIntent);
        }

        if (PreferenceUtility.showPersistentNotification(getApplicationContext()) && !(!isAdbOverWifiActive()
                && PreferenceUtility.persistNotificationOnlyOnActiveAdbOverWifi(getApplicationContext()))) {
            builder.setOngoing(true);

            int notificationPriority;
            switch (PreferenceUtility.getNotificationPriority(getApplicationContext())) {
            case 0:
                notificationPriority = NotificationCompat.PRIORITY_MIN;
                break;
            case 1:
                notificationPriority = NotificationCompat.PRIORITY_LOW;
                break;
            case 2:
                notificationPriority = NotificationCompat.PRIORITY_DEFAULT;
                break;
            case 3:
                notificationPriority = NotificationCompat.PRIORITY_HIGH;
                break;
            case 4:
                notificationPriority = NotificationCompat.PRIORITY_MAX;
                break;
            default:
                notificationPriority = NotificationCompat.PRIORITY_DEFAULT;
                break;
            }

            Log.i("Notification Priority: " + notificationPriority);
            builder.setPriority(notificationPriority);
        } else {
            builder.setOngoing(false);
        }

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

From source file:com.google.android.gms.nearby.messages.samples.nearbybackgroundbeacons.MainActivityFragment.java

private PendingIntent getPendingIntent() {
    return PendingIntent.getService(getActivity(), 0, getBackgroundSubscribeServiceIntent(),
            PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.darshancomputing.alockblock.ALockBlockService.java

private void maximize() {
    mNotificationManager.cancelAll();//from ww  w.  ja v a2 s. com
    stopForeground(true);

    Intent mainWindowIntent = new Intent(context, ALockBlockActivity.class);
    mainWindowPendingIntent = PendingIntent.getActivity(context, 0, mainWindowIntent, 0);

    ComponentName comp = new ComponentName(getPackageName(), ALockBlockService.class.getName());
    Intent reEnableIntent = new Intent().setComponent(comp).putExtra(EXTRA_ACTION, ACTION_REENABLE);
    PendingIntent reEnablePendingIntent = PendingIntent.getService(this, 0, reEnableIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder kgunb = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.kg_unlocked)
            .setLargeIcon(largeIconU).setContentTitle("Lock Screen Disabled").setContentText("A Lock Block")
            .setContentIntent(mainWindowPendingIntent).setShowWhen(false).setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_MAX);

    if (settings.getBoolean(SettingsActivity.KEY_REENABLE_FROM_NOTIFICATION, false))
        kgunb.addAction(R.drawable.ic_menu_login, "Re-enable", reEnablePendingIntent);

    startForeground(NOTIFICATION_KG_UNLOCKED, kgunb.build());
}

From source file:com.saarang.samples.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from  w  ww  .  j ava 2 s .  c om
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LogUtils.LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LogUtils.LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.saarang.samples.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LogUtils.LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LogUtils.LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LogUtils.LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LogUtils.LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.remdo.app.MainActivity.java

/**
 * Enables Alerts service with configured minutes span in Services database
 */// www.  j  a v a 2  s  .co  m
private void enableNotificationsService() {
    mAlertsInterval = dm.getServcieMinutes("Alerts");
    long milisegundos = mAlertsInterval * 60 * 1000;

    Intent intent = new Intent(this, NotificationService.class);
    pendingAlertsIntent = PendingIntent.getService(this, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), milisegundos,
            pendingAlertsIntent);

    Toast.makeText(this, getString(R.string.started_alerts_service), Toast.LENGTH_LONG).show();
}