【问题标题】:Intent.createChooser() is not showing Whatsapp and TwitterIntent.createChooser() 没有显示 Whatsapp 和 Twitter
【发布时间】:2021-09-15 10:26:17
【问题描述】:

我必须将图像分享到 Facebook、Whatsapp 和 Twitter,但是当只分享 Facebook 时,其他应用程序不来。上个月它正在工作,但突然停止工作。我犯了什么错误?

    //get file uri
    Uri myImageFileUri = FileProvider.getUriForFile(this,
            getApplicationContext().getPackageName() + ".provider", file);

    //create a intent for facebook, twitter, whatsapp
    List<Intent> intentShareList = new ArrayList<Intent>();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("image/png");
    List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
    for (ResolveInfo resInfo : resolveInfoList) {
        String packageName = resInfo.activityInfo.packageName;
        String name = resInfo.activityInfo.name;
        if (packageName.contains("com.facebook") ||
                packageName.contains("com.twitter.android") || packageName.contains("com.whatsapp")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(Intent.EXTRA_STREAM, myImageFileUri);
            intent.setType("image/png");
            intentShareList.add(intent);
        }
    }

    if (intentShareList.isEmpty()) {
        // no apps install
    } else {
        Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share with");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    }

intentShareList 三个应用程序都已添加,但共享底页已打开,只有 Facebook 应用程序存在。

【问题讨论】:

  • Intent.createChooser is not showing whatsapp and twitter 我认为 createChooser() 无关紧要,您可以尝试不测试。但是你没有提到Intent.ACTION_SEND是不好的。
  • 或者你是说packageName.contains("com.whatsapp")返回false?
  • @blackapps 它不会在该列表中返回错误的 intentShareList,它正在被添加,但在显示时它不存在。
  • intentShareList.add(intent); 做一个测试,你使用startActivity(intent); 只用于whatsapp。所用设备的安卓版本?你有版本更新吗?
  • 您是否在清单中添加了 &lt;queries&gt; 元素来处理 Android 11+ 的包可见性规则?

标签: android android-studio share


【解决方案1】:

Android 11 及更高版本需要 &lt;queries&gt; 元素来显示包。您必须在应用的清单中定义它,并使用您想要与之交互的每个包名称。

<manifest>
    <queries>
        <package android:name="com.twitter.android" />
        <package android:name="com.facebook.orca" />
        <package android:name="com.whatsapp" />
    </queries>
...
...
</manifest>

【讨论】:

  • 在清单中添加以上行后仍然无法正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 2018-01-18
  • 1970-01-01
相关资源
最近更新 更多