简体   繁体   English

Android:无法停止前台服务

[英]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 . 我已经创建了一个包含绑定和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. 问题是,上传完成后,我先调用stopForeground(true) ,然后stopSelf但通知没有被删除,并且(可能)服务没有被杀死。

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");
                    stopForeground(true);
                    stopSelf();
                }, LogWrapper::fatalError);
    }

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

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

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

    @Override
    public void onCreate() {
        super.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 ie from an Activity. 虽然我在docs中找不到它,但应该从服务外部(即从活动)调用startService命令。 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. 非常感谢@pskink为我指出了正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM