Twitter,Facebook和Google+如何在chooserIntent上显示?

时间:2014-01-02 05:02:06

标签: android facebook android-intent twitter

我正在尝试将Twitter,Facebook和Google Plus添加到我的chooserIntent中,而我所获得的只是电子邮件,Gmail和消息传递选项。根据选择的社交媒体,我有不同的消息。我相信我遗漏了一些东西来迫使这些应用程序列入我的列表。我的测试设备也安装了所有这三个应用程序。

我为此调用了shareIntent()方法。

private Intent shareIntent() {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.setType("text/plain");

        String shareMsg = "Message 1";

        String emailMsg = "Message 2";

        PackageManager pm = getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
        for (final ResolveInfo app : activityList) {
            String packageName = app.activityInfo.packageName;
            Log.d(TAG, "packageName(" + packageName +")");
            Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            targetedShareIntent.setType("text/plain");
            targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
                    "SUBJECT");

            if (TextUtils.equals(packageName, "com.twitter.android")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        shareMsg);
            } else if (TextUtils.equals(packageName, "com.facebook.android")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        shareMsg);
            } else if (TextUtils.equals(packageName, "com.google.android.apps.plus")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        shareMsg);
            } else if (TextUtils.equals(packageName, "com.google.android.gm")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                        new String[] {"email@hotmail.com", "email@gmail.com"});
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        emailMsg);
            } else if (TextUtils.equals(packageName, "com.android.email")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                        new String[] {"email@hotmail.com", "email@gmail.com"});
            } else if (TextUtils.equals(packageName, "com.android.ms")) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        shareMsg);
            }

            targetedShareIntent.setPackage(packageName);
            targetedShareIntents.add(targetedShareIntent);
        }
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), 
                "Share via");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
                targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
        return shareIntent;
    }

2 个答案:

答案 0 :(得分:1)

尝试使用以下PackageNames

对于Twitter:

  if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) 

对于Facebook:

   if ((app.activityInfo.name).contain("facebook")) 

对于Gmail:

 if ((app.activityInfo.name).contain("gmail"))

查看HERE

答案 1 :(得分:0)

我有一个解决方案。看一看。感谢所有帮助过的人。

private Intent shareIntent() {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.setType("text/plain");

    String shareMsg = "Message 1";

    String emailMsg = "Message 2";

    PackageManager pm = getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {
        String packageName = app.activityInfo.packageName;
        Log.d(TAG, "packageName(" + packageName +")");
        Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        targetedShareIntent.setType("text/plain");
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
                "Subject");

    if (TextUtils.equals(packageName, "com.google.android.gm")) {
        Log.d(TAG, "gmail: " + app.activityInfo.name);
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                new String[] {"email@hotmail.com", "email@gmail.com"});
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                emailMsg);
    } else if (TextUtils.equals(packageName, "com.android.email")) {
        Log.d(TAG, "email: " + app.activityInfo.name);
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                new String[] {"email@hotmail.com", "email@gmail.com"});
    } else if (TextUtils.equals(packageName, "com.android.mms")) {
        Log.d(TAG, "text msg: " + app.activityInfo.name);
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                shareMsg);
    }

        targetedShareIntent.setPackage(packageName);
        targetedShareIntents.add(targetedShareIntent);
    }

    Intent facebookIntent = getShareIntent("facebook", "Tip Calculator", shareMsg);
    if(facebookIntent != null)
        targetedShareIntents.add(facebookIntent);

    Intent twitterIntent = getShareIntent("twitter", "Tip Calculator", shareMsg);
    if(twitterIntent != null)
        targetedShareIntents.add(twitterIntent);

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), 
            "Share via");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
            targetedShareIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);
    return shareIntent;
}

你也需要这个其他方法。

private Intent getShareIntent(String type, String subject, String text) 
{
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(share, 0);
    System.out.println("resinfo: " + resInfo);
    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,     text);
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return null;
        return share;
    }
    return null;
}

希望将来可以帮助其他人。