1

I'd like to cancel my service on the user action (i.e a cancel button on the notification) It's working on API < 26, but not on 26+. I'm looking for help to fix this if possible.

Here is my code:

Activity

Intent serviceIntent = new Intent(this, DownloadService.class);
        serviceIntent.putExtra("fileTitle", myFile.name);
        ContextCompat.startForegroundService(this, serviceIntent);

My Service (JobIntentService)

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

        Intent closeIntent = new Intent(this, NotificationReceiver.class);
        closeIntent .setAction("cancel_button")
                    .setFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REPLACE_PENDING);
        PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT);

       nBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(android.R.drawable.stat_sys_download)
                        .setTicker("")
                        .setContentTitle(FILE_TITLE)
                        .setContentText("Download starting")
                        .setProgress(100,0,false)
                        .addAction(android.R.drawable.ic_menu_close_clear_cancel,"Cancel", actionIntent);
}

 @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        if(isPostorEqualOS(Build.VERSION_CODES.O)){
            startForeground(NOTIFICATION_ID, nBuilder.build());
            enqueueWork(this, intent);
        }
        return super.onStartCommand(intent, flags, startId);
    }

My Receiver

@Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if("cancel_button".equals(action)){
            Intent stopService = new Intent(context, DownloadService.class);
            boolean stop = context.stopService(stopService);
        }
    }
Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • What do you mean by "not working"? – greeble31 Nov 29 '19 at 17:02
  • I mean that I see the boolean from my Receiver being called and returning true, but the service isn't going into `onDestroy` method – Biscuit Nov 30 '19 at 08:15
  • How exactly do you know your Service is created & running at the moment you press the button? Your foreground notification is showing, I assume? And, of course, you have not bound to the service? – greeble31 Nov 30 '19 at 13:50
  • I've used this comment https://stackoverflow.com/a/5921190/6074224 to help me figure out, I've called the function after my `context.stopService(stopService)` – Biscuit Dec 01 '19 at 15:03
  • But obviously, the foreground notification is showing, right? And it continues showing after the `stopService()` call? – greeble31 Dec 02 '19 at 15:01

1 Answers1

0

Turns out JobIntentService aren't designed to be cancel.

For more info: https://stackoverflow.com/a/47159305/6074224

I changed my service to IntentService instead and it's working.

Biscuit
  • 4,840
  • 4
  • 26
  • 54