我正在尝试捕捉结果Intent.createChooser
了解用户选择共享哪个应用程序。
我知道有很多与此相关的帖子:
- 如何知道用户在使用意图选择器时选择了哪个应用程序? https://stackoverflow.com/questions/10613953/how-to-know-which-application-the-user-chose-when-using-an-intent-chooser?rq=1
- https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1 https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
- 如何从 startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED); 获取用户选择? https://stackoverflow.com/questions/4417019/how-to-get-the-user-selection-from-startactivityforresultintent-createchooserf
- 在 Android 上捕获和拦截 ACTION_SEND 意图 https://stackoverflow.com/questions/8350531/capturing-and-intercepting-action-send-intents-on-android
但这些帖子有些旧了,我希望可能有一些新的进展。
我正在尝试实施一个分享行动 http://developer.android.com/training/sharing/shareaction.html而不让它出现在菜单中。最接近我想要的解决方案是由点击点击点击 http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/谁建议实施自定义应用程序选择器,但这似乎很严厉。另外,似乎可能有一些 Android 挂钩来获取所选的应用程序,例如ActivityChooserModel.OnChooseActivityListener https://android.googlesource.com/platform/frameworks/base/+/b9f286e1a31bfc13e9c0e9635294742d558014c6/core/java/android/widget/ShareActionProvider.java.
我的 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) http://developer.android.com/reference/android/content/Intent.html#createChooser%28android.content.Intent,%20java.lang.CharSequence,%20android.content.IntentSender%29添加),允许接收用户选择的结果。当您提供IntentSender
to createChooser
,发送者将通过选择器对话框收到通知ComponentName
由用户选择。它将在额外命名的文件中提供EXTRA_CHOSEN_COMPONENT http://developer.android.com/reference/android/content/Intent.html#EXTRA_CHOSEN_COMPONENT整数IntentSender
那是notified http://developer.android.com/reference/android/content/Intent.html#EXTRA_CHOSEN_COMPONENT_INTENT_SENDER.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)