初次做這個程序的時候,是仿照着網上別人的程序做的。因為本人比較菜,是一個新手,以前的基礎知識沒有學好,所以盡管有了別人的代碼但是還是不知道怎么在界面上顯示出它的效果來,廢話不多少,現在就貼出我的參考程序的來源。http://www.cnblogs.com/love2009/archive/2009/02/24/1397201.html 大家可以在看以下內容前,通讀一下。想要了解javamail的機制,我們還需要JAVAMAIL的API,這里也貼出幫助文檔內容《JavaMail API詳解》http://pringles.iteye.com/blog/125196。因為我本人也是在零的基礎上做出這個程序的,參考這兩篇文章才懂得什么意思。
下面進行后續內容的介紹:
我們需要讀取內容,就是需要把手機上的賬號和互聯網上的賬號綁定起來。所以我們需要讀取到,welcome界面時候存入的用戶名,以及密碼。才能執行自己所需要的操作
1.讀取數據內容(用戶名,以及密碼):
sharedPreference // sharedpreference讀取數據,用split()方法,分開字符串。 SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION, MODE_WORLD_READABLE); String content = pre.getString("save", ""); String[] Information = content.split(";"); username = Information[0]; password = Information[1];
2.界面准備工作
每次點擊收信按鈕的時候,大家看到的都是一個列表形式的收信箱,然后點擊一條數據,才會顯示信件的具體內容。所以我們就需要進行一下操作:
效果布局文件有兩個,一個是listMenu.xml和Item.xml
listMenu <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:text="收件箱:" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="311dp" android:layout_weight="1" android:orientation="horizontal" > <View android:layout_width="12dp" android:layout_height="309dp" > </View> <ListView android:id="@+id/my_list" android:layout_width="wrap_content" android:layout_height="313dp" > </ListView> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:src="@drawable/sinalogo" /> </LinearLayout>
Item <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textSize="22px" > </TextView> <TextView android:id="@+id/info" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textSize="13px" > </TextView> </LinearLayout>
界面效果圖如下:
3.現在進行代碼顯示部分(ReceiveList.java)
ReceiveList package mi.email.activity; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Properties; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.MimeMessage; import mi.email.core.ResolveMail; import mi.learn.com.R; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; public class ReceiveList extends Activity { private static final String SAVE_INFORMATION = "save_information"; private ListView listview; private int number; String Title; String Date; String From; String Content; String username; String password; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.listmenu); listview = (ListView) findViewById(R.id.my_list); try { MenuList(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void MenuList() throws MessagingException, IOException { // sharedpreference讀取數據,用split()方法,分開字符串。 SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION, MODE_WORLD_READABLE); String content = pre.getString("save", ""); String[] Information = content.split(";"); username = Information[0]; password = Information[1]; Properties props = new Properties(); Session session = Session.getDefaultInstance(props); // 取得pop3協議的郵件服務器 Store store = session.getStore("pop3"); // 連接pop.sina.com郵件服務器 // store.connect("pop.sina.com", username, password); // 返回文件夾對象 Folder folder = store.getFolder("INBOX"); // 設置僅讀 folder.open(Folder.READ_ONLY); // 獲取信息 Message message[] = folder.getMessages(); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();//定義一個List並且將其實例化 for (int i = 0; i < message.length; i++) {//通過for語句將讀取到的郵件內容一個一個的在list中顯示出來 ResolveMail receivemail = new ResolveMail((MimeMessage) message[i]); Title = receivemail.getSubject();//得到郵件的標題 Date = receivemail.getSentDate();//得到郵件的發送時間 HashMap<String, String> map = new HashMap<String, String>();//定義一個Map.將獲取的內容以鍵值的方式將內容展現 map.put("title", Title);//顯示郵件的標題 map.put("info", Date);//顯示郵件的信息 list.add(map); SimpleAdapter listAdapter = new SimpleAdapter(this, list,R.layout.item, new String[] { "title", "info" }, new int[] { R.id.title, R.id.info }); listview.setAdapter(listAdapter); } folder.close(true);//用好之后記得將floder和store進行關閉 store.close(); // Item長按事件。得到Item的值,然后傳遞給MailDetail的值 listview.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.putExtra("ID", position); intent.setClass(ReceiveList.this, MailDetails.class); startActivity(intent); return true; } }); } }
4.我們長按一個Item的時候需要觸發點擊事件,然后獲取到ID得值,將ID的值傳送到下一個MailDetail.java的界面
setOnItemLongClickListener // Item長按事件。得到Item的值,然后傳遞給MailDetail的值 listview.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.putExtra("ID", position); intent.setClass(ReceiveList.this, MailDetails.class); startActivity(intent); return true; } });
5.MailDetails.java
MailDetails package mi.email.activity; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; import javax.mail.Part; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.MimeMessage; import mi.email.core.ResolveMail; import mi.learn.com.R; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; public class MailDetails extends Activity { private static final String SAVE_INFORMATION = "save_information"; private TextView text1; private TextView text2; private TextView text3; private TextView text4; private ReceiveList ml; public void receive() throws Exception { // sharedpreference讀取數據,用split()方法,分開字符串。 SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,MODE_WORLD_READABLE); String content = pre.getString("save", ""); String[] Information = content.split(";"); String username = Information[0]; String password = Information[1]; Intent intent = getIntent();//得到上一個文件傳入的ID號 Bundle i = intent.getExtras(); int num = i.getInt("ID");//將得到的ID號傳遞給變量num Properties props = new Properties(); Session session = Session.getDefaultInstance(props); // 取得pop3協議的郵件服務器 Store store = session.getStore("pop3"); // 連接pop.qq.com郵件服務器 store.connect("pop.sina.com", username, password); // 返回文件夾對象 Folder folder = store.getFolder("INBOX"); // 設置僅讀 folder.open(Folder.READ_ONLY); // 獲取信息 Message message[] = folder.getMessages(); ResolveMail receivemail = new ResolveMail((MimeMessage) message[num]); text1.setText(receivemail.getSubject());//得到郵件解析后的標題內容並且在控件中顯示出來 text2.setText(receivemail.getFrom());//得到郵件解析后的發送者 text3.setText(receivemail.getSentDate());//得到郵件解析后的發送時間 text4.setText((CharSequence) message[num].getContent().toString());//得到郵件解析有的內容 folder.close(true); store.close(); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main2); text1 = (TextView) findViewById(R.id.text1); text2 = (TextView) findViewById(R.id.text2); text3 = (TextView) findViewById(R.id.text3); text4 = (TextView) findViewById(R.id.text4); try { receive(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
界面顯示圖如下: