Android: Unable to stop foreground service

Simply, I want to upload files over the net using a service.

I have created a service with both: binding and startService. I create a foreground service that displays the progress.

The issue is, after the upload is complete, I call stopForeground(true) and then stopSelf but the notification is not removed and (probably) the service is not killed.

Code of the service class:

public class UploaderService extends Service {

    // ...

    public void finishUpload(File audioFile, File eventsData) {
        LogWrapper.d(TAG, "finishUpload");
        mUploadHelper.upload(audioFile, eventsData)
        // consider this just a callback:
                .subscribe(uuid -> {
                    // Log is printed but service is not stopped
                    LogWrapper.d(TAG, "finishUpload.subscribe");
                }, LogWrapper::fatalError);

    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationCompat.Builder builder = NotificationsManager.getDefaultBuilder(this)
                .setProgress(0, 0, true)
                .setContentTitle("Uploading data to server");
        // first creating a notification with uuid,
        // after POST /post call, post.uid will be used.

    public IBinder onBind(Intent intent) {
        LogWrapper.d(TAG, "onBind " + intent);
        return mUploaderBinder;

    public boolean onUnbind(Intent intent) {
        LogWrapper.d(TAG, "onUnbind " + intent);
        intent.putExtra(EXTRA_TOPIC_NAME, mUploadHelper.getTopic().name);
        return false;

    public void onCreate() {
        mUploaderBinder = new UploaderBinder();

    public class UploaderBinder extends Binder {
        public UploaderService getService() {
            return UploaderService.this;


Though I could not find it in docs, the startService command should be called from outside the service i.e. from an Activity. I called it from my activity just before binding to it and it worked as expected.

I was probably experiencing an undefined behavior- the service was starting but not stopping. I wish someone could give me a better explanation of why that was happening, and that this indeed is the correct way to do stuff.

Great thanks to @pskink for pointing me in the right direction.

Need Your Help

Range.Find.Execute - Forward only misbehave

c# .net ms-word office-interop

I'm using the .Net libraries for Microsoft Word 2010 interoping

How to I convert html data in table to a input form?

php jquery

I have a table that is passed via ajax and displayed. When the code is passed, it also passed all the javascript associated with the pass, as the code doesn't seem to work if I just put it in the p...