java – 从Intent.createChooser获取选择的应用程序

我试图捕获Intent.createChooser的结果,以了解用户选择共享的应用程序.

我知道有很多与此相关的帖子:

> How to know which application the user chose when using an intent chooser?
> https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
> How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent,“Open file using…”),APP_PICKED);?
> Capturing and intercepting ACTION_SEND intents on Android

但是这些帖子有些陈旧,我希望可能会有一些新的发展.

我正在尝试实现share action,而不会出现在菜单中.我想要的最接近的解决方案由ClickClickClack提供,他建议实施自定义应用程序选择器,但这似乎很重要.此外,似乎可能会有一些Android钩子来获取所选的应用程序,如ActivityChooserModel.OnChooseActivityListener.

我在MainActivity中有以下代码,但onShareTargetSelected方法永远不会被调用.

Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT,shareMessage());
    sendIntent.setType("text/plain");

    Intent intent = Intent.createChooser(sendIntent,getResources().getText(R.string.share_prompt));

    ShareActionProvider sap = new ShareActionProvider(this);
    sap.setShareIntent(sendIntent);
    sap.setonShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider source,Intent intent) {
            System.out.println("Success!!");
            return false;
        }
    });

    startActivityForResult(intent,1);

解决方法

从API级别22开始,它现在实际上是可能的.在Android 5.1中,添加了一种方法( createChooser (Intent target,CharSequence title,IntentSender sender)),允许接收用户选择的结果.当您提供IntentSender来创建选择器时,选择器对话框将通过用户选择的ComponentName通知发件人.它将在 notified的IntentSender中以额外的名称 EXTRA_CHOSEN_COMPONENT提供.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...