Sharing URL to Facebook, Twitter and email in Andr

2019-03-09 12:46发布

Is there anything similar to getsharekit.com for Android? It allows to share URL's to social networking sites. Is there anything similar to this or do I need to code separately for facebook, Twitter and email?

6条回答
相关推荐>>
2楼-- · 2019-03-09 13:06

For facebook you can use `

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

website url could be any thing refereing to any resource for example if you want to get an image from internet and sharung it on your wall .

hope this would help

查看更多
冷血范
3楼-- · 2019-03-09 13:10

I don't know if that's what you mean but you can use the Android built-in sharing menu...

You can share a URL to Facebook, Twitter, Gmail and more (as long as the apps are installed on your device) using Intents:

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

If the app you want to share to is not installed on the user's device, for example - facebook, then you'll have to use Facebook SDK.

If you want your Activity to handle text data shared from other apps as well, you can add this to your 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>

Hope this helps!

查看更多
姐就是有狂的资本
4楼-- · 2019-03-09 13:10

You can use also ShareCompat class from support library.

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

查看更多
\"骚年 ilove
5楼-- · 2019-03-09 13:11

For me this works just fine:

startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND,
    Uri.parse("http://...")),"Share URL"));// share url is on your own
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-03-09 13:22

Here is my implementation, you can add more apps if you wish by just adding package names. This code also sorts the applications according to their names.

    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();
        }
    }
查看更多
The star\"
7楼-- · 2019-03-09 13:25

You can try this...

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..."));
    }
查看更多
登录 后发表回答