Android and Facebook share intent

前端 未结 12 2126
闹比i
闹比i 2020-11-22 13:02

I\'m developing an Android app and am interested to know how you can update the app user\'s status from within the app using Android\'s share intents.

Having looked

相关标签:
12条回答
  • 2020-11-22 13:48

    In Lollipop (21), you can use Intent.EXTRA_REPLACEMENT_EXTRAS to override the intent for Facebook specifically (and specify a link only)

    https://developer.android.com/reference/android/content/Intent.html#EXTRA_REPLACEMENT_EXTRAS

    private void doShareLink(String text, String link) {
      Intent shareIntent = new Intent(Intent.ACTION_SEND);
      shareIntent.setType("text/plain");
      Intent chooserIntent = Intent.createChooser(shareIntent, getString(R.string.share_via));
    
      // for 21+, we can use EXTRA_REPLACEMENT_EXTRAS to support the specific case of Facebook
      // (only supports a link)
      // >=21: facebook=link, other=text+link
      // <=20: all=link
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        shareIntent.putExtra(Intent.EXTRA_TEXT, text + " " + link);
        Bundle facebookBundle = new Bundle();
        facebookBundle.putString(Intent.EXTRA_TEXT, link);
        Bundle replacement = new Bundle();
        replacement.putBundle("com.facebook.katana", facebookBundle);
        chooserIntent.putExtra(Intent.EXTRA_REPLACEMENT_EXTRAS, replacement);
      } else {
        shareIntent.putExtra(Intent.EXTRA_TEXT, link);
      }
    
      chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(chooserIntent);
    }
    
    0 讨论(0)
  • 2020-11-22 13:48

    The easiest way that I could find to pass a message from my app to facebook was programmatically copy to the clipboard and alert the user that they have the option to paste. It saves the user from manually doing it; my app is not pasting but the user might.

    ...
    if (app.equals("facebook")) {
        // overcome fb 'putExtra' constraint;
        // copy message to clipboard for user to paste into fb.
        ClipboardManager cb = (ClipboardManager) 
                getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("post", msg);
        cb.setPrimaryClip(clip);
    
        // tell the to PASTE POST with option to stop showing this dialogue
        showDialog(this, getString(R.string.facebook_post));
    }
    startActivity(appIntent);
    ...
    
    0 讨论(0)
  • 2020-11-22 13:49

    This solution works aswell. If there is no Facebook installed, it just runs the normal share-dialog. If there is and you are not logged in, it goes to the login screen. If you are logged in, it will open the share dialog and put in the "Share url" from the Intent Extra.

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "Share url");
    intent.setType("text/plain");
    
    List<ResolveInfo> matches = getMainFragmentActivity().getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().contains("facebook")) {
            intent.setPackage(info.activityInfo.packageName);
        }
    }
    
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 13:51

    The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.

    Here is bug link: https://developers.facebook.com/bugs/332619626816423

    Thanks @billynomates:

    The thing is, if you put a URL in the EXTRA_TEXT field, it does work. It's like they're intentionally stripping out any text.

    0 讨论(0)
  • 2020-11-22 13:52

    Here is something I did which open Facebook App with Link

    shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setComponent(new ComponentName("com.facebook.katana",
                        "com.facebook.katana.activity.composer.ImplicitShareIntentHandler"));
    
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT,  videoUrl);
    
    0 讨论(0)
  • 2020-11-22 13:54
        public void invokeShare(Activity activity, String quote, String credit) {
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.share_subject));
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Example text");    
        shareIntent.putExtra("com.facebook.platform.extra.APPLICATION_ID", activity.getString(R.string.app_id));                        
        activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title)));
    }
    
    0 讨论(0)
提交回复
热议问题