为什么在 Android Studio 中同时使用 startService 和 bindService 启动服务?
Why is a service launched with both startService and bindService in Android Studio?
以下代码来自project.
在我看来,服务是使用 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;
}
...
}
下面是一些说明 startService
和 bindService
可以一起使用的原因:
https://developer.android.com/guide/components/bound-services#bind-started-service
它说:
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()
.
之后:
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.
以下代码来自project.
在我看来,服务是使用 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;
}
...
}
下面是一些说明 startService
和 bindService
可以一起使用的原因:
https://developer.android.com/guide/components/bound-services#bind-started-service
它说:
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 callingbindService()
.
之后:
Although you usually implement either
onBind()
oronStartCommand()
, 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.