Android 从 intentservice 共享 intent

标签 android android-intent service share intentservice

我想将我的应用中的多张图片分享给其他应用。在 Android 的开发者页面上,我发现:

Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, SavedImages);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "Share images to.."));

如何从 intentservice 使用此代码? 使用 intentservice 中的示例代码时,我的应用程序因 logcat 错误而崩溃:

Calling startActivity from outside of an Activity context requires the flag FLAG_ACTIVITY_NEW_TASK

所以我加了

shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

但我仍然遇到同样的错误,我的应用程序崩溃了。

如何使用 intentservice 的共享 intent?

最佳答案

这行代码

startActivity(Intent.createChooser(shareIntent, "Share images to.."));

这意味着您创建了一个 Intent 对象,该对象用于启动对话 Activity ,供用户选择哪个 Activity 来处理您的 shareIntent。因此,在这种情况下,显示选择器对话框 Activity 的 Intent 需要标志 FLAG_ACTIVITY_NEW_TASK
你可以试试:

Intent chooserIntent = Intent.createChooser(shareIntent, "Share images to..");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(chooserIntent);

关于Android 从 intentservice 共享 intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213094/

相关文章:

android - Android Data Binding 会取代 findViewById 吗?

PreferenceFragment 的 android 兼容性 API 替代品

java - Intent 获取数据仅在一项 Activity 中出错

angular - Angular 4 中的循环依赖

javascript - 如何从 angularjs 应用程序获取服务

java - 通过环境属性实现Spring服务

android - 使用 String[] 数据填充 Spinner

java:将 HashMap<String, String> 存储在 ArrayList<Object> 中

java - 应用程序在从 Intent 接收数据时崩溃

Android - 未找到处理 Intent 的 Activity { act=android.intent.action.VIEW - 尝试打开 PDF 文件