根据他们选择分享的方法分享Android Share Intent附加功能 [英] Branching the Android Share Intent extras depending on which method they choose to share

查看:85
本文介绍了根据他们选择分享的方法分享Android Share Intent附加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要分享一个动态文本字符串+该应用的URL。 Android的原始Android分享意图设置正确,并且使用Twitter,Gmail等功能完美无缺,但是很多人会猜测,它与Facebook无效。显然,因为Facebook不会接受 intent.EXTRA_TEXT 字段中的文本,只有一个URL。

Simply want to share a dynamic text string + the URL to the app. The native Android share intent is setup correctly, and works perfect with Twitter, Gmail, etc. But, as many will guess, it does not work with Facebook. Appearantly because Facebook will not accept text in the intent.EXTRA_TEXT field, only a single URL.

嗯,我的问题是:有没有办法根据他们选择分享哪种方法分享股份意图追加?例如,如果他们通过gmail或Twitter共享,请使用现有的 String + URL (所需选项) EXTRA_TEXT 但是如果他们选择通过Facebook分享,只能使用一个URL作为 EXTRA_TEXT

Well, my question to y'all is: is there a way to branch off the share intent extras depending on which method they choose to share? for example, if they share via gmail or Twitter, use the existing String + URL (the desired option) EXTRA_TEXT, but if they choose to share via Facebook, only use a URL as the EXTRA_TEXT.

不是真的想要实现Facebook Android SDK是一个简单的任务,内置Android版本。

Not really wanting to implement the Facebook Android SDK for such a simple task that is built-in natively in Android.

欣赏您的时间和建议。

尝试这样的东西,但它显然失败了,因为它只检查共享选项是否存在(当共享弹出时,不是在他们点击共享方法后),它们在选择方法时没有响应。 p>

Tried something like this, but it obviously fails because its only checking if the sharing option exists (when share pops up, not after they click a share method), it doesn't respond when they choose a method.

  String shareBody = "app string text " + act_txt + " more text! Get the app at http://www.appurl.com";

 PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {
         Log.i(TAG, "app.actinfo.name: " + app.activityInfo.name);
        //if((app.activityInfo.name).contains("facebook")) {
              if("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) {


            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://www.appurl.com");
            startActivity(Intent.createChooser(sharingIntent, "Share idea"));
            break;
        } else {
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "app name");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share"));
            break;
        }
    }


推荐答案

一个解决方案,就这个SO问题要求别的东西: https://stackoverflow.com/a/8550043/1938669

found a solution, on this SO question asking for something else: https://stackoverflow.com/a/8550043/1938669

尝试发布我的原始问题在这里是接近的。在可能的shareIntent列表的周期内,您需要创建一个针对特定共享选择的新共享意图(如Facebook或Twitter)。

the attempt posted my original question here was close. within that cycle of possible shareIntent List, you need to create a new share intent targeted at the specific sharing choice (like facebook or twitter)

这里是一个最终的工作解决方案如果选择了Facebook,则只分享一个URL,否则共享完整的文本字符串+ url:

here is a final working solution that shares only a URL if facebook is choosen, otherwise shares the complete text string + url:

public void shareIt(View view){
    //sharing implementation
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";

    PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {

         String packageName = app.activityInfo.packageName;
         Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
         targetedShareIntent.setType("text/plain");
         targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
         if(TextUtils.equals(packageName, "com.facebook.katana")){
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
         } else {
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
         }

         targetedShareIntent.setPackage(packageName);
         targetedShareIntents.add(targetedShareIntent);

    }

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);

}

这篇关于根据他们选择分享的方法分享Android Share Intent附加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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