Android and Facebook share intent

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 through Facebook's SDK it appears that this is easy enough to do, however I'm keen to allow the user to do it via the regular Share Intent pop up window? seen here:

弹出

I have tried the usual share intent code, however this no longer appears to work for Facebook.

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

    activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title)));
}

UPDATE: Having done more digging, it looks as though it's a bug with Facebook's app that has yet to be resolved! (facebook bug) For the mean time it looks like I'm just going to have to put up with the negative "Sharing doesn't work!!!" reviews. Cheers Facebook :*(


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.


Apparently Facebook no longer (as of 2014) allows you to customise the sharing screen, no matter if you are just opening sharer.php URL or using Android intents in more specialised ways. See for example these answers:

  • "Sharer.php no longer allows you to customize."
  • "You need to use Facebook Android SDK or Easy Facebook Android SDK to share."
  • Anyway, using plain Intents, you can still share a URL, but not any default text with it , as billynomates commented. (Also, if you have no URL to share, just launching Facebook app with empty "Write Post" (ie status update) dialog is equally easy; use the code below but leave out EXTRA_TEXT .)

    Here's the best solution I've found that does not involve using any Facebook SDKs.

    This code opens the official Facebook app directly if it's installed, and otherwise falls back to opening sharer.php in a browser. (Most of the other solutions in this question bring up a huge "Complete action using…" dialog which isn't optimal at all!)

    String urlToShare = "https://stackoverflow.com/questions/7545254";
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
    
    // See if official Facebook app is found
    boolean facebookAppFound = false;
    List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
            intent.setPackage(info.activityInfo.packageName);
            facebookAppFound = true;
            break;
        }
    }
    
    // As fallback, launch sharer.php in a browser
    if (!facebookAppFound) {
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
    }
    
    startActivity(intent);
    

    (Regarding the com.facebook.katana package name, see MatheusJardimB's comment.)

    The result looks like this on my Nexus 7 (Android 4.4) with Facebook app installed:

    在这里输入图像描述


    The usual way

    The usual way to create what you're asking for, is to simply do the following:

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "The status update text");
        startActivity(Intent.createChooser(intent, "Dialog title text"));
    

    This works without any issues for me.

    The alternative way (maybe)

    The potential problem with doing this, is that you're also allowing the message to be sent via e-mail, SMS, etc. The following code is something I'm using in an application, that allows the user to send me an e-mail using Gmail. I'm guessing you could try to change it to make it work with Facebook only.

    I'm not sure how it responds to any errors or exceptions (I'm guessing that would occur if Facebook is not installed), so you might have to test it a bit.

        try {
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            String[] recipients = new String[]{"e-mail address"};
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "E-mail subject");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "E-mail text");
            emailIntent.setType("plain/text"); // This is incorrect MIME, but Gmail is one of the only apps that responds to it - this might need to be replaced with text/plain for Facebook
            final PackageManager pm = getPackageManager();
            final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
            ResolveInfo best = null;
            for (final ResolveInfo info : matches)
                if (info.activityInfo.packageName.endsWith(".gm") ||
                        info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
                    if (best != null)
                        emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
                    startActivity(emailIntent);
        } catch (Exception e) {
            Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
        }
    
    链接地址: http://www.djcxy.com/p/59624.html

    上一篇: Android的关键哈希

    下一篇: Android和Facebook共享意图