Android and Facebook share intent

前端 未结 12 2299
闹比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);
    }
    

提交回复
热议问题