Reputation: 12135
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>
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)
All these methods to start a service of another app work from 23 API (6 Android) to 29 API (10 Android)
On Android 11 (30 API) it doesn't work, a service doesn't start, no exception:
When using 2-3 methods, in Logcat it prints:
W/ActivityManager: Unable to start service Intent { cmp=com.example.app.service/.service.MyService } U=0: not found
For 1 method nothing happens at all, no message at Logcat
So how can we start a service of some app from another app on Android 11?
Upvotes: 6
Views: 4829
Reputation: 5064
I also was looking how to open exprnal app, when I can't get info is this app installed on phone or not. You can try to startActivity() and handle ActivityNotFoundException, if it's present, that mean that app is not installed on the phone or can't open links like this. And also there are intent flags which can also make ActivityNotFoundException in case intent starts browser or pick activity dialog: FLAG_ACTIVITY_REQUIRE_NON_BROWSER, FLAG_ACTIVITY_REQUIRE_DEFAULT
Here is more official and other info on this case:
Open external apps in Android:
https://developer.android.com/training/package-visibility/use-cases
https://developer.android.com/training/package-visibility
https://developer.android.com/training/basics/intents
Tells when we can use QUERY_ALL_PACKAGES permission: https://support.google.com/googleplay/android-developer/answer/10158779
https://developer.android.com/training/package-visibility/use-cases
https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9
https://pretagteam.com/question/android-11-sdk-30-launch-external-app-doesnt-work
Upvotes: 1
Reputation: 12135
The app that starts a service of another app must include the next declaration in manifest:
<queries>
<package android:name="com.example.anotherapp" />
</queries>
or
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
(apps like Automate and Tasker have this permission)
Thanks to CommonsWare Android 11: starting a service of another app
Upvotes: 6