在android中从服务启动活动时出错

时间:2012-08-11 18:06:05

标签: android android-activity android-service

我有一个收听来电的接收器。从这个接收器我开始一个服务,将发送电子邮件(使用Android Intent),但我得到一个例外(PLZ引用日志猫的日志)。

  

java.lang.RuntimeException:无法启动服务   com.pack.android.service.EmailService@40519818与Intent {   CMP = com.pack.android.email.service / com.pack.android.service.EmailService   :android.util.AndroidRuntimeException:从中调用startActivity()   在Activity上下文之外需要FLAG_ACTIVITY_NEW_TASK   旗。这真的是你想要的吗?

我设置了'FLAG_ACTIVITY_NEW_TASK'和'FLAG_FROM_BACKGROUND'。这不是用户活动。我正在尝试使用意图INTENT.SENDTO / SEND开始现有活动(发送电子邮件)。

奇怪的是错误: 引起:android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.SENDTO flg = 0x10000004(有额外内容)}

我在这里缺少什么?

我在使用Java Mail API发送电子邮件时遇到的另一个问题是邮件正在正常发送,它看起来像是什么但不知何故邮件似乎永远不会出现在邮箱中。我指的是以下链接,无需意图发送电子邮件: Send email without intent

如果有人可以对此提出一些见解,那将是gr8。

代码如下: 呼叫接收者: -

public class CallReceiver extends BroadcastReceiver
{
    private static final String LOG_TAG = "CallReceiver";
    private static final String ACTION = "android.intent.action.PHONE_STATE";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        System.out.println("--------Inside onReceive of CallReceiver--------");
        Log.d(LOG_TAG, "Inside onReceive");

        if(intent.getAction().equals(ACTION)){
            Log.d(LOG_TAG, "-----Criteria matched-----");
            Intent emailIntent = new Intent(context, EmailService.class);
            emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
            context.startService(emailIntent);
        }
    }
}

电子邮件服务: -

public class EmailService extends Service {

    private static final String LOG_TAG = "EmailService";

    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d(LOG_TAG, "-------On create called-------");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.d(LOG_TAG, "-----Inside On Start Command-----");
        MailService mailer = new MailService("24adithya@gmail.com","24adithya@gmail.com","Test","Hi This is 5554 from Mail Service", "<b>HtmlBody</b>");
        try {
            boolean success = mailer.sendAuthenticated();
            Log.d(LOG_TAG, String.valueOf(success));
        } catch (Exception e) {
            Log.e(LOG_TAG, "Failed sending email.", e);
        }

        try {   
            GMailSender sender = new GMailSender("%MyUserId%@gmail.com", "%Mypassword%");
            sender.sendMail("Test Subject",   
                    "Hi, This is 5554 from Gmail Sender",   
                    "24adithya@gmail.com",   
                    "24adithya@gmail.com");   
        } catch (Exception e) {   
            Log.e("SendMail", e.getMessage(), e);   
        }

        /*Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
//      emailIntent.setType("plain/text");

        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] { "24adithya@gmail.com" });

        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "Email from Intent");

        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                "Hi, This is 5554 from intent");

//      getApplicationContext().startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        Log.d(LOG_TAG, "context = " + getApplicationContext());
        startActivity(emailIntent);*/


        return Service.START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d(LOG_TAG, "-------On destroy called-------");
    }
}

邮件服务:

public class MailService {
    private static final String LOG_TAG = "MailService";
    private String toList;
    private String ccList;
    private String bccList;
    private String subject;
    final private static String SMTP_SERVER = "smtp.gmail.com";
    private String from;
    private String txtBody;
    private String htmlBody;
    private String replyToList;

    private boolean authenticationRequired = false;

    public MailService(String from, String toList, String subject, String txtBody, String htmlBody) {
        this.txtBody = txtBody;
        this.htmlBody = htmlBody;
        this.subject = subject;
        this.from = from;
        this.toList = toList;
        this.ccList = null;
        this.bccList = null;
        this.replyToList = null;
        this.authenticationRequired = true;
    }

    public boolean sendAuthenticated() throws AddressException, MessagingException {
        authenticationRequired = true;
        return send();
    }

    /**
     * Send an e-mail
     * 
     * @throws MessagingException
     * @throws AddressException
     */
    public boolean send() throws AddressException, MessagingException {

        Log.d(LOG_TAG, "Inside Send !");
        Properties props = new Properties();

        // set the host smtp address
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", SMTP_SERVER);
        props.put("mail.smtp.auth", "true"); // needed for gmail
        props.put("mail.smtp.port", "465");  // gmail smtp port - 587
//        props.put("mail.user", from);

        props.put("mail.smtp.socketFactory.port", "465");   
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        Session session;

        if (authenticationRequired) {
            Authenticator auth = new SMTPAuthenticator();
            Log.d(LOG_TAG, "auth = "+auth);
            session = Session.getDefaultInstance(props, auth);
        } else {
            session = Session.getDefaultInstance(props, null);          
        }

        // get the default session
        session.setDebug(true);

        // create message
        Message msg = new javax.mail.internet.MimeMessage(session);

        // set from and to address
        try {
            msg.setFrom(new InternetAddress(from, from));
            msg.setReplyTo(new InternetAddress[]{new InternetAddress(from,from)});
        } catch (Exception e) {
            msg.setFrom(new InternetAddress(from));
            msg.setReplyTo(new InternetAddress[]{new InternetAddress(from)});
        }

        // set send date
        msg.setSentDate(Calendar.getInstance().getTime());

        // parse the recipients TO address
        java.util.StringTokenizer st = new java.util.StringTokenizer(toList, ",");
        int numberOfRecipients = st.countTokens();

        javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[numberOfRecipients];

        int i = 0;
        while (st.hasMoreTokens()) {
            addressTo[i++] = new javax.mail.internet.InternetAddress(st
                    .nextToken());
        }
        msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);

        // parse the replyTo addresses
        if (replyToList != null && !"".equals(replyToList)) {
            st = new java.util.StringTokenizer(replyToList, ",");
            int numberOfReplyTos = st.countTokens();
            javax.mail.internet.InternetAddress[] addressReplyTo = new javax.mail.internet.InternetAddress[numberOfReplyTos];
            i = 0;
            while (st.hasMoreTokens()) {
                addressReplyTo[i++] = new javax.mail.internet.InternetAddress(
                        st.nextToken());
            }
            msg.setReplyTo(addressReplyTo);
        }

        // parse the recipients CC address
        if (ccList != null && !"".equals(ccList)) {
            st = new java.util.StringTokenizer(ccList, ",");
            int numberOfCCRecipients = st.countTokens();

            javax.mail.internet.InternetAddress[] addressCC = new javax.mail.internet.InternetAddress[numberOfCCRecipients];

            i = 0;
            while (st.hasMoreTokens()) {
                addressCC[i++] = new javax.mail.internet.InternetAddress(st
                        .nextToken());
            }

            msg.setRecipients(javax.mail.Message.RecipientType.CC, addressCC);
        }

        // parse the recipients BCC address
        if (bccList != null && !"".equals(bccList)) {
            st = new java.util.StringTokenizer(bccList, ",");
            int numberOfBCCRecipients = st.countTokens();

            javax.mail.internet.InternetAddress[] addressBCC = new javax.mail.internet.InternetAddress[numberOfBCCRecipients];

            i = 0;
            while (st.hasMoreTokens()) {
                addressBCC[i++] = new javax.mail.internet.InternetAddress(st
                        .nextToken());
            }

            msg.setRecipients(javax.mail.Message.RecipientType.BCC, addressBCC);
        }

        msg.setSubject(subject);
        Multipart mp = new MimeMultipart("related");

        // set body message
        MimeBodyPart bodyMsg = new MimeBodyPart();
        bodyMsg.setText(txtBody);
        mp.addBodyPart(bodyMsg);

        msg.setContent(mp);

        // send it
        try {
            Address[] fromAddressArray = msg.getFrom();
            for(Address address : fromAddressArray)
            {
                Log.d(LOG_TAG,"from = " + address.toString()+", ");
            }

            Address[] recipientAddressArray = msg.getAllRecipients();
            for(Address address : recipientAddressArray)
            {
                Log.d(LOG_TAG,"recipient = " + address.toString()+", ");
            }

            Address[] replyToAddressArray = msg.getReplyTo();
            for(Address address : replyToAddressArray)
            {
                Log.d(LOG_TAG,"replyTo = " + address.toString()+", ");
            }

            javax.mail.Transport.send(msg);
            return true;
        } catch (Exception e) {
            Log.e(LOG_TAG, e.getMessage());
            e.printStackTrace();
        }
        return false;
    }

    /**
     * SimpleAuthenticator is used to do simple authentication when the SMTP
     * server requires it.
     */
    private static class SMTPAuthenticator extends javax.mail.Authenticator {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {

            String username = "%MyUserId%@gmail.com";
            String password = "%Mypassword%";

            return new PasswordAuthentication(username, password);
        }
    }

    public String getToList() {
        return toList;
    }

    public void setToList(String toList) {
        this.toList = toList;
    }

    public String getCcList() {
        return ccList;
    }

    public void setCcList(String ccList) {
        this.ccList = ccList;
    }

    public String getBccList() {
        return bccList;
    }

    public void setBccList(String bccList) {
        this.bccList = bccList;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setTxtBody(String body) {
        this.txtBody = body;
    }

    public void setHtmlBody(String body) {
        this.htmlBody = body;
    }

    public String getReplyToList() {
        return replyToList;
    }

    public void setReplyToList(String replyToList) {
        this.replyToList = replyToList;
    }

    public boolean isAuthenticationRequired() {
        return authenticationRequired;
    }

    public void setAuthenticationRequired(boolean authenticationRequired) {
        this.authenticationRequired = authenticationRequired;
    }

}

Gmail发件人:

public class GMailSender extends javax.mail.Authenticator {

    private static final String LOG_TAG = "GmailSenderService";
    private String mailhost = "smtp.gmail.com"; 
    private String popMailHost = "pop.gmail.com";
    private String user;   
    private String password;   
    private Session session;   

    static {   
        Security.addProvider(new JSSEProvider());   
    }  

    public GMailSender(String user, String password) {   
        this.user = user;   
        this.password = password;   

        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.host", mailhost);   
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", "465");   
        props.put("mail.smtp.socketFactory.port", "465");   
        props.put("mail.smtp.socketFactory.class",   
                "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, this);
        session.setDebug(true);
    }   

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

        Address[] fromAddressArray = message.getFrom();
        for(Address address : fromAddressArray)
        {
            Log.d(LOG_TAG,"from = " + address.toString()+", ");
        }

        Address[] recipientAddressArray = message.getAllRecipients();
        for(Address address : recipientAddressArray)
        {
            Log.d(LOG_TAG,"recipient = " + address.toString()+", ");
        }

        Address[] replyToAddressArray = message.getReplyTo();
        for(Address address : replyToAddressArray)
        {
            Log.d(LOG_TAG,"replyTo = " + address.toString()+", ");
        }

        Transport.send(message);   
        }catch(Exception e){

        }
    }   

    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
}

Log Cat:

08-15 00:04:23.676: D/EmailService(522): -------On create called-------
08-15 00:04:23.686: D/EmailService(522): -----Inside On Start Command-----
08-15 00:04:23.686: D/MailService(522): Inside Send !
08-15 00:04:23.706: D/MailService(522): auth = com.pack.android.other.MailService$SMTPAuthenticator@40518f80
08-15 00:04:23.716: I/System.out(522): DEBUG: setDebug: JavaMail version 1.4.1
08-15 00:04:23.776: D/MailService(522): from = "24adithya@gmail.com" <24adithya@gmail.com>, 
08-15 00:04:23.776: D/MailService(522): recipient = 24adithya@gmail.com, 
08-15 00:04:23.776: D/MailService(522): recipient = rnyn@rediffmail.com, 
08-15 00:04:23.776: D/MailService(522): replyTo = "24adithya@gmail.com" <24adithya@gmail.com>, 
08-15 00:04:23.896: I/System.out(522): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
08-15 00:04:23.986: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true
08-15 00:04:24.001: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true
08-15 00:04:24.006: I/System.out(522): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
08-15 00:04:25.226: I/System.out(522): 220 mx.google.com ESMTP fu4sm8557775igc.4
08-15 00:04:25.226: I/System.out(522): DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
08-15 00:04:25.246: I/System.out(522): EHLO localhost
08-15 00:04:25.566: I/System.out(522): 250-mx.google.com at your service, [117.219.113.96]
08-15 00:04:25.566: I/System.out(522): 250-SIZE 35882577
08-15 00:04:25.566: I/System.out(522): 250-8BITMIME
08-15 00:04:25.566: I/System.out(522): 250-AUTH LOGIN PLAIN XOAUTH
08-15 00:04:25.566: I/System.out(522): 250 ENHANCEDSTATUSCODES
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "SIZE", arg "35882577"
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "8BITMIME", arg ""
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
08-15 00:04:25.576: I/System.out(522): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
08-15 00:04:25.576: I/System.out(522): DEBUG SMTP: Attempt to authenticate
08-15 00:04:25.576: I/System.out(522): AUTH LOGIN
08-15 00:04:26.085: I/System.out(522): 334 VXNlcm5hbWU6
08-15 00:04:26.085: I/System.out(522): MjRhZGl0aHlhQGdtYWlsLmNvbQ==
08-15 00:04:26.408: I/System.out(522): 334 UGFzc3dvcmQ6
08-15 00:04:26.408: I/System.out(522): I2EsZmF3a2VzKzE=
08-15 00:04:27.365: I/System.out(522): 235 2.7.0 Accepted
08-15 00:04:27.386: I/System.out(522): DEBUG SMTP: use8bit false
08-15 00:04:27.488: I/System.out(522): MAIL FROM:<24adithya@gmail.com>
08-15 00:04:27.991: I/System.out(522): 250 2.1.0 OK fu4sm8557775igc.4
08-15 00:04:27.996: I/System.out(522): RCPT TO:<24adithya@gmail.com>
08-15 00:04:28.326: I/System.out(522): 250 2.1.5 OK fu4sm8557775igc.4
08-15 00:04:28.326: I/System.out(522): RCPT TO:<rnyn@rediffmail.com>
08-15 00:04:28.705: I/System.out(522): 250 2.1.5 OK fu4sm8557775igc.4
08-15 00:04:28.705: I/System.out(522): DEBUG SMTP: Verified Addresses
08-15 00:04:28.705: I/System.out(522): DEBUG SMTP:   24adithya@gmail.com
08-15 00:04:28.736: I/System.out(522): DEBUG SMTP:   rnyn@rediffmail.com
08-15 00:04:28.736: I/System.out(522): DATA
08-15 00:04:29.586: I/System.out(522): 354  Go ahead fu4sm8557775igc.4
08-15 00:04:29.656: I/System.out(522): Date: Wed, 15 Aug 2012 00:04:23 +0530 (GMT+05:30)
08-15 00:04:29.656: I/System.out(522): From: "24adithya@gmail.com" <24adithya@gmail.com>
08-15 00:04:29.656: I/System.out(522): Reply-To: "24adithya@gmail.com" <24adithya@gmail.com>
08-15 00:04:29.656: I/System.out(522): To: 24adithya@gmail.com, rnyn@rediffmail.com
08-15 00:04:29.656: I/System.out(522): Message-ID: <1079391960.1.1344969263899.JavaMail.javamailuser@localhost>
08-15 00:04:29.656: I/System.out(522): Subject: Test
08-15 00:04:29.656: I/System.out(522): MIME-Version: 1.0
08-15 00:04:29.656: I/System.out(522): Content-Type: multipart/related; 
08-15 00:04:29.656: I/System.out(522):  boundary="----=_Part_0_1079137608.1344969263768"
08-15 00:04:29.656: I/System.out(522): 
08-15 00:04:29.656: I/System.out(522): ------=_Part_0_1079137608.1344969263768
08-15 00:04:29.656: I/System.out(522): Content-Type: text/plain; charset=us-ascii
08-15 00:04:29.656: I/System.out(522): Content-Transfer-Encoding: 7bit
08-15 00:04:29.656: I/System.out(522): 
08-15 00:04:29.656: I/System.out(522): Hi This is 5554 from Mail Service
08-15 00:04:29.656: I/System.out(522): ------=_Part_0_1079137608.1344969263768--
08-15 00:04:29.666: I/System.out(522): .
08-15 00:04:31.030: I/System.out(522): 250 2.0.0 OK 1344969267 fu4sm8557775igc.4
08-15 00:04:31.035: I/System.out(522): QUIT
08-15 00:04:31.055: D/EmailService(522): true
08-15 00:04:31.075: I/System.out(522): DEBUG: setDebug: JavaMail version 1.4.1
08-15 00:04:31.095: D/GmailSenderService(522): from = 24adithya@gmail.com, 
08-15 00:04:31.105: D/GmailSenderService(522): recipient = 24adithya@gmail.com, 
08-15 00:04:31.115: D/GmailSenderService(522): replyTo = 24adithya@gmail.com, 
08-15 00:04:31.145: I/System.out(522): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
08-15 00:04:31.145: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true
08-15 00:04:31.156: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true
08-15 00:04:31.156: I/System.out(522): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
08-15 00:04:32.478: I/System.out(522): 220 mx.google.com ESMTP bp8sm22904456igb.12
08-15 00:04:32.498: I/System.out(522): DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
08-15 00:04:32.498: I/System.out(522): EHLO localhost
08-15 00:04:32.905: I/System.out(522): 250-mx.google.com at your service, [117.219.113.96]
08-15 00:04:32.905: I/System.out(522): 250-SIZE 35882577
08-15 00:04:32.905: I/System.out(522): 250-8BITMIME
08-15 00:04:32.925: I/System.out(522): 250-AUTH LOGIN PLAIN XOAUTH
08-15 00:04:32.925: I/System.out(522): 250 ENHANCEDSTATUSCODES
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "SIZE", arg "35882577"
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "8BITMIME", arg ""
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Attempt to authenticate
08-15 00:04:32.960: I/System.out(522): AUTH LOGIN
08-15 00:04:33.527: I/System.out(522): 334 VXNlcm5hbWU6
08-15 00:04:33.527: I/System.out(522): MjRhZGl0aHlhQGdtYWlsLmNvbQ==
08-15 00:04:33.915: I/System.out(522): 334 UGFzc3dvcmQ6
08-15 00:04:33.915: I/System.out(522): I2EsZmF3a2VzKzE=
08-15 00:04:34.785: I/System.out(522): 235 2.7.0 Accepted
08-15 00:04:34.807: I/System.out(522): DEBUG SMTP: use8bit false
08-15 00:04:34.815: I/System.out(522): MAIL FROM:<24adithya@gmail.com>
08-15 00:04:35.239: I/System.out(522): 250 2.1.0 OK bp8sm22904456igb.12
08-15 00:04:35.245: I/System.out(522): RCPT TO:<24adithya@gmail.com>
08-15 00:04:35.635: I/System.out(522): 250 2.1.5 OK bp8sm22904456igb.12
08-15 00:04:35.635: I/System.out(522): DEBUG SMTP: Verified Addresses
08-15 00:04:35.646: I/System.out(522): DEBUG SMTP:   24adithya@gmail.com
08-15 00:04:35.666: I/System.out(522): DATA
08-15 00:04:36.539: I/System.out(522): 354  Go ahead bp8sm22904456igb.12
08-15 00:04:36.605: I/System.out(522): Sender: 24adithya@gmail.com
08-15 00:04:36.605: I/System.out(522): To: 24adithya@gmail.com
08-15 00:04:36.605: I/System.out(522): Message-ID: <1079897912.2.1344969271138.JavaMail.javamailuser@localhost>
08-15 00:04:36.605: I/System.out(522): Subject: Test Subject
08-15 00:04:36.605: I/System.out(522): MIME-Version: 1.0
08-15 00:04:36.605: I/System.out(522): Content-Type: text/plain; charset=us-ascii
08-15 00:04:36.605: I/System.out(522): Content-Transfer-Encoding: 7bit
08-15 00:04:36.605: I/System.out(522): 
08-15 00:04:36.605: I/System.out(522): Hi, This is 5554 from Gmail Sender
08-15 00:04:36.605: I/System.out(522): .
08-15 00:04:37.896: I/System.out(522): 250 2.0.0 OK 1344969274 bp8sm22904456igb.12
08-15 00:04:37.896: I/System.out(522): QUIT

3 个答案:

答案 0 :(得分:4)

你得到的原因

android.content.ActivityNotFoundException: No Activity found to handle Intent { 
    act=android.intent.action.SENDTO flg=0x10000004 (has extras) }

是因为您从未在Intent中设置数据。 Android会尝试查找可以处理“SENDTO”操作和您提供的数据的活动。但是您没有提供任何数据,因此无法找到合适的活动。你有这个代码:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
//  emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[] { "24adithya@gmail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Email from Intent");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
            "Hi, This is 5554 from intent");
//  getApplicationContext().startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Log.d(LOG_TAG, "context = " + getApplicationContext());
startActivity(emailIntent);

但您需要致电emailIntent.setdataAndType()emailIntent.setData()1 and emailIntent.setType()`。

答案 1 :(得分:0)

在Log中查看这一行:

  

android.util.AndroidRuntimeException:从中调用startActivity()   在Activity上下文之外需要FLAG_ACTIVITY_NEW_TASK   旗。这真的是你想要的吗?

明确表示您需要FLAG_ACTIVITY_NEW_TASK才能从服务启动活动。所以添加

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);

从服务

开始活动

答案 2 :(得分:0)

使用FLAG_ACTIVITY_NEW_TASK

Intent myIntent = new Intent(getBaseContext(), myActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_FROM_BACKGROUND);
getApplication().startActivity(myIntent);
相关问题