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.example.android.wearable.timer.SetTimerActivity.java

/**
 * Build a notification including different actions and other various setup and return it.
 *
 * @param duration the duration of the timer.
 * @return the notification to display./*from  www .j a v a 2s .com*/
 */

private Notification buildNotification(long duration) {
    // Intent to restart a timer.
    Intent restartIntent = new Intent(Constants.ACTION_RESTART_ALARM, null, this,
            TimerNotificationService.class);
    PendingIntent pendingIntentRestart = PendingIntent.getService(this, 0, restartIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Intent to delete a timer.
    Intent deleteIntent = new Intent(Constants.ACTION_DELETE_ALARM, null, this, TimerNotificationService.class);
    PendingIntent pendingIntentDelete = PendingIntent.getService(this, 0, deleteIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Create countdown notification using a chronometer style.
    return new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_cc_alarm)
            .setContentTitle(getString(R.string.timer_time_left))
            .setContentText(TimerFormat.getTimeString(duration)).setUsesChronometer(true)
            .setWhen(System.currentTimeMillis() + duration)
            .addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_restart), pendingIntentRestart)
            .addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_delete), pendingIntentDelete)
            .setDeleteIntent(pendingIntentDelete).setLocalOnly(true).build();
}

From source file:com.concentricsky.android.khanacademy.app.HomeActivity.java

private void setupRepeatingLibraryUpdateAlarm() {
    Log.d(LOG_TAG, "setupRepeatingLibraryUpdateAlarm");
    AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);

    Intent intent = new Intent(getApplicationContext(), KADataService.class);
    intent.setAction(ACTION_LIBRARY_UPDATE);
    PendingIntent existing = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_RECURRING_LIBRARY_UPDATE, intent, PendingIntent.FLAG_NO_CREATE);
    boolean alreadyScheduled = existing != null;

    if (!alreadyScheduled) {
        // Initial delay.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, UPDATE_DELAY_FROM_FIRST_RUN);

        // Schedule the alarm.
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
                REQUEST_CODE_RECURRING_LIBRARY_UPDATE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Log.d(LOG_TAG, "(re)setting alarm");
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                pendingIntent);//w  ww.  j av a  2s .  c  om
    }
}

From source file:google.geofence.GooglegeofenceModule.java

/**
 * Gets a PendingIntent to send with the request to add or remove Geofences.
 * Location Services issues the Intent inside this PendingIntent whenever a
 * geofence transition occurs for the current list of geofences.
 * //from  w  w w.j  a v a 2s . c  o m
 * @return A PendingIntent for the IntentService that handles geofence
 *         transitions.
 */
@Kroll.method
private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    System.out.println("ATTEMPTING TO start service");

    Intent intent = null;
    try {

        intent = new Intent(appContext, GeofenceTransitionsIntentService.class);
        TiApplication.getInstance().startService(intent);

        System.out.println("geoFenceTransitionsIntentService  Created");

    } catch (Exception ex) {
        System.out.println("Exception caught " + ex);

    }

    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent
    // back when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.google.android.apps.iosched.calendar.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 ww  w.ja  v a2s.com
    final long currentTime = System.currentTimeMillis();

    // If the session is already started, do not schedule system notification.
    if (currentTime > 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 - TEN_MINUTES_MILLIS;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent alarmIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);

    // Setting data to ensure intent's uniqueness for different session start times.
    alarmIntent.setData(new Uri.Builder().authority(ScheduleContract.CONTENT_AUTHORITY)
            .path(String.valueOf(sessionStart)).build());
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime,
            PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT));
}

From source file:com.jerrellmardis.amphitheatre.util.Utils.java

private static boolean isAlarmAlreadySet(Context context, Class c) {
    Intent intent = new Intent(context, c);
    PendingIntent pi = PendingIntent.getService(context, LIBRARY_UPDATE_REQUEST_CODE, intent,
            PendingIntent.FLAG_NO_CREATE);
    return pi != null;
}

From source file:com.google.android.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  ww w .j  a  v a2 s  .co m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > 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;
    }

    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.android.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.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.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.gdgdevfest.android.apps.devfestbcn.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);/*  www. j  ava 2  s .  co m*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > 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;
    }

    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.gdgdevfest.android.apps.devfestbcn")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.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.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:ca.zadrox.dota2esportticker.service.ReminderAlarmService.java

private void scheduleAlarm(final long matchId, final long matchStart) {

    final long currentTime = TimeUtils.getUTCTime();
    LOGD(TAG, "Schedule Alarm");

    if (currentTime > matchStart) {
        LOGD(TAG, "CurrentTime > matchStart");
        return;/*from   w  w  w  . j  a  va 2 s  .  c  o  m*/
    }

    long alarmTime = matchStart - MILLI_TEN_MINUTES;

    final Intent notifIntent = new Intent(ACTION_NOTIFY_MATCH, null, this, ReminderAlarmService.class);

    notifIntent.setData(
            new Uri.Builder().authority("ca.zadrox.dota2esportticker").path(String.valueOf(matchId)).build());

    notifIntent.putExtra(ReminderAlarmService.EXTRA_MATCH_ID, matchId);
    notifIntent.putExtra(ReminderAlarmService.EXTRA_MATCH_START, matchStart);

    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    final AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.nextgis.maplibui.service.TrackerService.java

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

    mHasGPSFix = false;//w  ww  . j  av  a 2  s.c  om

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    IGISApplication application = (IGISApplication) getApplication();
    String authority = application.getAuthority();
    mContentUriTracks = Uri.parse("content://" + authority + "/" + TrackLayer.TABLE_TRACKS);
    mContentUriTrackPoints = Uri.parse("content://" + authority + "/" + TrackLayer.TABLE_TRACKPOINTS);

    mPoint = new GeoPoint();
    mValues = new ContentValues();

    SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + "_preferences",
            Constants.MODE_MULTI_PROCESS);
    mSharedPreferencesTemp = getSharedPreferences(TEMP_PREFERENCES, MODE_PRIVATE);

    String minTimeStr = sharedPreferences.getString(SettingsConstants.KEY_PREF_TRACKS_MIN_TIME, "2");
    String minDistanceStr = sharedPreferences.getString(SettingsConstants.KEY_PREF_TRACKS_MIN_DISTANCE, "10");
    long minTime = Long.parseLong(minTimeStr) * 1000;
    float minDistance = Float.parseFloat(minDistanceStr);

    mTicker = getString(R.string.tracks_running);
    mSmallIcon = R.drawable.ic_action_maps_directions_walk;

    Intent intentSplit = new Intent(this, TrackerService.class);
    intentSplit.setAction(ACTION_SPLIT);
    mSplitService = PendingIntent.getService(this, 0, intentSplit, PendingIntent.FLAG_UPDATE_CURRENT);

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (!PermissionUtil.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            || !PermissionUtil.hasPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION))
        return;

    mLocationManager.addGpsStatusListener(this);

    String provider = LocationManager.GPS_PROVIDER;
    if (mLocationManager.getAllProviders().contains(provider)) {
        mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);

        if (Constants.DEBUG_MODE)
            Log.d(Constants.TAG, "Tracker service request location updates for " + provider);
    }

    provider = LocationManager.NETWORK_PROVIDER;
    if (mLocationManager.getAllProviders().contains(provider)) {
        mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);

        if (Constants.DEBUG_MODE)
            Log.d(Constants.TAG, "Tracker service request location updates for " + provider);
    }

    NotificationHelper.showLocationInfo(this);
}

From source file:com.near.chimerarevo.fragments.SettingsFragment.java

private void setAlarm(int sel, boolean isEnabled) {
    Intent intent = new Intent(getActivity().getApplicationContext(), NewsService.class);
    PendingIntent pintent = PendingIntent.getService(getActivity().getApplicationContext(), 0, intent, 0);
    AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

    alarm.cancel(pintent);//  w  w w .ja v a  2s.  c  o  m

    if (isEnabled) {
        long delay;
        switch (sel) {
        case 0:
            delay = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
            break;
        case 1:
            delay = AlarmManager.INTERVAL_HALF_HOUR;
            break;
        case 2:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        case 3:
            delay = 2 * AlarmManager.INTERVAL_HOUR;
            break;
        case 4:
            delay = 3 * AlarmManager.INTERVAL_HOUR;
            break;
        case 5:
            delay = 6 * AlarmManager.INTERVAL_HOUR;
            break;
        case 6:
            delay = AlarmManager.INTERVAL_HALF_DAY;
            break;
        case 7:
            delay = AlarmManager.INTERVAL_DAY;
            break;
        default:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        }

        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.uptimeMillis(), delay, pintent);
    }
}