为什么在Android Studio中使用startService和bindService同时启动一个服务?

huangapple go评论75阅读模式
英文:

Why is a service launched with both startService and bindService in Android Studio?

问题

以下代码来自项目

在我看来,一个服务可以使用startService或者bindService来启动。

但是以下代码同时使用了startServicebindService,为什么呢?

public class RecordViewModel extends AndroidViewModel {

    public void connectService(Intent intent) {
        getApplication().startService(intent);
        getApplication().bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }
    
    ...
}
   
public class RecordingService extends Service {

    public class LocalBinder extends Binder {
        public RecordingService getService() {
            return RecordingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStartCommandCalls++;
        return START_NOT_STICKY;
    }

    ...
}
英文:

The following code is from the project.

In my mind, a service is launched using either startService or bindService.

But the following code use both startService and bindService, why?

public class RecordViewModel extends AndroidViewModel {

    public void connectService(Intent intent) {
        getApplication().startService(intent);
        getApplication().bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }
    
    ...
}
   

public class RecordingService extends Service {

    public class LocalBinder extends Binder {
        public RecordingService getService() {
            return RecordingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStartCommandCalls++;
        return START_NOT_STICKY;
    }

    ...
}

答案1

得分: 1

以下是翻译好的部分:

这里是关于为什么可以同时使用startServicebindService的一些说明:

https://developer.android.com/guide/components/bound-services#bind-started-service

其中提到:

> 正如在服务文档中讨论的那样,您可以创建既是启动型服务又是绑定型服务。也就是说,您可以通过调用startService()来启动服务,使服务可以无限期运行,还可以通过调用bindService()允许客户端绑定到服务。

之后:

> 尽管通常会实现onBind()onStartCommand()之一,但有时需要两者都实现。例如,音乐播放器可能会发现允许其服务无限期运行并提供绑定是有用的。这样,活动可以启动服务以播放音乐,即使用户离开应用程序,音乐仍会继续播放。然后,当用户返回应用程序时,活动可以绑定到服务以重新控制播放。

英文:

Here is some description why startService and bindService could be used together:

https://developer.android.com/guide/components/bound-services#bind-started-service

It says:

> As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService(), which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService().

After that:

> Although you usually implement either onBind() or onStartCommand(), it's sometimes necessary to implement both. For example, a music player might find it useful to allow its service to run indefinitely and also provide binding. This way, an activity can start the service to play some music and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the activity can bind to the service to regain control of playback.

huangapple
  • 本文由 发表于 2020年9月28日 09:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64094922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定