如何在Android中自定义共享意图?

时间:2011-07-26 08:44:01

标签: android share

现在我可以使用共享意图来打开共享对话框

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, link);  
    startActivity(Intent.createChooser(intent, "Share with"));

但是我需要对话框不显示并直接分享到一个社交网络(例如FB或推特)

任何建议如何做到这一点?

4 个答案:

答案 0 :(得分:79)

有一种方法可以直接打开您想要的意图。您可以获取意图列表并仅打开一个。

请参阅此代码:

private void initShareIntent(String type) {
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                    info.activityInfo.name.toLowerCase().contains(type) ) {
                share.putExtra(Intent.EXTRA_SUBJECT,  "subject");
                share.putExtra(Intent.EXTRA_TEXT,     "your text");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) ); // Optional, just if you wanna share an image.
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return;

        startActivity(Intent.createChooser(share, "Select"));
    }
}

如果您想打开Twitter,请执行以下操作:

initShareIntent("twi");

如果facebook:

initShareIntent("face");

如果邮件:

initShareIntent("mail"); // or "gmail"

如果您想显示与该类型匹配的意图列表,请使用第一个马赫,请参阅此帖子:Android Intent for Twitter application

答案 1 :(得分:7)

不,你不能。 Intent应该以这种方式工作。如果您必须强制打开特定应用,请在目标应用支持时使用显式意图。如果不知道目标应用程序的包名称或组件名称,或类型或mime类型的数据,则无法强制特定应用程序处理通用意图。

答案 2 :(得分:4)

首先,列出要分享的所有应用。

private java.util.List<ResolveInfo> showAllShareApp() {
    java.util.List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
    Intent intent = new Intent(Intent.ACTION_SEND, null);
    intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    intent.setType("text/plain");
    PackageManager pManager = getPackageManager();
    mApps = pManager.queryIntentActivities(intent,
            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
    return mApps;
}

然后,选择一个

    private void share(ResolveInfo appInfo) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        if (appInfo != null) {
            sendIntent
                    .setComponent(new ComponentName(
                            appInfo.activityInfo.packageName,
                            appInfo.activityInfo.name));
        }
        sendIntent.setType("text/plain");
//        startActivity(Intent.createChooser(sendIntent, "Share"));
        startActivity(sendIntent);
    }

Here's an sample project。希望它有所帮助。

答案 3 :(得分:1)

您可以使用ACTION_SENDTO获取所有电子邮件客户端

        Intent getMailClients = new Intent(Intent.ACTION_SENDTO);
        getMailClients.setData(Uri.parse("mailto:"));
        final PackageManager pm = this.getPackageManager();
        final List<ResolveInfo> emailsClients = pm.queryIntentActivities(getMailClients, 0);

        if (emailsClients.size() == 0) {
            Toast.makeText(this, "There are no email clients installed", Toast.LENGTH_LONG).show();
            return;
        }

然后创建自己的选择器(包含已创建应用程序列表的对话框)。当用户点击项目时,你可以像这样做。

        Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
        sendMailIntent.setType(some type like text/plain or other you need);
        ...
        there you can set SUBJECT,EMAILTO, attach files
        ...
        final List<ResolveInfo> matches = pm.queryIntentActivities(sendMailIntent, 0);
        ResolveInfo sendingProgramm = null;
        for (final ResolveInfo info : matches) {
            if (info.activityInfo.packageName.equals(clickedResolveInfo.activityInfo.packageName)) {
                sendingProgramm = info;
                sendMailIntent
                        .setClassName(sendingProgramm.activityInfo.packageName, sendingProgramm.activityInfo.name);
                break;
            }
        }
        startActivity(sendMailIntent);

也许它会帮助你。