如何过滤分享意图Android时显示的应用?

时间:2016-08-20 17:12:06

标签: android android-intent

我知道之前已经问过这个问题并且我引用了其中一个答案,但我无法按照应有的方式运行代码。我有一个简单的笔记记录应用程序,允许用户通过外部应用程序共享笔记。该代码应该过滤应用程序,以便用户只能分享到Facebook,Twitter,mms,gmail和电子邮件(所以我想)。我从最顶层的答案How to filter specific apps for ACTION_SEND intent (and set a different text for each app)开始。当我点击启动方法分享意图的按钮时,我确实得到了mms,gmail,facebook twitter和电子邮件,但我也得到了谷歌驱动器,android梁,evernote,twitter直接消息,facebook messenger和snapchat。

信息正确发送到的唯一应用是mms,电子邮件和Gmail。 Facebook不起作用,下面有一些关于原因的评论,以及我不确定的推特因为我没有帐户来测试它。我没有代码来检查packageName是否包含" drive"或者" googledrive",但我仍然可以分享驾驶,我的笔记中的数据会被传递。我希望能够发送备注文本和备注标题(我的应用程序中有2个editText字段),但我不知道怎么做,因为我不知道包名是什么。

对于无法工作或我想从选项列表中删除的应用,我该如何摆脱它们?这是代码:

public void sendNote(View view) {
    Resources resources = getResources();

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString());
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, noteEditor.getText().toString());
    emailIntent.setType("message/rfc822");

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");

    Intent openInChooser = Intent.createChooser(emailIntent, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if(packageName.contains("android.email")) {
            emailIntent.setPackage(packageName);
        } else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            if(packageName.contains("twitter")) {
                intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
            } else if(packageName.contains("facebook")) {
                // Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
                // One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
                // will show the <meta content ="..."> text from that page with our link in Facebook.
                intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
            } else if(packageName.contains("mms")) {
                intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
            } else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
                intent.putExtra(Intent.EXTRA_TEXT, noteEditor.getText().toString());
                intent.putExtra(Intent.EXTRA_SUBJECT, titleEditor.getText().toString());
                intent.setType("message/rfc822");
            }
            //else if (packageName.contains("drive")) {
                //intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
            //}
            else {

            }

            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);
}

我有一个其他的,如果靠近底部被注释掉了。这是我试图找到谷歌驱动器packageName并发送意图所以用户仍然可以使用该应用程序,它没有工作。就像我之前说的那样,驱动器会获取发送给它的信息,但它只是注释文本,而不是我想要的标题和文本。

所以我有2个问题。

  1. 如何过滤掉我不希望用户能够发送信息的所有应用,以便他们甚至不会显示为选项,让我们选择Twitter示例

  2. 如果无法解决上述问题,我该如何找到这些不同应用的包名?对于Google云端硬盘,我尝试使用注释掉的else if语句向应用程序发送意图,但它从未执行过。我到处搜索了谷歌驱动器的意图,但我无法获得包名称以使我的代码正常工作。

  3. 提前致谢

0 个答案:

没有答案