简体   繁体   中英

Error while starting activity from service in android

I have a receiver which listens to incoming calls. From this receiver i am starting a service which will send emails (using android Intent) but i am getting an exception (plz refer logs from Log Cat).

java.lang.RuntimeException: Unable to start service com.pack.android.service.EmailService@40519818 with Intent { cmp=com.pack.android.email.service/com.pack.android.service.EmailService }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I have set 'FLAG_ACTIVITY_NEW_TASK' and 'FLAG_FROM_BACKGROUND'. This isn't a user activity. I am trying to start an existing activity (send email) using intent INTENT.SENDTO/SEND.

Strange thing was the error : Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO flg=0x10000004 (has extras) }

What am i missing here ?

Another problem i faced while sending email using Java Mail API was the mail was being sent properly that's what it looks like but somehow the mail never seems to appear in the mailbox. I am referring the below link to send emails without intents : Send email without intent

It would be gr8 if someone could give some insights into this.

Code is as follows: Call Receiver :-

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);
        }
    }
}

Email Service :-

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-------");
    }
}

Mail Service :

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 Sender:

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

The reason that you get

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

is because you never set the data in the Intent. Android tries to find an activity that can deal with the action "SENDTO" and the data that you provide. But you didn't provide any data so it can't find a suitable activity. You have this code:

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);

but you need to call emailIntent.setdataAndType() or emailIntent.setData()1 and emailIntent.setType()`.

Look at this line in Log :

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

which clearly saying you requires FLAG_ACTIVITY_NEW_TASK to start Activity from Service. so add

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

for starting activity from service

Use 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM