java - 在 Android 上捕获和拦截 ACTION_SEND Intent

标签 java android android-intent

目前我有一个非常标准的 ACTION_SEND Intent 来从我的应用程序内部共享信息。代码类似于以下内容:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, text);
    context.startActivity(Intent.createChooser(intent, title));

现在,如果用户在他们的手机上安装了 Facebook 应用程序,那么 Facebook 将显示为意向选择器的一个选项。但是,我想拦截用户对“Facebook”的点击,并使用 Facebook SDK 来执行任务,而不是用户手机上已有的 Facebook 应用程序。有没有办法拦截 Facebook 选项的 onClick?谢谢!

最佳答案

这是我的博客文章的链接,其中包含详细的解决方案,包括代码和屏幕截图。

http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

引用自那篇文章:

[W]e first create a new action_send intent and set the type to text/plain. Next, we create a List. We make the call to the package manager to query for all Activities with the action_send intent registered. This call returs a list of ResolveInfo objects, each corresponding to an Activity on the device that claims to handle send actions.

[Then, r]ather than launching the action_send intent and letting it create its own dialog (one that we would have no control over), we will build our own with the AlertDialog.Builder. First we give it a title, then set its adapter to the list adapter we just created with the activities list as our data set[.]

The next important piece we need to look at is the OnClick listener we gave the Builder. To find which item the user clicked, we use the adapter.getItem(int which) method. This will return the object in that position of our original list, in this case, a ResolveInfo object corresponding to the selected Activity. For my case in particular, I only care about separating things into two groups: Facebook and not Facebook. To do this, I simply check if the selected Activity’s package name contains ‘facebook’. If it does not, I create a new action_send intent, set the class name to the selected activity, and launch it. However, if the package name DOES contain ‘facebook’, I instantiated my personal PostToFacebookDialog object which will create a basic Android dialog with post and cancel buttons, and will post to Facebook directly using the Graph API, thus circumventing the user’s installed Facebook app.

关于java - 在 Android 上捕获和拦截 ACTION_SEND Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350531/

相关文章:

java - 为什么我的方法没有为类型对象定义?

android-intent - 从 Android 应用程序打开社交网络个人资料网址?

java - 无法实现 getIntent(); fragment 中的方法

java - 无法通过代理连接到互联网/存储库

java - 如何跳转到特定行并从 java 中读取

java - 如何使用Java创建一个在Oracle数据库中创建表和修改数据并具有多种用途的程序

android - 使用样式在主题中设置 Activity 背景?

android - 更新到android studio 3.0后无法mergedex

android - 无法在 Android 设备上查看 PDF(带有嵌入式 swf)

android - 从 Android 浏览器/Chrome 启动自定义 Android 应用程序