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:at.software2014.trackme.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mServerInterface = new ServerComm();

    registerUserAtFirstLaunch();//from w w  w . j a v  a  2s .  c o m

    SharedPreferences prefs = getSharedPreferences(TRACK_ME_PREFERENCES, MODE_PRIVATE);
    mEMail = prefs.getString("email", "");
    mName = prefs.getString("first_name", "");

    mDisableServerComm = getDisableServerComm();
    mDisableLocation = getDisableLocationServices();

    mContacts = new ArrayList<ContactEntry>();
    mRegisteredUsers = new ArrayList<ContactEntry>();

    mLocationRequest = LocationRequest.create();
    mLocationClient = new LocationClient(this, this, this);

    setLocationPriority(true, false);

    mTitle = getTitle();
    mMenuTitles = getResources().getStringArray(R.array.navigation_menu);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLinear = (LinearLayout) findViewById(R.id.drawer_linear_layout);
    mDrawerText = (TextView) findViewById(R.id.left_drawer_text);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    mDrawerText.setText(mName);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mMenuTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    //if (savedInstanceState == null) {
    //   if(isGooglePlayServicesConnected()){            
    //      selectItem(0, "");
    //   }
    //}

    if (mDisableLocation == false) {
        Intent intent = new Intent(this, LocationUpdatesIntentService.class);
        intent.putExtra("eMail", mEMail);
        mLocationUpdatesPendingIntent = PendingIntent.getService(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

}

From source file:com.android.fpuna.activityrecognition.MainActivity.java

/**
 * Gets a PendingIntent to be sent for each activity detection.
 */// w w w .  ja  v  a2  s  .c  o  m
private PendingIntent getActivityDetectionPendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mActivityDetectionPendingIntent != null) {
        return mActivityDetectionPendingIntent;
    }
    Intent intent = new Intent(this, DetectedActivitiesIntentService.class);

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

From source file:com.devbrackets.android.exomedia.EMLockScreen.java

/**
 * Creates a PendingIntent for the given action to the specified service
 *
 * @param action The action to use//www . j  a v  a2  s.  co m
 * @param serviceClass The service class to notify of intents
 * @return The resulting PendingIntent
 */
private PendingIntent createPendingIntent(String action, Class<? extends Service> serviceClass) {
    Intent intent = new Intent(context, serviceClass);
    intent.setAction(action);

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

From source file:com.kiandastream.musicplayer.MusicService.java

public void setListeners(RemoteViews view, String songname) {
    //listener 1/*from   ww  w  . j a va2  s  .c o  m*/
    view.setTextViewText(R.id.songname, songname);
    PendingIntent backword = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_REWIND), 0);
    view.setOnClickPendingIntent(R.id.backword, backword);

    //listener 2
    if (mState == State.Playing) {

        PendingIntent playpause = PendingIntent.getService(this, 1, new Intent(MusicService.ACTION_PAUSE), 0);
        view.setOnClickPendingIntent(R.id.play, playpause);
        view.setImageViewResource(R.id.play, R.drawable.pause);
    } else {
        if (mState == State.Paused) {
            PendingIntent playpause = PendingIntent.getService(this, 1, new Intent(MusicService.ACTION_RESUME),
                    0);
            view.setOnClickPendingIntent(R.id.play, playpause);
            view.setImageViewResource(R.id.play, R.drawable.play);
        }

    }
    //listener 3

    PendingIntent forward = PendingIntent.getService(this, 2, new Intent(MusicService.ACTION_NEXT), 0);
    view.setOnClickPendingIntent(R.id.forward, forward);

    //listener 4

    PendingIntent close = PendingIntent.getService(this, 3, new Intent(MusicService.ACTION_STOP), 0);
    view.setOnClickPendingIntent(R.id.close, close);
}

From source file:com.juick.android.XMPPMessageReceiver.java

public static void updateInfo(final Context context, int nMessages, boolean silent) {
    final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String tickerText = "juick: new message";
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    // public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }
    // Notification notif = new Notification(R.drawable.juick_message_icon, null,System.currentTimeMillis());

    int iconIndex;
    if (nMessages < 1) {
        iconIndex = 0; // to prevent out of bounds
    } else if (nMessages > NOTIFICATION_ICONS.length - 1) {
        iconIndex = NOTIFICATION_ICONS.length - 1; // to prevent out of bounds
    } else {//  w w w  . java  2 s  . co m
        iconIndex = nMessages;
    }
    boolean showNumberUnread = sp.getBoolean("show_number_unread", true);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(
                    showNumberUnread ? NOTIFICATION_ICONS[iconIndex] : R.drawable.juick_message_icon_plain)
            .setWhen(System.currentTimeMillis());

    //context.getResources().getDrawable(smallIcon)
    // public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }

    int notification = 0;
    if (!silent) {
        if (sp.getBoolean("led_enabled", true))
            notification |= Notification.DEFAULT_LIGHTS;
        if (System.currentTimeMillis() - lastVibrate > 5000) {
            // add some sound
            if (sp.getBoolean("vibration_enabled", true))
                notification |= Notification.DEFAULT_VIBRATE;
            if (sp.getBoolean("ringtone_enabled", true)) {
                String ringtone_uri = sp.getString("ringtone_uri", "");
                if (ringtone_uri.length() > 0) {
                    notificationBuilder.setSound(Uri.parse(ringtone_uri));
                } else
                    notification |= Notification.DEFAULT_SOUND;
            }
            lastVibrate = System.currentTimeMillis();
        }
    }
    notificationBuilder.setDefaults(silent ? 0 : notification);
    Intent intent = new Intent(context, XMPPService.class);
    intent.setAction(XMPPService.ACTION_LAUNCH_MESSAGELIST);
    PendingIntent pendingIntent = PendingIntent.getService(context, 1000, intent, 0);
    //PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, nintent, 0);
    notificationBuilder.setContentTitle("Juick: " + nMessages + " new message" + (nMessages > 1 ? "s" : ""))
            .setContentText(tickerText).setContentIntent(pendingIntent).setNumber(nMessages);
    nm.notify("", 2, notificationBuilder.getNotification());
}

From source file:com.velli.passwordmanager.ApplicationBase.java

public static void showLogoutNotification() {
    final Intent logout = new Intent(getAppContext(), IntentServiceLogOut.class);

    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getAppContext())
            .setSmallIcon(R.drawable.ic_action_fingerprint)
            .setContentTitle(getAppContext().getString(R.string.notification_log_out_title))
            .setContentText(getAppContext().getString(R.string.notification_log_out_content))
            .setColor(getAppContext().getResources().getColor(R.color.color_primary_500)).setShowWhen(false)
            .setContentIntent(// w w w  .  j a  v  a2 s .co m
                    PendingIntent.getService(getAppContext(), -1, logout, PendingIntent.FLAG_ONE_SHOT));

    NotificationManager notificationManager = (NotificationManager) getAppContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

From source file:com.andrew.apollo.NotificationHelper.java

/**
 * @param which Which {@link PendingIntent} to return
 * @return A {@link PendingIntent} ready to control playback
 *///  w  ww. j a  v  a  2  s. com
private final PendingIntent retreivePlaybackActions(final int which) {
    Intent action;
    PendingIntent pendingIntent;
    final ComponentName serviceName = new ComponentName(mService, MusicPlaybackService.class);
    switch (which) {
    case 1:
        // Play and pause
        action = new Intent(MusicPlaybackService.TOGGLEPAUSE_ACTION);
        action.setComponent(serviceName);
        pendingIntent = PendingIntent.getService(mService, 1, action, 0);
        return pendingIntent;
    case 2:
        // Skip tracks
        action = new Intent(MusicPlaybackService.NEXT_ACTION);
        action.setComponent(serviceName);
        pendingIntent = PendingIntent.getService(mService, 2, action, 0);
        return pendingIntent;
    case 3:
        // Previous tracks
        action = new Intent(MusicPlaybackService.PREVIOUS_ACTION);
        action.setComponent(serviceName);
        pendingIntent = PendingIntent.getService(mService, 3, action, 0);
        return pendingIntent;
    case 4:
        // Stop and collapse the notification
        action = new Intent(MusicPlaybackService.STOP_ACTION);
        action.setComponent(serviceName);
        pendingIntent = PendingIntent.getService(mService, 4, action, 0);
        return pendingIntent;
    default:
        break;
    }
    return null;
}

From source file:com.hmsoft.weargoproremote.services.WearMessageHandlerService.java

void updateNotification(String contentText) {
    if (mNotificationBuilder == null) {
        Context context = getApplicationContext();
        Intent activityIntent = new Intent(context, MobileMainActivity.class);
        activityIntent.setAction(Intent.ACTION_MAIN);
        activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK);

        Intent stopIntent = new Intent(context, WearMessageHandlerService.class);
        stopIntent.setAction(ACTION_STOP);

        mNotificationBuilder = (new Builder(this)).setSmallIcon(R.drawable.icon_notification)
                .setContentTitle(getString(R.string.notification_tittle))
                .setContentIntent(PendingIntent.getActivity(context, 0, activityIntent, 0)).setLocalOnly(true)
                .addAction(R.drawable.icon_power, getString(R.string.action_stop),
                        PendingIntent.getService(context, 0, stopIntent, 0));
    }//from  w ww.j  a v  a2  s  .  c o m

    mNotificationBuilder.setContentText(contentText);
    startForeground(1, mNotificationBuilder.build());
}

From source file:com.poloure.simplerss.FeedsActivity.java

private void setServiceIntent(int state) {
    // Load the ManageFeedsRefresh boolean value from settings.
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    if (!pref.getBoolean("refreshing_enabled", false) && ALARM_SERVICE_START == state) {
        return;//from w ww  .j a  va2s  .co m
    }

    // Create intent, turn into pending intent, and get the alarm manager.
    Intent intent = new Intent(this, ServiceUpdate.class);

    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Depending on the state string, start or stop the service.
    if (ALARM_SERVICE_START == state) {
        String intervalString = pref.getString("refresh_interval", "120");

        long interval = Long.parseLong(intervalString) * MINUTE_VALUE;
        long next = System.currentTimeMillis() + interval;
        am.setRepeating(AlarmManager.RTC_WAKEUP, next, interval, pendingIntent);
    } else if (ALARM_SERVICE_STOP == state) {
        am.cancel(pendingIntent);
    }
}

From source file:com.perm.DoomPlay.PlayingService.java

private RemoteViews getNotifViews(int layoutId) {
    RemoteViews views = new RemoteViews(getPackageName(), layoutId);

    Audio audio = audios.get(indexCurrentTrack);

    views.setTextViewText(R.id.notifTitle, audio.getTitle());
    views.setTextViewText(R.id.notifArtist, audio.getArtist());

    Bitmap cover = AlbumArtGetter.getBitmapFromStore(audio.getAid(), this);

    if (cover == null) {
        Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover);
        views.setImageViewBitmap(R.id.notifAlbum, tempBitmap);
        tempBitmap.recycle();/*from  w  w w .j ava2 s. c  o m*/
    } else {
        //TODO: java.lang.IllegalArgumentException: RemoteViews for widget update exceeds
        // maximum bitmap memory usage (used: 3240000, max: 2304000)
        // The total memory cannot exceed that required to fill the device's screen once
        try {
            views.setImageViewBitmap(R.id.notifAlbum, cover);
        } catch (IllegalArgumentException e) {
            Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover);
            views.setImageViewBitmap(R.id.notifAlbum, tempBitmap);
            tempBitmap.recycle();
        } finally {
            cover.recycle();
        }
    }

    views.setImageViewResource(R.id.notifPlay, isPlaying ? R.drawable.widget_pause : R.drawable.widget_play);

    ComponentName componentName = new ComponentName(this, PlayingService.class);

    Intent intentPlay = new Intent(actionPlay);
    intentPlay.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifPlay, PendingIntent.getService(this, 0, intentPlay, 0));

    Intent intentNext = new Intent(actionNext);
    intentNext.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifNext, PendingIntent.getService(this, 0, intentNext, 0));

    Intent intentPrevious = new Intent(actionPrevious);
    intentPrevious.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifPrevious, PendingIntent.getService(this, 0, intentPrevious, 0));

    Intent intentClose = new Intent(actionClose);
    intentClose.setComponent(componentName);
    views.setOnClickPendingIntent(R.id.notifClose, PendingIntent.getService(this, 0, intentClose, 0));

    return views;
}