如何筛选用于ACTION_SEND意图特定的应用程序(并设置了不同的文本为每个应用程序) [英] How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

查看:256
本文介绍了如何筛选用于ACTION_SEND意图特定的应用程序(并设置了不同的文本为每个应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ACTION_SEND意图时,你怎么能过滤掉特定的应用程序?这个问题已经被问以不同的方式,但我一直没能收集的基础上给出的答案的解决方案。希望有人可以提供帮助。我想提供给内的应用程序共享的能力。继<一href="http://stackoverflow.com/questions/4986612/how-to-catch-android-share-intents-limited-to-specific-urls-using-intent-filter">Android开发亚历山大·卢卡斯的建议,我倒是preFER它使用的意图,而不是使用Facebook / Twitter的API来这样做。

How can you filter out specific apps when using the ACTION_SEND intent? This question has been asked in various ways, but I haven't been able to gather a solution based on the answers given. Hopefully someone can help. I would like to provide the ability to share within an app. Following Android Dev Alexander Lucas' advice, I'd prefer to do it using intents and not using the Facebook/Twitter APIs.

使用ACTION_SEND意图共享是伟大的,但的问题是:(1)我不希望每个共享选项那里,我宁愿把它限制在FB,Twitter和电子邮件,以及(2)我不想共享同样的事情到每个共享应用程序。例如,在我的微博分享我要包括一些提到和#标签限制到140个字符以内,而facebook的份额将包括一个链接,一个特征图像。

Sharing using the ACTION_SEND intent is great, but the problem is (1) I don't want every sharing option there, I'd rather limit it to FB, Twitter, and Email, and (2) I don't want to share the same thing to each sharing app. For example, in my twitter share I'm going to include some mentions and hashtags limited it to 140 chars or less, while the facebook share is going to include a link and a feature image.

是否有可能限制了ACTION_SEND(股)意图的选项?我已经看到了一些关于使用PackageManager和queryIntentActivities,但一直没能找出PackageManager和ACTION_SEND意图之间的连接。

Is it possible to limit the options for ACTION_SEND (share) intent? I've seen something about using PackageManager and queryIntentActivities, but haven't been able to figure out the connection between the PackageManager and the ACTION_SEND intent.

而不是过滤共享应用程序,我的问题也得到解决,如果我可以使用ACTION_SEND意图直接到Facebook或Twitter,而不是弹出对话框。如果是这样的话,那么我可以创建自己的对话框,当他们点击脸谱创建一个Facebook专用的意图,只是给他们一路到Facebook。同样的,Twitter的。

Rather than filter the sharing apps, my problem could also be solved if I could use the ACTION_SEND intent to go directly to facebook or twitter rather than popping up the dialog. If that were the case then I could create my own dialog and when they click "Facebook" create a Facebook-specific intent and just send them all the way to Facebook. Same with Twitter.

或者是不可能的?是Facebook和Twitter的API的唯一途径?

OR is it not possible? Are the Facebook and Twitter APIs the only way?

推荐答案

据我所知,StackOverflow的有很多人要求以各种方式这个问题,但没有人回答了这个问题尚未完全

To my knowledge, StackOverflow has lots of people asking this question in various ways, but nobody has answered it completely yet.

我的规格要求的用户可以选择邮件,微博,Facebook或短信,与自定义文本为每一个。这里是我做到了这一点:

My spec called for the user to be able to choose email, twitter, facebook, or SMS, with custom text for each one. Here is how I accomplished that:

public void onShareClick(View v) {
    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, Html.fromHtml(resources.getString(R.string.share_email_native)));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
    emailIntent.setType("message/rfc822");

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


    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    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, resources.getString(R.string.share_twitter));
            } 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, resources.getString(R.string.share_facebook));
            } else if(packageName.contains("mms")) {
                intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
            } 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, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));               
                intent.setType("message/rfc822");
            }

            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);       
}

我找到了如何做到这一点在各个地方位,但我还没有看到这一切在一个地方其他地方。

I found bits of how to do this in various places, but I haven't seen all of it in one place anywhere else.

请注意,此方法还隐藏了所有我不想愚蠢的选项,如分享了WiFi和蓝牙。

Note that this method also hides all the silly options that I don't want, like sharing over wifi and bluetooth.

希望这可以帮助别人。

编辑: 在评论,我被要求解释一下这个code在做什么。基本上,它是创造一个ACTION_SEND意图本地电子邮件客户端只,然后套结等意图到选择器。使得原本意图电子邮件专用摆脱所有额外的垃圾像WiFi和蓝牙,然后我抢在对方意图我想从类型明文的通用ACTION_SEND,并显示在选配前钉它们。

In a comment, I was asked to explain what this code is doing. Basically, it's creating an ACTION_SEND intent for the native email client ONLY, then tacking other intents onto the chooser. Making the original intent email-specific gets rid of all the extra junk like wifi and bluetooth, then I grab the other intents I want from a generic ACTION_SEND of type plaintext, and tack them on before showing the chooser.

当我抢额外意图,我设置的自定义文本为每一个。

When I grab the additional intents, I set custom text for each one.

EDIT2:这是一段时间,因为我张贴这一点,事情已经改变了一点。如果您看到的Gmail两次在选项列表中,请尝试去除特殊处理android.gm作为一个注释下面@h_k建议。

It's been awhile since I posted this, and things have changed a bit. If you are seeing gmail twice in the list of options, try removing the special handling for "android.gm" as suggested in a comment by @h_k below.

由于这一个答案是,几乎所有我的计算器声望点数的来源,我必须至少尽量保持最新的:)

Since this one answer is the source of nearly all my stackoverflow reputation points, I have to at least try to keep it up to date :)

这篇关于如何筛选用于ACTION_SEND意图特定的应用程序(并设置了不同的文本为每个应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆