在Android中分享到Facebook,Twitter和电子邮件的URL?

时间:2010-11-01 06:43:48

标签: android facebook email twitter sharing

有没有类似于getsharekit.com的Android?它允许共享URL到社交网站。有没有类似的东西,或者我需要单独为Facebook,Twitter和电子邮件编码?

7 个答案:

答案 0 :(得分:70)

我不知道这是不是你的意思,但你可以使用Android内置的分享菜单......

您可以使用Intents分享Facebook,Twitter,Gmail等网址(只要您的设备上安装了应用):

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
i.putExtra(Intent.EXTRA_TEXT, "http://www.url.com");
startActivity(Intent.createChooser(i, "Share URL"));

如果您要共享的应用程序未安装在用户的设备上,例如 - facebook,那么您将不得不使用Facebook SDK。

如果您希望Activity能够处理从其他应用程序共享的文本数据,您可以将其添加到AndroidManifest.xml:

<activity android:name=".ShareLink">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

希望这有帮助!

答案 1 :(得分:20)

您也可以使用支持库中的 ShareCompat 类。

ShareCompat.IntentBuilder.from(activity)
    .setType("text/plain")
    .setChooserTitle("Share URL")
    .setText("http://www.url.com")
    .startChooser();

https://developer.android.com/reference/android/support/v4/app/ShareCompat.html

答案 2 :(得分:5)

对于Facebook,你可以使用`

  

https://m.facebook.com/sharer.php?u=website_url&t=titleOfThePost

网站网址可以是任何引用任何资源的内容,例如,如果您想从互联网上获取图像并将其置于墙上。

希望这会有所帮助

答案 3 :(得分:2)

你可以试试这个......

private void shareTextUrl() {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

        share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
        share.putExtra(Intent.EXTRA_TEXT, "<source url>");

        startActivity(Intent.createChooser(share, "Share text to..."));
    }

答案 4 :(得分:0)

这是我的实现,如果您愿意,只需添加包名称即可添加更多应用。此代码还会根据应用程序的名称对应用程序进行排序。

    List<Intent> targetShareIntents = new ArrayList<Intent>();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    PackageManager pm = getActivity().getPackageManager();
    List<ResolveInfo> resInfos = pm.queryIntentActivities(shareIntent, 0);
    if (!resInfos.isEmpty()) {
        System.out.println("Have package");
        for (ResolveInfo resInfo : resInfos) {
            String packageName = resInfo.activityInfo.packageName;
            Log.i("Package Name", packageName);

            if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")
                    || packageName.contains("com.whatsapp") || packageName.contains("com.google.android.apps.plus")
                    || packageName.contains("com.google.android.talk") || packageName.contains("com.slack")
                    || packageName.contains("com.google.android.gm") || packageName.contains("com.facebook.orca")
                    || packageName.contains("com.yahoo.mobile") || packageName.contains("com.skype.raider")
                    || packageName.contains("com.android.mms")|| packageName.contains("com.linkedin.android")
                    || packageName.contains("com.google.android.apps.messaging")) {
                Intent intent = new Intent();

                intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                intent.putExtra("AppName", resInfo.loadLabel(pm).toString());
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "https://website.com/");
                intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_text));
                intent.setPackage(packageName);
                targetShareIntents.add(intent);
            }
        }
        if (!targetShareIntents.isEmpty()) {
            Collections.sort(targetShareIntents, new Comparator<Intent>() {
                @Override
                public int compare(Intent o1, Intent o2) {
                    return o1.getStringExtra("AppName").compareTo(o2.getStringExtra("AppName"));
                }
            });
            Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        } else {
            Toast.makeText(getActivity(), "No app to share.", Toast.LENGTH_LONG).show();
        }
    }

答案 5 :(得分:0)

//表示URL

Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.codeofaninja.com");
startActivity(Intent.createChooser(share, "Share link!"));

//用于图片

Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");

// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
        + "/myImage.png";

File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));

答案 6 :(得分:-4)

对我来说这很好用:

startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND,
    Uri.parse("http://...")),"Share URL"));// share url is on your own