user1503346
user1503346

Reputation: 89

how to check whether email client installed on device

I need to check whether email client is installed on a device or not. I have used the following code but it does not work for me.

public boolean isIntentAvailable() {
    final PackageManager packageManager = getApplicationContext().getPackageManager();
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_META_DATA);
    return list.size() > 0;
} 

Upvotes: 8

Views: 5051

Answers (5)

234fly
234fly

Reputation: 1

this is my way,it is works in xiaomi(android 13) phone:

  1. add these in the manifest.xml:

       <queries>
           <intent>
             <action android:name="android.intent.action.SENDTO" />
             <data android:scheme="*" />
          </intent>
      </queries>
    
  2. in the View.OnClickListener's onClick method:

     val mailIntent = Intent.makeMainSelectorActivity(
                            Intent.ACTION_MAIN,
                            Intent.CATEGORY_APP_EMAIL
                         )
       mailIntent.data = Uri.parse("mailto:")
       val emails = arrayOf(data?.serviceEmail)
       mailIntent.putExtra(Intent.EXTRA_EMAIL, emails)
       if (mailIntent.resolveActivity(packageManager) != null) {
             startActivity(mailIntent)
       } else {
             showTips("please check whether you have an email app")
       }
    

Upvotes: 0

DeadPool
DeadPool

Reputation: 71

The proper way to check for email clients on all devices (onePlus, Moto, Samsung etc) is as follows:

In your manifest file you need to add the following code after application tag.

<queries>
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="*" />
        </intent>
</queries>

You can replace the scheme as required. After doing this, you need to use the following code on your button click (or whatever event you have email associated with):

Intent email = new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:"));
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
email.putExtra(Intent.EXTRA_SUBJECT, "Any Subject");
if (email.resolveActivity(getPackageManager()) != null)         
    startActivity(email);
else 
   // A toast to display any message when email client is not available

Upvotes: 0

Badr Bujbara
Badr Bujbara

Reputation: 8691

use this method:

    private fun sendEmail(to: Array<String>) {
        val intent = Intent(Intent.ACTION_SENDTO)
        intent.data = Uri.parse("mailto:") // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, to)
//        intent.putExtra(Intent.EXTRA_SUBJECT, subject)
        if (intent.resolveActivity(requireContext().packageManager) != null) {
            startActivity(intent)
        }
    }

To be able to check for email clients when targeting api 30, add "queries" to the Manifest:

<queries>
    <intent>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="*" />
    </intent>
</queries>

Upvotes: 6

eyal
eyal

Reputation: 2409

For email client, specifically, you should use:

intent.setType("message/rfc822");

instead of:

intent.setType("text/html");

Upvotes: 3

Nitin
Nitin

Reputation: 1451

Use this, works for me :

public static boolean isMailClientPresent(Context context){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);

    if(list.size() == 0)
        return false;
    else 
        return true;
}

Upvotes: 12

Related Questions