为什么intent.resolveActivity(getPackageManager())返回null,尽管已经有处理它的Activity?

4
我正在跟随Android Codelabs,具体来说,我正在使用涉及隐式意图的 Codelab。
该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!");
   }
}

问题在于intent.resolveActivity(getPackageManager())返回了null,但如果我省略这个并直接调用startActivity(intent),它可以正常工作并在Google Chrome中打开Uri。
我想知道为什么intent.resolveActivity(getPackageManager())返回null,即使Uri可以在Google Chrome中打开? 清单文件:
<?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>

在这种情况下,我们想要打开的URL来自于EditText字段,所以我们不能像这里描述的那样使用带有android:hostintent-filter

如果您正在 Android 11 或更高版本上进行测试,则受到包可见性限制的影响。 - CommonsWare
1
@CommonsWare 您是正确的。我在之前的版本中测试过它,它可以工作。我正在寻找如何在Android 11中解决这个问题。 - Αntonis Papadakis
3个回答

7
如果您还没有解决问题。我不知道您是否正在使用API 30,但是如果您正在使用,您应该检查此新的软件包可见性限制。软件包可见性已更改,应用程序不再能够直接访问包管理器。您需要在AndroidManifest文件中添加一个元素。在您的情况下,您需要像这样的东西。
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>

3

我曾经遇到过同样的问题。通过在清单文件中添加使用浏览器的请求,我解决了这个问题。现在我的清单文件看起来像这样。请阅读这篇文章获得更多信息。

<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>

0

对我来说有很多问题:

  1. 在Android清单中使用查询

     <queries>
     <!-- WebView -->
     <intent>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.BROWSABLE" />
         <data android:scheme="http" />
     </intent>
     <intent>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.BROWSABLE" />
         <data android:scheme="https" />
     </intent>
    
     <!-- Camera -->
     <intent>
         <action android:name="android.media.action.IMAGE_CAPTURE" />
     </intent>
    
     <!-- Gallery -->
     <intent>
         <action android:name="android.intent.action.GET_CONTENT" />
     </intent>
    
移除
File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(new_name, ".jpg", sd_directory);

并使用

      File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      return new File(storageDir, imageFileName + ".jpg");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接