android相关问题 - Android 如何让引入的lib库中的Service在系统中唯一
巴扎黑
巴扎黑 2017-05-16 13:21:35
[Android讨论组]

首先,我有一个lib库,库里面定义了以个Service:

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <service
        android:name=".SharedService"
        android:process="com.lib.aidl.SharedService"
        android:enabled="true"
        android:exported="true">
    </service>
</application>

现在,加入有两个app都引入了这个库,然后在各自的代码里都调用了:

startService(new Intent(context, SharedService.class))

我现在想的是系统中应该只有一个SharedService的实例,在进程com.lib.aidl.SharedService中。

但是实际情况是有两个SharedService的实例,它们都在进程名为com.lib.aidl.SharedService的进程中,但是进程id是不一样的。这是为什么?

我现在想让系统中仅出现一个SharedService的实例,当第二次调用startService时回调onStartCommand方法,这个可以做到么?

巴扎黑
巴扎黑

全部回复(1)
PHP中文网

android:process="com.lib.aidl.SharedService"

替换成

android:process=":com.lib.aidl.SharedService"

试试

注意,加了个 ':'

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The <application> element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号