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

huangapple go评论107阅读模式
英文:

Why does intent.resolveActivity(getPackageManager()) return null even though there is Activity to handle it?

问题

我正在按照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", "无法处理此意图!");
   }
}

问题在于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

英文:

I'm following the Android Codelabs, specifically I'm working on this Codelab with implicit intents.

The Codelab has the following method:

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(&quot;ImplicitIntents&quot;, &quot;Can&#39;t handle this intent!&quot;);
   }
}

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.

I'm wondering why intent.resolveActivity(getPackageManager()) returns null, even thought the Uri can be opened in Google Chrome?

The Manifest file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.implicitintents&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

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.

答案1

得分: 5

如果你还没有解决这个问题,我不知道你是否在使用API级别30,但如果是的话,你应该检查这个新的包可见性限制。包可见性已经发生了变化,应用程序不再能够直接访问包管理器。你需要在你的AndroidManifest文件中添加一个<queries>元素。在你的情况下,你需要类似这样的内容。

&lt;queries&gt;
    &lt;intent&gt;
        &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
        &lt;data android:scheme=&quot;https&quot; /&gt;
    &lt;/intent&gt;
&lt;/queries&gt;
英文:

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. Package Visibility has changed and apps are no longer able to access the Package Manager directly. You need to add a <queries> element in your AndroidManifest file. In your case you would need something like this.

&lt;queries&gt;
    &lt;intent&gt;
        &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
        &lt;data android:scheme=&quot;https&quot; /&gt;
    &lt;/intent&gt;
&lt;/queries&gt;

答案2

得分: 2

我遇到了同样的问题。我通过在清单中添加一个请求来使用浏览器来解决它。现在我的清单文件看起来像这样。尝试阅读这篇文章

<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>
</manifest>
英文:

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.

&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.example.android.quakereport&quot;&gt;
&lt;queries&gt;
    &lt;intent&gt;
        &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
        &lt;data android:scheme=&quot;https&quot; /&gt;
    &lt;/intent&gt;
&lt;/queries&gt;
&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/AppTheme&quot;&gt;
    &lt;activity android:name=&quot;.EarthquakeActivity&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;

</manifest>

答案3

得分: 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>
</queries>
  1. 移除:
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");
英文:

A lot of problem for me:

  1. Use queries into Android Manifest

     &lt;queries&gt;
     &lt;!-- WebView --&gt;
     &lt;intent&gt;
         &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
         &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
         &lt;data android:scheme=&quot;http&quot; /&gt;
     &lt;/intent&gt;
     &lt;intent&gt;
         &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
         &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
         &lt;data android:scheme=&quot;https&quot; /&gt;
     &lt;/intent&gt;
    
     &lt;!-- Camera --&gt;
     &lt;intent&gt;
         &lt;action android:name=&quot;android.media.action.IMAGE_CAPTURE&quot; /&gt;
     &lt;/intent&gt;
    
     &lt;!-- Gallery --&gt;
     &lt;intent&gt;
         &lt;action android:name=&quot;android.intent.action.GET_CONTENT&quot; /&gt;
     &lt;/intent&gt;
    

    </queries>

  1. Remove

     File sd_directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
     return File.createTempFile(new_name, &quot;.jpg&quot;, sd_directory);
    

and use

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

huangapple
  • 本文由 发表于 2020年8月11日 20:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63358146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定