Android仅通过选定的社交媒体应用程序共享详细信息

时间:2019-03-21 05:40:59

标签: android android-intent share

我只想与whatsApp,viber,twitter和gmail应用程序共享消息,因此使用Intent.createChooser编写我的代码,但它不起作用甚至没有显示错误,我在侧面片段中调用了此方法...此代码在我的另一个项目中起作用,我在对话框中调用了此方法。

 public void shareDetails(String message) {

        List<Intent> intentShareList = new ArrayList<Intent>();
        Intent shareIntent = new Intent();

        List<ResolveInfo>  resolveInfoList = context.getPackageManager().queryIntentActivities(shareIntent, 0);
        try {

            for (ResolveInfo resInfo : resolveInfoList) {
                String packageName = resInfo.activityInfo.packageName;
                String name = resInfo.activityInfo.name;

                if (packageName.contains("om.viber.voip") ||
                        packageName.contains("com.twitter.android") ||
                        packageName.contains("com.google.android.apps.plus") ||
                        packageName.contains("com.google.android.gm") ||
                        packageName.contains("com.whatsapp")) {

                    if (name.contains("com.twitter.android.DMActivity")) {
                        continue;
                    }

                    shareIntent.setComponent(new ComponentName(packageName, name));
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.setType("text/plain");
                    shareIntent.putExtra(Intent.EXTRA_TEXT, message);

                    intentShareList.add(shareIntent);
                }
            }
        if (intentShareList.isEmpty()) {
            Toast.makeText(context, "No apps to share !", Toast.LENGTH_SHORT).show();
        } else {
            Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Product Details via");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }
    } catch (Exception e) {

    }

}

请帮助

2 个答案:

答案 0 :(得分:0)

您使用的Parcelable没有大小。试试这个修改过的。

public void shareDetails(String message) {
    try {

        List<Intent> intentShareList = new ArrayList<Intent>();
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
        if (!resolveInfoList.isEmpty()) {
            for (ResolveInfo resolveInfo : resolveInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
                targetedShareIntent.setType("text/plain");
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
                if (packageName.contains("om.viber.voip") ||
                        packageName.contains("com.twitter.android") ||
                        packageName.contains("com.google.android.apps.plus") ||
                        packageName.contains("com.google.android.gm") ||
                        packageName.contains("com.whatsapp")) {
                    targetedShareIntent.setPackage(packageName);
                    intentShareList.add(targetedShareIntent);
                }

            }


            if (intentShareList.isEmpty()) {
                Toast.makeText(context, "No apps to share !", Toast.LENGTH_SHORT).show();
            } else {
                Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Product Details via");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[intentShareList.size()]));
                startActivity(chooserIntent);
            }
        }
    } catch (Exception e) {

    }

}

答案 1 :(得分:0)

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