Рет қаралды 14,934
Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to send an email by clicking on a button.
You will learn how to use the Android ACTION_SENDTO and ACTION_SEND Intent to be able to send an email from your app.
Here are the steps:
1- Open up Android Studio.
2- Inside Button onClickListener declare Android ACTION_SENDTO Intent like this.
Intent intent = new Intent(Intent.ACTION_SENDTO);
3- Next, you need to set the SendTo email field like this.
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email.getText().toString()});
4- You need to set the subject of the email like this.
intent.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
5- You also need to set the email body like this.
intent.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
6- You need to add the following code in order to show only the email applications inside the device that can support this action.
intent.setData(Uri.parse("mailto:"));
7- Before you start the Intent, you need to check in your device whether there is an email application that can support this action. If you don't do that and your device doesn't have an application, the app will crash. Here how you can do the checking.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "There is no application that support this action",
Toast.LENGTH_SHORT).show();
}
If the device has an application that supports this action, then it will call startActivity(intent). Otherwise, it will show a Toast message with an error.
8- Now build and run the app to see the result :)
Links:
Android ACTION_DIAL Intent: developer.android.com/guide/c...
Website: www.codingdemos.com
FaceBook: / codingdemos