【问题标题】:Automatically close application when i call intent.createChooser to send mail?当我调用 intent.createChooser 发送邮件时自动关闭应用程序?
【发布时间】:2014-09-19 12:14:35
【问题描述】:

在我的 android 应用程序中有一个发送邮件的按钮,当我使用 startActivity(Intent.createChooser(emailIntent, "Send Mail...")); 单击该发送按钮时,Gmail 将打开此行,然后我单击发送邮件。邮件会在我的申请关闭的同时成功发送,为什么?我需要留在同一页面吗?

try 
{
        String extpath=Environment.getExternalStorageDirectory() +"/NewFolder/DBName";
        File pathp=new File(extpath);
        Log.d("New Path", pathp.toString());

        long fileSize = pathp.length();
        if(fileSize > 0)
        {

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

            String address = "mailid@yourmail.com";
            String subject = "Database";
            String emailtext = "Please check the attached database and save it";

            emailIntent.setType("application/octet-stream");

            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address });

            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

            emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + pathp));

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

            startActivity(Intent.createChooser(emailIntent, "Send Mail..."));
        }
        else
        {
            Log.d("Error", "Attachment didn't attach ");
        }
    } 
    catch (Throwable t) 
    {
        Log.d("Error on sending mail", t.toString());
    }

当我运行此代码时发送的邮件然后应用程序将关闭。我不想关闭应用程序。帮助我提前谢谢。

【问题讨论】:

  • 贴一些相关代码
  • @kgandroid 我更新了我的问题,请阅读
  • @Sri 你有什么错误吗??
  • 您的代码似乎没问题。您可以发布您的 logcat 吗??
  • 我想你打电话给finish() 到Activity 或清除Activity back stack 这就是为什么你不能返回你的应用程序并且你的应用程序被关闭了......

标签: android email android-intent


【解决方案1】:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

Button button;
Session session;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    SendMailBySite bySite=new SendMailBySite();
    bySite.execute();

}


class SendMailBySite extends AsyncTask<Void, Void, Void>{  



    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog=new ProgressDialog(MainActivity.this);
        dialog.setMessage("Sending mail.....");
        dialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

          final String user="XXXXXXXX@gmail.com";//change accordingly  
          final String password="XXXXXXX";//change accordingly  

          String to="toXXXXXXXX@gmail.com";//change accordingly  

           //Get the session object  
          Properties props = new Properties();
          props.put("mail.smtp.host", "smtp.gmail.com");
             props.put("mail.smtp.socketFactory.port", "465");
             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
             props.put("mail.smtp.auth", "true");
             props.put("mail.smtp.port", "465");

           Session session = Session.getDefaultInstance(props,  
            new javax.mail.Authenticator() {  
              protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(user,password);  
              }  
            });  

           //Compose the message  
            try {  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(user));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
             message.setSubject("Mobiappsdevelper");  
             message.setText("This is simple program of sending email using JavaMail API");  

            //send the message  
             Transport.send(message);  



             } catch (MessagingException e) {e.printStackTrace();} 


        return null;


    }  
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();
         Toast.makeText(getApplicationContext(), "message sent successfully...", 3000).show();
    }
    }  
}

【讨论】:

    【解决方案2】:

    您可以使用此代码:

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 2016-10-30
      • 2014-02-03
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 2013-10-30
      • 1970-01-01
      相关资源
      最近更新 更多