英文:
Why is a service launched with both startService and bindService in Android Studio?
问题
以下代码来自项目。
在我看来,一个服务可以使用startService
或者bindService
来启动。
但是以下代码同时使用了startService
和bindService
,为什么呢?
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
以下是翻译好的部分:
这里是关于为什么可以同时使用startService
和bindService
的一些说明:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论