我试图捕获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
提供.