[英]Why does intent.resolveActivity(getPackageManager()) return null even though there is Activity to handle it?
I'm following the Android Codelabs , specifically I'm working on this Codelab with implicit intents.我正在关注Android Codelabs ,特别是我正在以隐含的意图在这个Codelab 上工作。
The Codelab has the following method: Codelab 有以下方法:
public void openWebsite(View view) {
String url = mWebsiteEditText.getText().toString();
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d("ImplicitIntents", "Can't handle this intent!");
}
}
The problem is that the intent.resolveActivity(getPackageManager())
returns null, but if I omit this and just call the startActivity(intent)
, it works fine and opens the Uri in Google Chrome.问题是intent.resolveActivity(getPackageManager())
返回null,但如果我省略它并只调用startActivity(intent)
,它可以正常工作并在 Google Chrome 中打开 Uri。
I'm wondering why intent.resolveActivity(getPackageManager())
returns null, even thought the Uri can be opened in Google Chrome?我想知道为什么intent.resolveActivity(getPackageManager())
返回 null,即使认为 Uri 可以在 Google Chrome 中打开?
The Manifest file:清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.implicitintents">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In this case the URL that we want to open comes from EditText
field, so we can't use an intent-filter
with android:host
as described here .在这种情况下,我们要打开的 URL 来自EditText
字段,因此我们不能使用带有android:host
的intent-filter
,如此处所述。
In case you haven't solve the problem yet.如果您还没有解决问题。 I don't know if you are using API level 30, but if you are you should check this new Package visibility restrictions .我不知道您是否使用的是 API 级别 30,但如果您是,您应该检查这个新的 Package 可见性限制。 Package Visibility has changed and apps are no longer able to access the Package Manager directly. Package 可见性已更改,应用程序不再能够直接访问 Package 管理器。 You need to add a element in your AndroidManifest file.您需要在 AndroidManifest 文件中添加一个元素。 In your case you would need something like this.在你的情况下,你需要这样的东西。
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
I had the same problem.我有同样的问题。 I solved it by adding a request to use the browser in the manifest.我通过在清单中添加使用浏览器的请求来解决它。 Now my manifest file looks like this.现在我的清单文件看起来像这样。 Try reading this article .尝试阅读这篇文章。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.quakereport">
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".EarthquakeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
add permission添加权限
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
then add Queries然后添加查询
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
</queries>
I hope it will solve your problem.我希望它能解决你的问题。 you can also check package visibility use cases您还可以查看package 可见性用例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.