在我的 android 应用程序中,我需要允许用户共享一些元素。我想要启用的共享方法是 Facebook、twitter 和用户输入电子邮件和一些信息的自定义表单,应用程序会将元素发送到此电子邮件。到目前为止我的代码是:
private void share(List<String> appNames, String imagePath, String text) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime type
for(String nameApp : appNames){
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
targetedShare.putExtra(Intent.EXTRA_TEXT, text);
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), getString(R.string.share_dialog_title));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
其中 appNames
是 private static final String[] sharingMethods = {"face", "twi"};
这按预期工作并允许用户在 Facebook 和 Twitter 之间进行选择,但我不知道如何向选择器添加自定义 Activity 。 这可能还是我应该建立自己的选择器 Activity ?谁能指出我正确的方向?
谢谢!
最佳答案
据我了解,在显示 Android 的共享对话框时,您需要显示自己的自定义 Activity 以及 Facebook 和 Twitter。
为此,您需要在 list 中为该 Activity 及其 mime 类型添加操作“android.content.Intent.ACTION_SEND”。例如:
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
有关更多信息,请查看此 link
关于Android:向选择器 Intent 添加自定义选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765621/