List of usage examples for android.content Intent EXTRA_ALTERNATE_INTENTS
String EXTRA_ALTERNATE_INTENTS
To view the source code for android.content Intent EXTRA_ALTERNATE_INTENTS.
Click Source Link
From source file:ru.dublgis.androidhelpers.DesktopUtils.java
public static boolean sendEmail(final Context ctx, final String to, final String subject, final String body, final String[] attachment, final String authorities) { try {//from w ww .j a va 2 s. c o m final String[] recipients = new String[] { to }; final Intent intent = new Intent( attachment.length > 1 ? Intent.ACTION_SEND_MULTIPLE : Intent.ACTION_SENDTO); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, recipients); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, body); boolean grant_permissions_workaround = false; final ArrayList<Uri> uri = new ArrayList<>(); if (attachment.length > 0) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { for (final String fileName : attachment) { uri.add(Uri.fromFile(new File(fileName))); } } else { // Android 6+: going the longer route. // For more information, please see: // http://stackoverflow.com/questions/32981194/android-6-cannot-share-files-anymore intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); grant_permissions_workaround = true; for (final String fileName : attachment) { uri.add(FileProvider.getUriForFile(ctx, authorities, new File(fileName))); } } // Should not put array with only one element into intent because of a bug in GMail. if (uri.size() == 1) { intent.putExtra(Intent.EXTRA_STREAM, uri.get(0)); } else { intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uri); } } final IntentResolverInfo mailtoIntentResolvers = new IntentResolverInfo(ctx.getPackageManager()); mailtoIntentResolvers .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", to, null))); final Intent chooserIntent; if (mailtoIntentResolvers.isEmpty()) { chooserIntent = Intent.createChooser(intent, null); } else { final IntentResolverInfo messageIntentResolvers = new IntentResolverInfo(ctx.getPackageManager()); messageIntentResolvers .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("sms", "", null))); messageIntentResolvers .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mms", "", null))); messageIntentResolvers .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("tel", "", null))); mailtoIntentResolvers.removeSamePackages(messageIntentResolvers.getResolveInfos()); final List<Intent> intentList = new ArrayList<>(); for (final ActivityInfo activityInfo : mailtoIntentResolvers.getResolveInfos()) { final String packageName = activityInfo.getPackageName(); final String name = activityInfo.getName(); // Some mail clients will not read the URI unless this is done. // See here: https://stackoverflow.com/questions/24467696/android-file-provider-permission-denial if (grant_permissions_workaround) { for (int i = 0; i < uri.size(); ++i) { try { ctx.grantUriPermission(packageName, uri.get(i), Intent.FLAG_GRANT_READ_URI_PERMISSION); } catch (final Throwable e) { Log.e(TAG, "grantUriPermission error: ", e); } } } final Intent cloneIntent = (Intent) intent.clone(); cloneIntent.setComponent(new ComponentName(packageName, name)); intentList.add(cloneIntent); } final Intent targetIntent = intentList.get(0); intentList.remove(0); chooserIntent = Intent.createChooser(targetIntent, null); if (!intentList.isEmpty()) { final Intent[] extraIntents = intentList.toArray(new Intent[intentList.size()]); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); } else { chooserIntent.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, extraIntents); } } } chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(chooserIntent); return true; } catch (final Throwable exception) { Log.e(TAG, "sendEmail exception: ", exception); return false; } }