意图选择器 - 针对不同应用程序的不同意图

时间:2014-11-09 18:40:55

标签: android android-intent

我找到了this回答,它解释了如何为每个应用创建不同的意图,但有些原因却不起作用:

此处是重现问题的代码,您甚至可以对if发表评论,您将坚持text而不是twitter

public Intent getShareChooserIntent(Context context) {
    Intent basicIntent = new Intent();
    basicIntent.setAction(Intent.ACTION_SEND);
    basicIntent.setType("text/plain");
    basicIntent.putExtra(Intent.EXTRA_TEXT, "text");
    basicIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    PackageManager pm = context.getPackageManager();

    Intent openInChooser = Intent.createChooser(basicIntent, "share");
    List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
       ResolveInfo ri = resInfo.get(i);
       String packageName = ri.activityInfo.packageName;
       Intent realIntent = new Intent();
       realIntent.setAction(Intent.ACTION_SEND);
       realIntent.setType("text/plain");
       realIntent.putExtra(Intent.EXTRA_TEXT, "text");
       realIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");

       if (packageName.contains("twitter")) // !!!!THIS LINE!!!!
           realIntent.putExtra(Intent.EXTRA_TEXT, "twitter");

       intentList.add(new LabeledIntent(realIntent, packageName, ri.loadLabel(pm), ri.icon));
    }

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
    return openInChooser;
}
我做错了什么?

1 个答案:

答案 0 :(得分:4)

不确定getShareBasicIntent是做什么的,但是这段代码对我来说运行正常(使用和不使用twitter检查)

static public Intent getShareChooserIntent(Context context) {

    PackageManager pm = context.getPackageManager();

    // Note that this intent should only be for email
    // to avoid duplicates in the list
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "not twitter"); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.setType("message/rfc822");
    Intent openInChooser = Intent.createChooser(emailIntent, "share");

    // the basic intent used to query all the activities that can support
    // text sharing
    Intent basicIntent = new Intent();
    basicIntent.setAction(Intent.ACTION_SEND);
    basicIntent.setType("text/plain"); 
    List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);

    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        Intent intent=new Intent();
        intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "not twitter"); 
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.setPackage(packageName);

        if (packageName.contains("twitter")) { 
            intent.putExtra(Intent.EXTRA_TEXT, "twitter");
        }

        intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
    }

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
    return openInChooser;
}