【问题标题】:Get the selected share intent获取选定的分享意图
【发布时间】:2012-11-30 18:15:36
【问题描述】:

我正在使用以下代码通过 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,如果是,我会做一些其他的编码,而不是像往常一样提示分享意图。

有可能吗?

【问题讨论】:

  • 据我所知,这是不可能的。您无法通过 Facebook、twitter 收到任何 Dialog ... 一个想法是在单击“共享”时启动一个 CountDownTimer,例如 10 秒,然后使用 ActivityManager 检查哪个 Activity 在您的顶部。跨度>
  • 您可以使用 PackageManager 执行此操作,您可以在 startActivity(Intent.createChooser(intent, "Dialog title text")); 之后检查 Activity Stack 中的 Current top Activity;
  • @imrankhan 我该怎么做?
  • @imrankhan 是的!工作得很好,非常感谢!如果您愿意,可以发布答案,以便我投票并勾选您的答案。
  • @idish :我认为您可以向我发布更好的答案。所以请将其发布为其他寻求相同问题的帮助的答案。非常感谢

标签: java android facebook android-intent share


【解决方案1】:

我刚刚找到以下链接: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>

【讨论】:

  • +1vote 您也可以接受它作为自己的答案和 xml 布局或 final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray()); 作为当前代码的更改
  • @imrankhan 好的已编辑,我只是​​试图接受我的回答,但我只能在 2 天内接受。无论如何,再次感谢您。
【解决方案2】:

有同样的问题,想要检测 facebook 共享选项并采用不同的逻辑路径(主要是因为通过常规 android 共享的 facebook 共享受到限制)

我的解决方案

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

【讨论】:

  • 谢谢,它对我有用,当用户选择 Twitter 时,我用它来剪切文本,谢谢
猜你喜欢
  • 2011-05-24
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2018-09-04
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多