java - (共享 Intent )外部共享在 Android 12 中不起作用

标签 java android share

Android 12 更新后共享 Intent 在三星 S10 设备中不起作用。此代码在低于 Android 版本 12 的设备中正常工作,但找不到原因
android 12 正在过滤掉。

        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        // (Optional) If you want a preview title, set it with Intent.EXTRA_TITLE
        sharingIntent.putExtra(Intent.EXTRA_TITLE, str_title);
        sharingIntent.putExtra(Intent.EXTRA_TEXT, "https://www.cyranolab.media/msg/?q=507dddd6-8e43-11ec-9d11-061d7e6be791");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, str_title);

        Intent receiver = new Intent(getActivityContext, UserSelectedShareBroadcast.class);
  PendingIntent pendingIntent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity(getActivityContext,
                    0, receiver, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        }else {
            pendingIntent = PendingIntent.getActivity(getActivityContext,
                    0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
        }

        Intent openInChooser = Intent.createChooser(sharingIntent, "Choose", pendingIntent.getIntentSender());
        List<LabeledIntent> intentList = new ArrayList<>();

        Intent externalEmailIntent = new Intent(getActivityContext, ExternalEmailShareActivity.class);
        externalEmailIntent.putExtra("programId", programId);
        externalEmailIntent.putExtra("sharedResourceId", sharedResourceId);
        externalEmailIntent.putExtra("INBOX", "Inbox");
        intentList.add(new LabeledIntent(externalEmailIntent, "Package Name", "Email to", R.drawable.ic_mail_outline));
        // convert intentList to array
        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[0]);

        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        int REQUEST_SHARED_URL = 2;
        getActivityContext.startActivityForResult(openInChooser, REQUEST_SHARED_URL);
    }
我想将我的程序分享给其他应用程序。我无法将我的程序分享给其他应用程序。共享 Intent 未打开,Android 12 更新后。

最佳答案

缺少 FLAG_IMMUTABLE
要声明给定的 PendingIntent 对象是可变的或不可变的,请分别使用 PendingIntent.FLAG_MUTABLE 或 PendingIntent.FLAG_IMMUTABLE 标志。上面的代码只是我添加了这行:现在工作正常

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity(getActivityContext,
                    0, receiver, PendingIntent.FLAG_IMMUTABLE);
        }else {
            pendingIntent = PendingIntent.getActivity(getActivityContext,
                    0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
        }

关于java - (共享 Intent )外部共享在 Android 12 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71122162/

相关文章:

android - 将过滤后的项目加载到搜索输出

android - 将来自不同项目的包组合到eclipse中的单个项目中

c# - 使用共享源在位图上绘图

javascript - 是否可以在智能手机上触发共享菜单(通过 HTML/JS)?

java - 在 Android 中使用反射是一个糟糕的设计吗?

android - Eclipse:转换为 Dalvik 格式失败,出现错误 1

java - 如何用 Java (Android) 编写这个 Objective-C Hmac 签名函数?

java - 使用分割字符串自定义共享按钮中的 Intent 或如何分割字符串并替换奇数逗号

java - 添加 JPanel 时 JTable 列标题消失

java - 如何在 android 中设置单个 listView 项目的样式?