In the last blog entry of this series I explored how to connect to an imap server and show the message subjects in alist. Here’s how to read a selected message. As a prerequisite you’ll need to add the java.awt.datatransfer package to your sources as described here.
Then create a new Activity to show the Message. I named mine “MessageDisplay” and registered it in the AndroidManifest.xml:
8<————————————————->8
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”de.eppleton.mail.imap”>
<application android:icon=”@drawable/icon”>
<activity class=”.ShowInbox” android:label=”@string/app_name”>
<intent-filter>
<action android:value=”android.intent.action.MAIN” />
<category android:value=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity class=”.MessageDisplay” android:label=”message”></activity>
</application>
</manifest>
8<————————————————->8
Then implement the class:
8<————————————————->8
package de.eppleton.mail.imap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MessageDisplay extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
TextView resultView = new TextView(this);
Bundle extras = getIntent().getExtras();
String value = “empty”;
if (extras != null) {
value = extras.getString(“message”);
}
resultView.setText(value);
setContentView(resultView);
}
}
8<————————————————->8
It simply displays the message String in a TextView. The trickiest part is to get data from your Main Activity to the subactivity. This is done via the Intent. Data is added in the main Activity to the Intent by calling putExtra(String key, Object value), and retrieved as shown above (in boldface).
Now we can use this view from the main activity (Changed part in boldface):
8<————————————————>8
package de.eppleton.mail.imap;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.mail.Message;
import javax.mail.MessagingException;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ShowInbox extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Message[] messages = new Message[] {};
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = getItem(position);
TextView resultView = null;
if (null == convertView || !(convertView instanceof TextView)) {
resultView = new TextView(super.getContext());
}
try {
resultView.setText(message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
return resultView;
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Message message = (Message) l.getAdapter().getItem(position);
String messageString = readMessage(message);
showMessage(messageString);
}
private void showMessage(String messageString) {
Intent i = new Intent(this, MessageDisplay.class);
i.putExtra(“message”, messageString);
startSubActivity(i, ACTIVITY_CREATE);
}
private String readMessage(Message message) {
String messageString = “”;
String contentType;
try {
contentType = message.getContentType();
if (contentType.startsWith(“text/plain”)
|| contentType.startsWith(“text/html”)) {
Object content = message.getContent();
if (content instanceof String) {
messageString = (String) message.getContent();
} else if (content instanceof InputStream) {
BufferedReader in = new BufferedReader(
new InputStreamReader((InputStream) content));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append(“\n”);
}
messageString = buffer.toString();
} else {
messageString = “Can’t read this!”;
}
} else
messageString = contentType;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return messageString;
}
}
8<————————————————>8
The only interesting thing here is how to receive the event from the list -that is done via overriding onListItemClick-, and how to start the MessageDisplay activity – that is done in showMessage. First the Intent is created, then the message is added to the Bundle via putExtra, and startSubActivity launches MessageDisplay. Actually startActivity should work equally well here, since we don’t implement onActivityResult() yet.
Now you can launch the Application and click the subjects to display the message text.
JavaTools is a Community at dev.java.net where lots of cool projects are hosted, e.g. Hudson and it’s NetBeans plugin. This week our