Android - Share on Facebook, Twitter, Mail, ecc

后端 未结 13 2308
眼角桃花
眼角桃花 2020-11-27 09:43

I need to develop an app that has the share function. I have to share on Facebook, twitter, email and maybe other services.

How can I do this? There a library on th

相关标签:
13条回答
  • 2020-11-27 10:22

    The ACTION_SEND will only give you options for sending using GMail, YahooMail... etc(Any application installed on your phone, that can perform ACTION_SEND). If you want to share on Facebook or Twitter you will need to place custom buttons for each and use their own SDK such as Facebook SDK or Twitter4J .

    0 讨论(0)
  • 2020-11-27 10:24

    Paresh Mayani's answer is mostly correct. Simply use a Broadcast Intent to let the system and all the other apps choose in what way the content is going to be shared.

    To share text use the following code:

    String message = "Text I want to share.";
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.putExtra(Intent.EXTRA_TEXT, message);
    
    startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));
    
    0 讨论(0)
  • 2020-11-27 10:24
    String message = "This is testing."
    Intent shareText = new Intent(Intent.ACTION_SEND);
    shareText .setType("text/plain");
    shareText .putExtra(Intent.EXTRA_TEXT, message);
    
    startActivity(Intent.createChooser(shareText , "Title of the dialog the system will open"));
    
    0 讨论(0)
  • 2020-11-27 10:33

    Use this

    Facebook - "com.facebook.katana"
    Twitter - "com.twitter.android"
    Instagram - "com.instagram.android"
    Pinterest - "com.pinterest"
    
    
    
    SharingToSocialMedia("com.facebook.katana")
    
    
    public void SharingToSocialMedia(String application) {
    
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    
        boolean installed = checkAppInstall(application);
        if (installed) {
            intent.setPackage(application);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(),
                    "Installed application first", Toast.LENGTH_LONG).show();
        }
    
    }
    
    
     private boolean checkAppInstall(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 10:36

    Create Choose Editable

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
                String sAux = "\nLet me recommend you this application\n\n";
                sAux = sAux + "https://play.google.com/store/apps/details?id=the.package.id \n\n";
                sendIntent.putExtra(Intent.EXTRA_TEXT, sAux);
                startActivity(Intent.createChooser(sendIntent, "choose one"));
    

    ==============================================================

    Create Choose Default

                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                startActivity(sendIntent);
    

    =================================================================

    Multi-Piece send

    Send multiple pieces of content

    To share multiple pieces of content, use the ACTION_SEND_MULTIPLE action together with a list of URIs pointing to the content. The MIME type varies according to the mix of content you're sharing. For example, if you share 3 JPEG images, the type is still "image/jpeg". For a mixture of image types, it should be "image/*" to match an activity that handles any type of image. You should only use "*/*" if you're sharing out a wide variety of types. As previously stated, it's up to the receiving application to parse and process your data. Here's an example:

    ArrayList<Uri> imageUris = new ArrayList<Uri>();
    imageUris.add(imageUri1); // Add your image URIs here
    imageUris.add(imageUri2);
    
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "Share images to.."));
    

    ================================================================

    Share With Facebook

        ShareLinkContent shareLinkContent = new ShareLinkContent.Builder()
                .setQuote("Application of social rating share with your friend")
                .setContentUrl(Uri.parse("https://google.com"))
                .build();
        if (ShareDialog.canShow(ShareLinkContent.class)) {
            sd.show(shareLinkContent);
        }
    

    ==============================================================

    Share With SMS

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("Body", "Application of social rating share with your friend");
        intent.setType("vnd.android-dir/mms-sms");
        startActivity(intent);
    

    ==============================================================

    Share With Mail

        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
        emailIntent.putExtra(Intent.EXTRA_EMAIL, "Address");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Application of social rating share with your friend");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
        startActivity(Intent.createChooser(emailIntent, "Send Email..."));
    

    =============================================================

    Share with WhatsApp

        Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
        whatsappIntent.setType("text/plain");
        whatsappIntent.setPackage("com.whatsapp");
        whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Application of social rating share with your friend");
        try {
            Objects.requireNonNull(getActivity()).startActivity(whatsappIntent);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getActivity(), "WhatsApp have not been installed.", Toast.LENGTH_SHORT).show();
        }
    
    0 讨论(0)
  • 2020-11-27 10:37
    sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"your subject" );
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "your text");
    startActivity(Intent.createChooser(sharingIntent, ""));
    
    0 讨论(0)
提交回复
热议问题