如何在发送邮件后再次移动MainActivity?

时间:2014-02-10 09:31:42

标签: android email android-intent

我正在开发一个应用程序,我在其中通过意图帮助发送电子邮件。我成功地从MainActivity发送邮件,但问题是,当我点击发送按钮发送电子邮件时,我将转到应用程序之外。但在发送电子邮件后,我想再次使用MainActivity。这是我的代码。

 protected void sendEmail() {
    String[] TO = {"xxx@gmail.com"};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
     emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");

      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "yyyy");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "zzzz.");

      try {
             startActivity(Intent.createChooser(emailIntent, "Send mail..."));
             finish();
          } catch (android.content.ActivityNotFoundException ex) {
             Toast.makeText(MainActivity.this, 
             "There is no email client installed.", Toast.LENGTH_SHORT).show();
          }

}

2 个答案:

答案 0 :(得分:2)

您可以删除finish()或使用startActivityForResult

答案 1 :(得分:1)

而不是startActivity(Intent.createChooser(emailIntent, "Send mail..."));

您可以使用

startActivityForResult(Intent.createChooser(emailIntent, 1));

您的活动在onActivityResult()回调中收到它。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}