First of all, I have a lib library, which defines a 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>
Now, add two apps that have introduced this library, and then called them in their respective codes:
startService(new Intent(context, SharedService.class))
What I am thinking now is that there should be only one instance of SharedService
in the system, in the process com.lib.aidl.SharedService
.
But the actual situation is that there are two instances of SharedService
, they are both in the process named com.lib.aidl.SharedService
, but the process ids are different . why is that?
Now I want only one instance of SharedService
to appear in the system. When startService
is called for the second time, the onStartCommand
method will be called back. Can this be done? ?
PHP中文网2017-05-16 13:23:35
将
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.