Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only Email apps to resolve an Intent

I have a problem .. I want only email activities to resolve intent ACTION.SEND but beside email I get other apps as well (e.g TubeMate) even though I have set the mime type as 'message/rfc822' ... Any idea how can I get Email applications to resolve it ..

like image 916
Waheed Khan Avatar asked Jun 28 '11 12:06

Waheed Khan


People also ask

How do I send intent from one app to another?

When you construct an intent, you must specify the action you want the intent to perform. Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type.


2 Answers

String recepientEmail = ""; // either set to destination email or leave empty Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:" + recepientEmail)); startActivity(intent); 

The point is to use ACTION_SENDTO as action and mailto: as data. If you want to let the user specify the destination email, use just mailto:; if you specify email yourself, use mailto:[email protected]

Suggested method filters all the application, that can send email(such as default email app or gmail)

like image 174
Mykhailo Gaidai Avatar answered Sep 23 '22 03:09

Mykhailo Gaidai


Here is how I send email with attachments (proved to work with attachments of various MIME types, even in the same email) and only allow email apps (it also has a solution for cases that no app support "mailto"). At first, we try and get activities that support mailto: format. If none are found then we get all activities that supports the message/rfc822 MIME type. We select the default app (if there is a default) or allow the user to select from the available apps. If no app supports mailto: and message/rfc822, then we use the default chooser.

public static void sendEmail(final Context p_context, final String p_subject, final String p_body, final ArrayList<String> p_attachments) {     try     {         PackageManager pm = p_context.getPackageManager();         ResolveInfo selectedEmailActivity = null;          Intent emailDummyIntent = new Intent(Intent.ACTION_SENDTO);         emailDummyIntent.setData(Uri.parse("mailto:[email protected]"));          List<ResolveInfo> emailActivities = pm.queryIntentActivities(emailDummyIntent, 0);          if (null == emailActivities || emailActivities.size() == 0)         {             Intent emailDummyIntentRFC822 = new Intent(Intent.ACTION_SEND_MULTIPLE);             emailDummyIntentRFC822.setType("message/rfc822");              emailActivities = pm.queryIntentActivities(emailDummyIntentRFC822, 0);         }          if (null != emailActivities)         {             if (emailActivities.size() == 1)             {                 selectedEmailActivity = emailActivities.get(0);             }             else             {                 for (ResolveInfo currAvailableEmailActivity : emailActivities)                 {                     if (true == currAvailableEmailActivity.isDefault)                     {                         selectedEmailActivity = currAvailableEmailActivity;                     }                 }             }              if (null != selectedEmailActivity)             {                 // Send email using the only/default email activity                 sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, selectedEmailActivity);             }             else             {                 final List<ResolveInfo> emailActivitiesForDialog = emailActivities;                  String[] availableEmailAppsName = new String[emailActivitiesForDialog.size()];                 for (int i = 0; i < emailActivitiesForDialog.size(); i++)                 {                     availableEmailAppsName[i] = emailActivitiesForDialog.get(i).activityInfo.applicationInfo.loadLabel(pm).toString();                 }                  AlertDialog.Builder builder = new AlertDialog.Builder(p_context);                 builder.setTitle(R.string.select_mail_application_title);                 builder.setItems(availableEmailAppsName, new DialogInterface.OnClickListener()                 {                     @Override                     public void onClick(DialogInterface dialog, int which)                     {                         sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, emailActivitiesForDialog.get(which));                     }                 });                  builder.create().show();             }         }         else         {             sendEmailUsingSelectedEmailApp(p_context, p_subject, p_body, p_attachments, null);         }     }     catch (Exception ex)     {         Log.e(TAG, "Can't send email", ex);     } }  protected static void sendEmailUsingSelectedEmailApp(Context p_context, String p_subject, String p_body, ArrayList<String> p_attachments, ResolveInfo p_selectedEmailApp) {     try     {         Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);          String aEmailList[] = { "[email protected]"};          emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);         emailIntent.putExtra(Intent.EXTRA_SUBJECT, null != p_subject ? p_subject : "");         emailIntent.putExtra(Intent.EXTRA_TEXT, null != p_body ? p_body : "");          if (null != p_attachments && p_attachments.size() > 0)         {             ArrayList<Uri> attachmentsUris = new ArrayList<Uri>();              // Convert from paths to Android friendly Parcelable Uri's             for (String currAttachemntPath : p_attachments)             {                 File fileIn = new File(currAttachemntPath);                 Uri currAttachemntUri = Uri.fromFile(fileIn);                 attachmentsUris.add(currAttachemntUri);             }             emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentsUris);         }          if (null != p_selectedEmailApp)         {             Log.d(TAG, "Sending email using " + p_selectedEmailApp);             emailIntent.setComponent(new ComponentName(p_selectedEmailApp.activityInfo.packageName, p_selectedEmailApp.activityInfo.name));              p_context.startActivity(emailIntent);         }         else         {             Intent emailAppChooser = Intent.createChooser(emailIntent, "Select Email app");              p_context.startActivity(emailAppChooser);         }     }     catch (Exception ex)     {         Log.e(TAG, "Error sending email", ex);     } } 
like image 41
Muzikant Avatar answered Sep 22 '22 03:09

Muzikant