绑定到已由 BroadcastReceiver 启动的服务会创建新的服务实例

发布于 2024-11-07 09:09:14 字数 631 浏览 0 评论 0原文

我的应用程序使用由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2024-11-14 09:09:15

我可以向你保证,事实并非如此。某一特定服务组件每次只有一个实例处于活动状态,无论您启动或绑定到该组件多少次。此外,该服务仅在一个进程中运行。没有办法让它在不同的时间在不同的进程中运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文