Android - 分享Facebook,Twitter,Mail,ecc

And*_*ino 57 twitter android share facebook

我需要开发一个具有共享功能的应用程序.我必须分享Facebook,Twitter,电子邮件和其他服务.

我怎样才能做到这一点?网上有图书馆吗?对于iOS开发,有ShareKit,但对于Android?

谢谢 :)

Jan*_*usz 80

Paresh Mayani的回答大多是正确的.只需使用Broadcast Intent让系统和所有其他应用程序选择内容将以何种方式共享.

要共享文本,请使用以下代码:

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"));
Run Code Online (Sandbox Code Playgroud)

  • 许多无关的应用程序出现在列表中 - 与共享任何文本无关 - 如何过滤和显示只有有意义的应用程序(如短信,推特,脸书,whatsapp,而不是一些随机的电子商务应用程序) (2认同)

Ion*_*gru 32

是的,你可以...你只需要知道应用程序的确切包名:

  • Facebook - "com.facebook.katana"
  • Twitter - "com.twitter.android"
  • Instagram - "com.instagram.android"
  • Pinterest - "com.pinterest"

你可以像这样创建意图

Intent intent = context.getPackageManager().getLaunchIntentForPackage(application);
if (intent != null) {
     // The application exists
     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.setPackage(application);

     shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, title);
     shareIntent.putExtra(Intent.EXTRA_TEXT, description);
     // Start the specific social application
     context.startActivity(shareIntent);
} else {
    // The application does not exist
    // Open GooglePlay or use the default system picker
}
Run Code Online (Sandbox Code Playgroud)


Par*_*ani 28

我想你想给分享按钮,点击哪个合适的媒体/网站选项应该与它分享.在Android中,您需要创建createChooser相同的内容.

分享文字:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is the text that will be shared.");
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Run Code Online (Sandbox Code Playgroud)

共享二进制对象(图像,视频等)

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);

sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Run Code Online (Sandbox Code Playgroud)

仅供参考,以上代码均来自使用ACTION_SEND Intent在Android中共享内容

  • @PareshMayani您好我们可以在同一份额的Intents上分享图片和文字吗? (7认同)
  • @PareshMayani.必须是"text/plain"而不是"plain/text".花了一个小时才明白为什么它不适合我. (3认同)

Sun*_*ary 6

用这个

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;
}
Run Code Online (Sandbox Code Playgroud)