Issue
After Android 12 update share intent not working in Samsung S10 device.This code is properly working in below Android version 12 devices but could not find the reason why in android 12 is filtering out.
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);
}
I want to share my Programs to other Apps. I unable to share my program to other apps. Share intent not opened, After android 12 update.
Solution
Missing FLAG_IMMUTABLE To declare that a given PendingIntent object is mutable or immutable, use the PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE flag, respectively. Above code Just i added this lines: Working fine now
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);
}
Answered By - Shobana Velmurugan
Answer Checked By - Mildred Charles (JavaFixing Admin)