Android 11:启动另一个应用程序的服务 [英] Android 11: starting a service of another app
问题描述
第一个应用有一个服务:
First app has a service:
<service
android:name="com.example.app.service.MyService"
android:exported="true">
<intent-filter>
<action android:name="com.example.app.START_MY_SERVICE" />
</intent-filter>
</service>
另一个应用程序使用(3 种可能的方法)启动第一个应用程序的服务:
Another app starts a service of the first app using (3 possible methods):
1:
val i = Intent("com.example.app.START_MY_SERVICE").apply {
setPackage("com.example.app")
}
startService(i)
2:
val i = Intent().apply {
component = ComponentName("com.example.app", "com.example.app.service.MyService")
}
startService(i)
3:
val i = Intent().apply {
setClassName("com.example.app", "com.example.app.service.MyService")
}
startService(i)
从 23 API (6 Android) 到 29 API (10 Android),所有这些启动另一个应用程序服务的方法都适用
All these methods to start a service of another app work from 23 API (6 Android) to 29 API (10 Android)
在 Android 11 (30 API) 上它不起作用,服务没有启动,也不例外:
On Android 11 (30 API) it doesn't work, a service doesn't start, no exception:
使用 2-3 种方法时,在 Logcat 中打印:
When using 2-3 methods, in Logcat it prints:
W/ActivityManager:无法启动服务 Intent { cmp=com.example.app.service/.service.MyService } U=0:未找到
对于 1 个方法没有任何反应,Logcat 没有消息
For 1 method nothing happens at all, no message at Logcat
那么我们如何在 Android 11 上从另一个应用启动某个应用的服务?
So how can we start a service of some app from another app on Android 11?
推荐答案
启动另一个应用程序服务的应用程序必须在清单中包含下一个声明:
The app that starts a service of another app must include the next declaration in manifest:
<queries>
<package android:name="com.example.anotherapp" />
</queries>
或
(Automate 和 Tasker 等应用拥有此权限)
(apps like Automate and Tasker have this permission)
感谢 CommonsWare Android 11:启动另一个服务应用
Thanks to CommonsWare Android 11: starting a service of another app
这篇关于Android 11:启动另一个应用程序的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!