如何开始活动

时间:2011-07-07 06:48:18

标签: java android javamail

我是Android开发的新手。我正在使用javamail api制作一个小型电子邮件客户端。我无法找到该活动将如何开始的解决方案。下面是我想要在运行应用程序时启动的电子邮件管理器的代码文件。

我只是需要一种方法来启动它,我真的很困惑android活动,并有线索如何实现它们 在此先感谢:)

package com.mailtest.android;

import android.app.Activity;
import android.os.Bundle;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.view.View;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;

public class EmailManager extends Activity {


    **/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);** 
    } 
    private String stmpHost = "smtp.gmail.com";
        private String mailServer = "imap.gmail.com";
        private EmailAccount account;
        private Session smtpSession; 
        private Session imapSession; 
        private Folder inbox;
        private Store store;



        public EmailManager(String username, String password, String urlServer, String stmpHost, String mailServer) {
            account = new EmailAccount(username, password, urlServer);
            this.stmpHost = stmpHost;
            this.mailServer = mailServer;
            initProtocol();
        }
        private void initProtocol() {
            EmailAuthenticator authenticator = new EmailAuthenticator(account);

            Properties props1 = new Properties();  
            props1.setProperty("mail.transport.protocol", "smtps");  
            props1.setProperty("mail.host", stmpHost);  
            props1.put("mail.smtp.auth", "true");  
            props1.put("mail.smtp.port", "465");  
            props1.put("mail.smtp.socketFactory.port", "465");  
            props1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
            props1.put("mail.smtp.socketFactory.fallback", "false");  
            props1.setProperty("mail.smtp.quitwait", "false");  
            smtpSession = Session.getDefaultInstance(props1, authenticator); 

            Properties props2 = new Properties();
            props2.setProperty("mail.store.protocol", "imaps");
            props2.setProperty("mail.imaps.host", mailServer);
            props2.setProperty("mail.imaps.port", "993");
            props2.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props2.setProperty("mail.imaps.socketFactory.fallback", "false");
            imapSession = Session.getInstance(props2);
        }   
        public Message[] getMails() throws MessagingException {
            store = imapSession.getStore("imaps");
            store.connect(mailServer, account.username, account.password);
            inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message[] result = inbox.getMessages();

            return result;
        }
        public void close() {
            //Close connection 
            try {
                inbox.close(false);
                store.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
        public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {  
            MimeMessage message = new MimeMessage(smtpSession);
            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));  
            Transport.send(message);  


            }
        } 

这是我的主要.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

</LinearLayout>  

2 个答案:

答案 0 :(得分:0)

首先,您可能希望将电子邮件管理器放在自己的类中,因为不必将其作为Activity扩展。但是,要回答您的问题,您可以查看http://developer.android.com/reference/android/app/Activity.html

这只是一个例子,假设应该启动其他活动的活动称为MainActivity。 然后把它放在MainActivity中:

  startActivity(new Intent(MainActivity.this, EmailManager.class)); 

请记住将电子邮件管理器作为AndroidManifest.xml中的活动

<activity android:name=".EmailManager"/>

可能的重复答案,可在此处找到更多答案:How to start activity in another application?

答案 1 :(得分:0)

回去阅读:http://developer.android.com/reference/android/app/Activity.html

不要扩展您的EmailManager的活动,您不需要,因为EmailManager不与用户界面交互,也不依赖于任何上下文。

实施一个单独的活动,您可以从活动方法(OnCreate,OnResume等)调用相关的EmailManager方法 OnCreate是您的活动开始的方式

当您更好地了解Android组件的生命周期时,您最好将活动中的内容移动到服务中:http://developer.android.com/reference/android/app/Service.html 这也是因为您没有来自您的EmailManager的ui交互,而且您的EmailManager所做的工作类型更适合于服务。