如何使我的服务只在多用户android系统中的一个实例中运行?

时间:2017-03-28 22:34:33

标签: android android-service android-manifest android-service-binding

我们的Android系统支持多种用户功能。我想从其他应用程序启动我的服务器,例如app1,app2,命令为startService(intent)。 根据谷歌https://source.android.com/devices/tech/admin/multiuser-apps.html的文件。 我需要设置android:singleUser =“true”以确保我的服务只在多用户android系统中的一个实例中运行。但是当我在其他应用程序中启动服务时,我得到了以下异常:

Not allowed to start service Intent { act=com.xx.xxx.action.Start pkg=com.xx.xxx (has extras) } without permission not exported from uid 1000

似乎android:exported =“true”被android禁用:singleUser =“true”。如果我没有添加android:singleUser =“true”,它工作正常,但我的服务有多个实例在后台运行。 我的问题是如何才能使我的服务仅在其他应用程序中使用startService(intent)的单个实例中运行? 我的Manifest.xml配置如下:

    <application
        android:name=".App"
        android:label="@string/app_name"
        android:directBootAware="true"
        android:multiprocess="false"
        android:persistent="true">
        <service
            android:name=".MyService"
            android:singleUser="true"
            android:exported="true"
            android:permission="com.xx.permission.xx">
            <intent-filter>
                <action android:name="com.xx.xx.action.xx" />
            </intent-filter>
        </service>
</Application>

非常感谢。

4 个答案:

答案 0 :(得分:1)

正如您链接的文档所说:&#34;目前只有系统应用可以使用此功能&#34;。如果您的产品可行,请将您的应用设为系统应用。

答案 1 :(得分:0)

是的,需要将app声明为系统应用。也可以将bindServiceAsUser()与System UserHandler一起使用。

答案 2 :(得分:0)

  1. make android:exported and android:singleUser to true
  2. 添加权限INTERACT_ACROSS_USERS
  3. 确保您的应用具有特权。所以你应该制作你的应用系统签名

答案 3 :(得分:0)

您需要INTERACT_ACROSS_USERS_FULL权限才能允许非主要用户绑定到单个实例服务。这是仅签名许可,因此您的应用需要使用平台密钥进行签名。

相关问题