绑定到已由 BroadcastReceiver 启动的服务会创建新的服务实例
我的应用程序使用由 BOOT_COMPLETE BroadcastReceiver 启动的服务,如下所示:
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
}
如果在我的应用程序的活动中,我尝试像这样绑定到此服务:
getApplicationContext().bindService(new Intent(getApplicationContext(), MyService.class),
_serviceConnection, Context.BIND_AUTO_CREATE);
创建该服务的新实例,尽管第一个实例(由BroadcastReceiver)仍在运行。我在服务的 onCreate() 方法中记录了进程 ID,第一个 服务与活动在不同的进程中运行,第二个实例与活动在同一进程中创建。我尝试在清单的服务元素中设置 android:process 参数(均带前导 : 和不带前导 : ),但结果保持不变。
如何绑定到在不同进程中运行的服务,而不是在活动运行的进程中创建新实例?
My Application uses a service that is started by a BOOT_COMPLETE BroadcastReceiver like this:
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
}
If, in an activity in my application, I try to bind to this service like this:
getApplicationContext().bindService(new Intent(getApplicationContext(), MyService.class),
_serviceConnection, Context.BIND_AUTO_CREATE);
a new Instance of the Service is created, allthough the first instance (created by the BroadcastReceiver) is still runnning. I logged the process IDs in the onCreate() method of the service and the first
service runs in a different process than the activity, the second instance is created in the same process as the activity. I tried setting a android:process argument in the service element of the manifest (both with leading : and without), but the result stays the same.
How do I bind to the service running in a different process instead of creating a new instance in the process the activity is running in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以向你保证,事实并非如此。某一特定服务组件每次只有一个实例处于活动状态,无论您启动或绑定到该组件多少次。此外,该服务仅在一个进程中运行。没有办法让它在不同的时间在不同的进程中运行。
I can assure you, it does not. Only one instance of a particular service component is ever active at a time, no matter how many times you start or bind to it. Further, the service will only run in one process. There is no way for it to run in different processes at different times.