intent.resolveActivity returns null in API 30
This appears to be due to the new restrictions on "package visibility" introduced in Android 11.
Basically, starting with API level 30, if you're targeting that version or higher, your app cannot see, or directly interact with, most external packages without explicitly requesting allowance, either through a blanket QUERY_ALL_PACKAGES
permission, or by including an appropriate <queries>
element in your manifest.
Indeed, your first snippet works as expected with that permission, or with an appropriate <queries>
element in the manifest; for example:
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
The information currently available isn't terribly specific, but it does state:
The
PackageManager
methods that return results about other apps, such asqueryIntentActivities()
, are filtered based on the calling app's<queries>
declaration
Though your example is using an Intent
method – i.e., resolveActivityInfo()
– that's actually calling PackageManager
"query" methods internally. An exhaustive list of every method and functionality affected by this change might not be feasible, but it's probably safe to assume that if PackageManager
is involved, you might do well to check its behavior with the new restrictions.
intent.resolveActivity returns null on android.intent.action.OPEN_DOCUMENT in API 30
in Android 11 were made some changes in package visbility, some info HERE and HERE
note that resolveActivity
should return package name of app, which can handle this intent. Thats against freshly introduced rules and new policy as above
instead try to run startActivityForResult
strictly without your if
and wrap it in try { ... } catch (ActivityNotFoundException ex){}
boolean canBeLaunched;
try {
fragment.startActivityForResult(intent, requestCode);
canBeLaunched = true
} catch (ActivityNotFoundException ex){
canBeLaunched = false
}
intent.resolveActivity != null but launching the intent throws an ActivityNotFound exception
From what i could see, intent.resolveActivity() will return true if there is ANY activity resolving this intent. Even if this activity is not exported (which makes it unusable for all practical purposes in case it's not from your package).
Android's API doesn't care to mention that, so you have to figure it out by yourself, and make sure that the activity you're trying to launch is indeed exported.
ActivityInfo activityInfo = intent.resolveActivityInfo(pm, intent.getFlags());
if (activityInfo.exported) {
doSomething();
}
EDIT: the point of this question is that ResolveActivity will return a componentName even if activityInfo.exported==false AND it's not from your own package - which makes it unlaunchable, and surprised me cause the intent was resolved and yet unlaunchable.
intent.resolveActivity() returning null for ACTION_SEND intent
I found a simple fix that works like a charm:
fun composeMmsMessage() {
val intent = Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "+2340000000000", null)).apply {
putExtra("sms_body", "Hi there!")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
Why is the Android resolveactivity of an intent returning null?
Approximately zero apps ever written honor the obscure android:resource
scheme, which is why this is not working.
I agree with 323go -- use a ContentProvider
, such as FileProvider
. This requires zero lines of Java code to implement on the provider side, as you simply use FileProvider
. It requires a manifest entry, one XML file of metadata, and one line when you call startActivity()
to allow the PDF viewer access to the content. This is more future-proof than is writing to external storage, and it is significantly more securable.
FWIW, I have a sample project demonstrating serving a PDF to an external PDF viewer via a FileProvider
.
Related Topics
Android Remove Space Between Tabs in Tabwidget
How to Scale/Resize Text to Fit a Textview
Firebase Overwrites Signin with Google Account
Debugging SQLite Database on the Device
Android Notification Is Not Showing
How to Customize Item Background and Item Text Color Inside Navigationview
How to Restrict the Edittext to Accept Only Alphanumeric Characters
How to Stop Asynctask Thread in Android
Forcing the Soft Keyboard Open
How to Implement a Listview Without Listactivity? (Use Only Activity)
How to Add "Go Back" Function in Webview Inside Fragment
Getlastknownlocation Always Return Null After I Re-Install the APK File via Eclipse
Android Studio Adb Syntax Error: ")" Unexpected