Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

android - i want to share my app via social netwoking like facebook ,twitter

Is it possible if user click on facebook button , the system open a chooser having only facebook app,if it is installed otherwise it shows an popup of app is not installed. The same for other social networks like twitter etc...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This sample code to share String to facebook,zalo, twiter. you can custom as desired

public void share(String message) {
    try {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        List<ResolveInfo> resInfo = getPackageManager()
                .queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = new Intent(
                        android.content.Intent.ACTION_SEND);
                targetedShare.setType("text/plain"); // put here your mime
                // type
                if (info.activityInfo.packageName.toLowerCase().contains(
                        "facebook") || info.activityInfo.name.toLowerCase().contains(
                        "facebook") || info.activityInfo.packageName.toLowerCase().contains(
                        "zalo") || info.activityInfo.name.toLowerCase().contains(
                        "zalo") || info.activityInfo.packageName.toLowerCase().contains(
                        "twitter") || info.activityInfo.name.toLowerCase().contains(
                        "twitter")) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT,
                            getString(R.string.app_name));
                    targetedShare.putExtra(Intent.EXTRA_TEXT, message);
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(
                    targetedShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }
    } catch (Exception e) {
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...