捕获和Android上的拦截ACTION_SEND意图 [英] Capturing and intercepting ACTION_SEND intents on Android

查看:300
本文介绍了捕获和Android上的拦截ACTION_SEND意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个pretty标准ACTION_SEND意图从我的应用程序内共享信息。 code是类似于下面的是:

Currently I have a pretty standard ACTION_SEND intent to share information from inside my app. Code is similar to what is below:

    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 SDK已经不是执行任务的Facebook应用程序的用户的手机上。有没有办法拦截的onClick为Facebook选项?谢谢!

Now, if the user has the Facebook app installed on their phone then Facebook shows up as an option for the intent chooser. However, I want to intercept the user's click on "Facebook" and use the Facebook SDK to carry out the task instead of the Facebook application already on the users phone. Is there a way to intercept the onClick for the Facebook option? Thanks!

推荐答案

下面是一个链接到我的博客文章有详细的解决方案,其中包括code和屏幕截图。

Here is a link to my blog post with a detailed solution, including code and screen shots.

http://clickclickclack.word$p$pss.com/2012/01/03/intercepting-androids-action_send-intents/

自该文章引述:

[我们]首先创建一个新ACTION_SEND意图和设置的类型
  文/平原。接下来,我们创建一个列表。我们拨打电话到包
  经理查询与ACTION_SEND意图活动
  注册。此调用returs ResolveInfo对象,每个列表
  相应的活动,声称处理发送设备上
  动作。

[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.

[随后,R]比粥启动ACTION_SEND意图,让它
  创建自己的对话框(一说我们将有没有控制权),我们
  将建立我们自己的AlertDialog.Builder。首先,我们给它一个
  标题,然后设置它的适配器,我们只是创建的列表适配器
  活动列表作为我们的数据集[。]

[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[.]

我们需要看看下一个重要的一块是听者的OnClick我们
  给了生成器。要找到用户单击的项目,我们使用
  adapter.getItem(INT这)方法。这将在返回的对象
  我们的原始列表的位置上,在这种情况下,一个ResolveInfo对象
  对应于所选择的活动。对于我在特定情况下,我
  只在乎分离的东西分成两组:Facebook和不
  Facebook的。要做到这一点,我只是检查所选活动的
  包名称中包含的Facebook。如果没有,我创建了一个新的
  ACTION_SEND意图,设置类名来选定活动,并
  启动它。但是,如果包名确实包含的Facebook',我
  实例化我个人PostToFacebookDialog对象,这将创造
  一个基本的Andr​​oid对话框,职位和取消按钮,并张贴到
  Facebook的直接使用图形API,从而绕过了用户的
  安装Facebook应用程序。

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.

这篇关于捕获和Android上的拦截ACTION_SEND意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆