2

I am just a beginner in app dev and referring the documentation wanted to build an email sending app using Implicit Intents. this works well when we write

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");

but when we write the following code to filter out only the mailing app it makes a toast saying that No app is installed which i have made in order to prevent my app from misbehaving.

Below is the simple code pls read it for once

    package com.example.emailsender;
//necessary imports

public class MainActivity extends AppCompatActivity {
//necessary variables edittext em, subject& button sub(submit)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    em = findViewById(R.id.editTextTextEmailAddress);


    subject = findViewById(R.id.sub);
    sub = findViewById(R.id.button);

    sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String ema=em.getText().toString();
            String emails[]=ema.split(",");
            String s = subject.getText().toString();
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(Intent.EXTRA_EMAIL, emails);
            intent.putExtra(Intent.EXTRA_SUBJECT, s);
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
            else
                Toast.makeText(MainActivity.this, "No app is installed", Toast.LENGTH_SHORT).show();
        }

    });
}

}

  • 1
    Have you seen: [Android: Email Intent ACTION_SENDTO doesn't work unless you add an intent filter](https://stackoverflow.com/q/68120288/295004) – Morrison Chang Aug 26 '22 at 22:59
  • 1
    Remove `resolveActivity()`, and instead wrap your `startActivity()` call in a `try`/`catch` block. You cannot use `resolveActivity()` on Android 11+ without extra stuff in your manifest, and in this case, you do not need it. – CommonsWare Aug 26 '22 at 23:20
  • Commanware can you change my code with try and catch statements so that i can understand better . thanks for the help – Nishant Khatri Aug 28 '22 at 16:17

0 Answers0