I searched stack overflow and the google documentation for email intents and found that code to be the go to code:
However, everytime I click "submit" in my activity I choose gmail. It opens a new email in gmail, but it only puts the receipient address. The subject and the text(name, email address and feedback text) are missing.
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
I implemented that as the following code. Everything after the else is to have a AlertDialog pop up if there is no app installed.
public class EmailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitvity_email);
final EditText nameField = (EditText) findViewById(R.id.editTextName);
final EditText addressField = (EditText) findViewById(R.id.editTextEmail);
final EditText subjectField = (EditText) findViewById(R.id.editTextSubject);
final EditText feedbackField = (EditText) findViewById(R.id.editTextFeedback);
final Button submitFeedback = (Button) findViewById(R.id.buttonSubmitFeedback);
final String name = nameField.getText().toString();
final String address = addressField.getText().toString();
final String subject = subjectField.getText().toString();
final String feedback = feedbackField.getText().toString().concat(name).concat(address);
submitFeedback.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
composeFeedback(subject, feedback);
}
});
}
public void composeFeedback(String subject, String feedback){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + "mydeveloperemail#gmail.com"));
intent.putExtra(Intent.EXTRA_TEXT,feedback);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
final Drawable fail = getResources().getDrawable(R.drawable.ic_fail);
AlertDialog.Builder builder = new AlertDialog.Builder(EmailActivity.this);
builder.setTitle("Fehler");
builder.setIcon(fail);
builder.setMessage("Keine Email App verfügbar!");
builder.setCancelable(false);
builder.setNeutralButton("Okay", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Edit: deleted intent filter as #CommonsWare pointed out in his answer
ACTION_SENDTO is not required to use any extras. Put your data in the mailto: Uri, the way a Web page would.
In the android manifest i declared an intent filter to the activtiy
Unless you are writing an email app, like Gmail, this <intent-filter> is both unnecessary and damaging to users.
Related
I am trying to send an email to a user with Android which will include a code. For whatever reason, it does not work.
I have followed this tutorial
What is the problem?
This is the code:
public class PWReset extends AppCompatActivity {
EditText getMail;
Button Continue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pwreset);
getMail=findViewById(R.id.Mail);
Continue=findViewById(R.id.PWR2);
Continue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String Code = UUID.randomUUID().toString().substring(0, 5);
//After checking if the email is registered in the DB
resetPassword(getMail.getText().toString(), Code);
Intent intent = new Intent(PWReset.this, PWReset2.class);
intent.putExtra("code", Code);
startActivity(intent);
}
});
}
public void resetPassword(String Mail, String code){
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{getMail.getText().toString()});
email.putExtra(Intent.EXTRA_SUBJECT, "Password reset");
email.putExtra(Intent.EXTRA_TEXT, "You have requested a password reset.\nHere is the code for resetting your password:" + code +
"\nIf you have not requested a password reset, ignore this message.");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "gmail"));
}
}
I want to share text, Image with clickable links in Gmail, facebook, twitter etc using intents.below is my code but it does not work. I have also put in link text in String resource file but does not work.The sharing functions as it should however when shared in a Gmail, Facebook, Twitter, etc. the link is ignored and the Gmail or tweet shows only plain text like this
private void share(Uri bmpUri)
{
String hyperlink_url="https://www.google.co.in/?gfe_rd=cr&ei=_3edWb68K4rT8gfx_pCoDw";
String text = "here";
String body = "Click";
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hey! I just saw ABC is having XYZ through the my App! We should check it out."+ "\n" + Html.fromHtml(body));
shareIntent.setType("text/html");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "");
//String[] mimetypes = {"image/*", "video/*","text/html"};
// shareIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(shareIntent, 0);
List<String> appNames = new ArrayList<String>();
for (ResolveInfo info : activities) {
appNames.add(info.loadLabel(getPackageManager()).toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Complete Action using...");
builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.equals("com.facebook.katana")) {
// Facebook was chosen
// shareToFacebook();
} else if (info.activityInfo.packageName.equals("com.twitter.android")) {
// Twitter was chosen
shareIntent.setPackage(info.activityInfo.packageName);
startActivity(shareIntent);
} else {
// start the selected activity
shareIntent.setPackage(info.activityInfo.packageName);
startActivity(shareIntent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
This question already has answers here:
Sending Email in Android using JavaMail API without using the default/built-in app
(25 answers)
Closed 6 years ago.
I've a feedback form and buttons for send and cancel. How can I make the feedback send responds to a default email when the send button is clicked? And how can I set a the default email. Is this possible? Please let me know. Thank you.
Here's is my java for the feedback form :
#Override
public void onClick(View arg0) {
// Create custom dialog object
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.activity_main);
// Set dialog title
dialog.setTitle("Feedback");
dialog.show();
Button button1 = (Button) dialog.findViewById(R.id.button1);
// if decline button is clicked, close the custom dialog
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Button button2 = (Button) dialog.findViewById(R.id.button2);
// if decline button is clicked, close the custom dialog
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Open your email client programatically using a chooser:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
To send email programatically without using chooser,you can use this link.
You can use following snippet:
private void sendEmail(String email) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
String aEmailList[] = {email};
emailIntent.setData(Uri.parse("mailto:")); // only email apps should handle this
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
String feedback_msg = getString(R.string.feedback_msg);
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<i><font color='your color'>" + feedback_msg + "</font></i>"));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.feedback_msg));
PackageManager packageManager = getActivity().getPackageManager();
boolean isIntentSafe = emailIntent.resolveActivity(packageManager) != null;
if (isIntentSafe) {
startActivity(emailIntent);
} else {
Toast.makeText(getActivity(), R.string.email_app_not_installed, Toast.LENGTH_SHORT).show();
}
}
}
I'm trying to start the SMS activity with the phone number from a scanned QR Code filled in. So far I've been able to get the activity to launch but the number and message fields are blank. Here's what I have, currently this is crashing the app:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
Uri uri = Uri.parse(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("vnd.android-dir/mms-sms");
i.setData(uri);
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
The String smsUri contains the scanned string from the QR Code, "SMSTO:666-666-1234:hello". How can I get the SMS Activity to launch with the phone number and the message already entered into the number and body fields?
I saw this post:
Sending SMS using Intent does not add recipients on some devices
Would I need to parse the QR Code result myself and break it into the phone number and message, then add those as Extras like that example?
Got it!
Since I can't answer my own question yet here it is:
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
public class AndroidEmailActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittextEmailAddress = (EditText) findViewById(R.id.email_address);
final EditText edittextEmailSubject = (EditText) findViewById(R.id.email_subject);
final EditText edittextEmailText = (EditText) findViewById(R.id.email_text);
Button buttonSendEmail_intent = (Button) findViewById(R.id.sendemail_intent);
buttonSendEmail_intent.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String emailAddress = edittextEmailAddress.getText().toString();
String emailSubject = edittextEmailSubject.getText().toString();
String emailText = edittextEmailText.getText().toString();
String emailAddressList[] = { emailAddress };
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
intent.putExtra(Intent.EXTRA_TEXT, emailText);
startActivity(Intent.createChooser(intent,
"Choice App t send email:"));
}
});
}
}
This is my code.I am not getting any emails.I need to send emails through this intent method.how can I acheive that?Where I went wrong?pls give some suggestions.
I am getting the error when I click the Send email button as "No applications can perform this action."
This is my code to send emails:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.share_email_subject));
sendIntent.setType("message/rfc822");
activity.startActivity(Intent.createChooser(sendIntent, "Choose Email Client"));
Try removing the plain/text type?
It is Good practice to work with original device rather than on Virtual Device. I suggest you try this code block on real device.
Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("plain/text");
startActivity(Intent.createChooser(sendEmail, "Email:"));
If you want to see more on Email with Intent then refer to this post:
http://androidtutforbeginner.blogspot.com/2012/03/send-email-with-intent-in-android.html