获取所选的共享意图

时间:2012-11-30 18:15:37

标签: java android facebook android-intent share

我使用以下代码通过gmail,facebook,twitter等分享一些文字:

  Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "The status update text");
startActivity(Intent.createChooser(intent, "Dialog title text"));

问题是我想检查所选的意图是否为Facebook,如果是,我会做一些其他编码而不是像往常一样提示共享意图。

有可能吗?

2 个答案:

答案 0 :(得分:3)

我刚刚找到以下链接:http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

它描述了如何执行此类任务,您需要做的就是创建一个行布局,然后就完成了。 我试了一下,效果很好!

修改 show函数中的文章中存在错误,应该如下:

final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray());

感谢@imram khan提到这一点。

如果有人想使用我的xml行布局:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


   <TextView android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:layout_marginRight="50dp"
/>


    <ImageView android:id="@+id/logo2"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:padding="10dp"
    android:src="@drawable/number1"
    android:layout_gravity="center_vertical"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

答案 1 :(得分:1)

有同样的问题,想要检测Facebook共享选项并采取不同的逻辑路径(主要是因为Facebook通过常规Android共享共享是有限的)

我的解决方案

public void shareIt(View view){
    //sharing implementation
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";

    PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {

         String packageName = app.activityInfo.packageName;
         Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
         targetedShareIntent.setType("text/plain");
         targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
         if(TextUtils.equals(packageName, "com.facebook.katana")){
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
         } else {
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
         }

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

    }

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");

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

}

列在此Q和A上 Branching the Android Share Intent extras depending on which method they choose to share