2
votes

I have an Ongoing notification on my radio app and I already managed to start my main activity from the notification but now I'm trying to add a button to the notification to stop the streaming service.

This is my notifaction method inside the service:

@SuppressWarnings("deprecation")
private void createNotification() {
    int myIconId = R.drawable.ic_pause;
    Intent mIntent = new Intent(context, StreamingService.class);
    Notification notification;
    Bundle bundle = new Bundle();
    bundle.putInt("list", list);
    PendingIntent stopIntent = PendingIntent.getService(context, 0, mIntent, 0) ;
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
            0, new Intent(getApplicationContext(), MainActivity.class)
                    .putExtras(bundle), PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification();
        notification.tickerText = station.getRadioName();
        notification.icon = R.mipmap.ic_launcher;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        notification.setLatestEventInfo(getApplicationContext(),
                station.getRadioName(), null, pi);
    } else {
        notification = new NotificationCompat.Builder(context)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(station.getRadioName())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(
                        BitmapFactory.decodeResource(
                                context.getResources(),
                                R.mipmap.ic_launcher))
                .addAction(myIconId,"STOP", stopIntent)
                .setOngoing(true).setContentIntent(pi).build();
    }

    startForeground(NOTIFICATION_ID, notification);
}

This is my OnStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) {
    task = new ProgressTask();
    task.execute();
    return START_NOT_STICKY;
}

I did the Intent and the PendingIntent to start the service, but how can I set the service to stopSelf() using a PendingIntent? I'm stuck at this for days, thanks in advance for the help.

2
did your try if(intent.getAction().equals("STOP"))stopSelf(); in your onStartCommandJRowan
Thanks man, thats's what I was missing. I can't believe I was stuck at this for days xDWendell Krohling
it happens to the best of us, when you go at it for days it just happens, lol, I put it as answer so you can mark itJRowan
Yeah, this was driving me crazy haha. I marked it, thanks again man :)Wendell Krohling
no problem I'm glad you got it working, happens to me sometimes too hahaJRowan

2 Answers

2
votes

in your OnStartCommand use this

if(intent.getAction().equals("STOP"))
stopSelf();
1
votes

JRowan put me in the right direction, thanks man.

I added this line to my notification method:

mIntent.setAction("STOPME");

And this is my onStartCommand now:

 public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent.getAction()!=null && intent.getAction().equals("STOPME")){
        stopSelf();
        return START_NOT_STICKY;
    }
    else {
        task = new ProgressTask();
        task.execute();
        return START_NOT_STICKY;
    }
}