Android and Facebook share intent

前端 未结 12 2125
闹比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:27

    Seems in version 4.0.0 of Facebook so many things has changed. This is my code which is working fine. Hope it helps you.

        /**
         * Facebook does not support sharing content without using their SDK however
         * it is possible to share URL
         *
         * @param content
         * @param url
         */
        private void shareOnFacebook(String content, String url)
        {
            try
            {
                // TODO: This part does not work properly based on my test
                Intent fbIntent = new Intent(Intent.ACTION_SEND);
                fbIntent.setType("text/plain");
                fbIntent.putExtra(Intent.EXTRA_TEXT, content);
                fbIntent.putExtra(Intent.EXTRA_STREAM, url);
                fbIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                fbIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                fbIntent.setComponent(new ComponentName("com.facebook.katana",
                        "com.facebook.composer.shareintent.ImplicitShareIntentHandler"));
    
                startActivity(fbIntent);
                return;
            }
            catch (Exception e)
            {
                // User doesn't have Facebook app installed. Try sharing through browser.
            }
    
            // If we failed (not native FB app installed), try share through SEND
            String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + url;
            SupportUtils.doShowUri(this.getActivity(), sharerUrl);
        }
    
    0 讨论(0)
  • 2020-11-22 13:27

    Facebook does not allow to share plain text data with Intent.EXTRA_TEXT but You can share text+link with facebook messanger using this, this works fine for me

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, text+url link);
                sendIntent.setType("text/plain");
                sendIntent.setPackage("com.facebook.orca");
                startActivity(sendIntent);
    
    0 讨论(0)
  • 2020-11-22 13:29

    I found out you can only share either Text or Image, not both using Intents. Below code shares only Image if exists, or only Text if Image does not exits. If you want to share both, you need to use Facebook SDK from here.

    If you use other solution instead of below code, don't forget to specify package name com.facebook.lite as well, which is package name of Facebook Lite. I haven't test but com.facebook.orca is package name of Facebook Messenger if you want to target that too.

    You can add more methods for sharing with WhatsApp, Twitter ...

    public class IntentShareHelper {
    
        /**
         * <b>Beware,</b> this shares only image if exists, or only text if image does not exits. Can't share both
         */
        public static void shareOnFacebook(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
    
            if (fileUri != null) {
                intent.putExtra(Intent.EXTRA_STREAM, fileUri);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setType("image/*");
            }
    
            boolean facebookAppFound = false;
            List<ResolveInfo> matches = appCompatActivity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo info : matches) {
                if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana") ||
                    info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.lite")) {
                    intent.setPackage(info.activityInfo.packageName);
                    facebookAppFound = true;
                    break;
                }
            }
    
            if (facebookAppFound) {
                appCompatActivity.startActivity(intent);
            } else {
                showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
            }
        }
    
        public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
    
        private static void showWarningDialog(Context context, String message) {
            new AlertDialog.Builder(context)
                    .setMessage(message)
                    .setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setCancelable(true)
                    .create().show();
        }
    }
    

    For getting Uri from File, use below class:

    public class UtilityFile {
        public static @Nullable Uri getUriFromFile(Context context, @Nullable File file) {
            if (file == null)
                return null;
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                try {
                    return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                return Uri.fromFile(file);
            }
        }
    
        // Returns the URI path to the Bitmap displayed in specified ImageView       
        public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
            Drawable drawable = imageView.getDrawable();
            Bitmap bmp = null;
            if (drawable instanceof BitmapDrawable) {
                bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            } else {
                return null;
            }
            // Store image to default external storage directory
            Uri bmpUri = null;
            try {
                // Use methods on Context to access package-specific directories on external storage.
                // This way, you don't need to request external read/write permission.
                File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
                FileOutputStream out = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.close();
    
                bmpUri = getUriFromFile(context, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bmpUri;
        }    
    }
    

    For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

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

    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" (i.e. 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:

    enter image description here

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

    Here is what I did (for text). In the code, I copy whatever text is needed to clipboard. The first time an individual tries to use the share intent button, I pop up a notification that explains if they wish to share to facebook, they need to click 'Facebook' and then long press to paste (this is to make them aware that Facebook has BROKEN the android intent system). Then the relevant information is in the field. I might also include a link to this post so users can complain too...

    private void setClipboardText(String text) { // TODO
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
            android.content.ClipData clip = android.content.ClipData.newPlainText("text label",text);
            clipboard.setPrimaryClip(clip);
        }
    }
    

    Below is a method for dealing w/prior versions

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_share:
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_TEXT, "text here");
    
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); //TODO
             ClipData clip = ClipData.newPlainText("label", "text here");
             clipboard.setPrimaryClip(clip);
    
            setShareIntent(shareIntent); 
    
            break;
        }
            return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-11-22 13:48

    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();
        }
    
    0 讨论(0)
提交回复
热议问题