1

I got an IntentService that does some long running work synchronously within onHandleIntent(Intent). So I'm showing a Notification for as long as the service runs. Now I would like to stop the service once the user taps onto the Notification.

So here's what I got. A broadcast-receiver class that is registered once the service is created. A notification with a pending-intent that is shown when the service does some work.

But somehow I never get the broadcast intent when the user taps onto the notification. Any idea?

Here's part of the code that can easily be re-used for testing.

public class SyncService extends IntentService
        {

    private static final String TAG = SyncService.class.getSimpleName();
    private static final int NOTIFICATION_REF_ID = 1;

    private static final String ACTION_CANCEL = TAG + ".CANCEL";
    private CancelReceiver receiver;

    public SyncService() {
        super(SyncService.class.getSimpleName());
    }

    private class CancelReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            stopSelf();
        }
    }

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

        receiver = new CancelReceiver();
        IntentFilter filter = new IntentFilter(ACTION_CANCEL);
        registerReceiver(receiver, filter);
    }

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

        if (receiver != null)
            unregisterReceiver(receiver);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Intent cancelIntent = new Intent(this, CancelReceiver.class);
        cancelIntent.setAction(ACTION_CANCEL);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                cancelIntent, 0);

        // create notification and set pending-intent
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("some caption")
                .setContentText("some hint")
                .setTicker("some caption").setSmallIcon(R.drawable.ic_action_refresh)
                .setOngoing(true).setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent).setProgress(0, 0, true);

        Notification notification;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
            notification = builder.getNotification();
        else
            notification = builder.build();

        // show notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID,
                notification);


        // now do some long running work synchronously


        // cancel/remove notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID);
    }
}

The service is started from within my activity:

        Intent syncIntent = new Intent(this, SyncService.class);
        startService(syncIntent);

The idea to stop the service like that comes from this post.

raiym
  • 1,439
  • 1
  • 28
  • 54
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • Do not use two notification managers. – greenapps Jul 16 '14 at 16:31
  • If the user taps the notificstion, does it disappear? – greenapps Jul 16 '14 at 16:40
  • An IntentService automatically calls stopSelf() automatically when it has completed its onHandleIntent() work. I'm wondering if onDestroy() is unregistering your receiver before it gets a chance to broadcast. I would place some log statements in each of the life-cycle methods to confirm the order things are being called. – RoraΖ Jul 16 '14 at 18:56
  • The service is still running when I tap the notification. The notification is not going away when I tap it. Good idea of using logCat. I will check that. – Matthias Jul 17 '14 at 06:51

1 Answers1

1

Change

Intent cancelIntent = new Intent(this, CancelReceiver.class);

to

Intent cancelIntent = new Intent();

although onReceive() will be called which calls stopSelf() which invokes onDestroy() the planned work will go on until finished. To handle this add to the sync class

private boolean cancelled = false;

set to true in onReceive and in your 'some long running work synchronously' check regularly cancelled and break out.

greenapps
  • 11,154
  • 2
  • 16
  • 19