我想通过像facebook,twitter这样的社交网络分享我的应用程序

时间:2016-04-28 07:59:34

标签: android android-layout

如果用户点击Facebook按钮,系统可以打开只有facebook应用程序的选择器,如果已安装,否则显示未安装应用程序的弹出窗口。 其他社交网络如twitter等也一样......

1 个答案:

答案 0 :(得分:1)

此示例代码将字符串分享给facebook,zalo,twiter。你可以根据需要定制

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) {
    }
}