Android Intent直接与Facebook共享?

时间:2014-04-16 15:54:21

标签: android facebook

以下代码完美无缺,

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
startActivity(Intent.createChooser(sharingIntent, "Share using"));

但它打开一个列表,从这个列表中我必须选择Facebook,然后一切顺利。

但是有没有任何配置更改可以直接打开Facebook共享而不是通过菜单。

2 个答案:

答案 0 :(得分:4)

尝试以下代码,现在意图将直接打开Facebook。

List<Intent> targetedShareIntents = new ArrayList<Intent>();

Intent facebookIntent = getShareIntent("facebook", "subject", "text_or_url");
// subject may not work, but if you have a url place it in text_or_url
if(facebookIntent != null)
    targetedShareIntents.add(facebookIntent);

Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");

chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

startActivity(chooser);

//要准确获取facebook意图,请使用以下代码

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 = getActivity().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;
}

以上代码摘自此stackoverflow-post

答案 1 :(得分:1)

这应该是可能的,如果你先得到所有活动,支持你的意图,然后得到正确的并做一个明确的电话。要使用context.getPackageManager.queryIntentActivities(Intent, int)获取所有活动。编码应该不是一个大问题。

点击此链接:PackageManager docu