Archiv für die Kategorie ‘android’

Android: IMAP Inbox Part 3 – Display Message Text

Samstag, 16. Februar 2008

message.PNG

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.



Android: IMAP Inbox Part 2 – Customizing the ListView

Sonntag, 10. Februar 2008

In a recent posting I showed how to read messages from an imap server and show them in a ListView. So far the ListView only showed the subject line from a String Array. There is no easy way back to the message object, so we need to change this.

I’ve improved this a little bit now after reading this article on how to customize an ArrayAdapter. The getMessages() method now returns the Message [], which is wrapped in the ArrayAdapter. To show the subject line in the list we simply override the getView method:

8<———————————————->8

package de.eppleton.mail;

import javax.mail.Message;
import javax.mail.MessagingException;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ShowInbox extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
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;
}

});
}
}

8<———————————————->8

And the ImapClient looks like this:

8<———————————————->8

package de.eppleton.mail.imap;

import java.net.ConnectException;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class ImapClient {
static {
Security
.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
static Session session;
static Store store;

public static Message[] getMail() throws MessagingException {
connect();
Folder folder = store.getFolder(“INBOX”);
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
return messages;
}

private static void connect() throws MessagingException {
if (null != session)
return;
java.util.Properties props = new java.util.Properties();
props.setProperty(“mail.imap.socketFactory.class”,
“javax.net.ssl.SSLSocketFactory”);
props.setProperty(“mail.imap.socketFactory.fallback”, “false”);
props.setProperty(“mail.imap.socketFactory.port”, “993″);
session = Session.getDefaultInstance(props);
store = session.getStore(“imap”);
store.connect(“mailserver”,”userid”,”password”);
}
}

8<———————————————->8

Well, the result looks almost the same, except multiline subbjects are supported:

inbox2.PNG

Android: IMAP Inbox Part 1

Sonntag, 10. Februar 2008

inbox.PNG

Here’s how to view your email inbox with Android. You need to add additional libraries to the project to get this running. Unfortunately I couldn’t get the java mail and activation libraries on the phone with the NetBeans Undroid module. If you know how to do that, let me know. I added the two mail and ativation libraries to the project and the build went fine, but when I launch the application I get a “NoClassDefFoundError” for javax/mail/Session. So I’m using the Eclipse plugin for this, until I figure out how to do it in NetBeans:

1. Create a new Android Project (new Project -> Android -> Android Project), name it “inbox”, Package “de.eppleton.inbox”, Activity Name “ShowInbox”, Application Name “inbox”

2. Create a new Class ImapClient and replace the boldface entries with your credentials:
8<———————————->8
package de.eppleton.mail;

import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class ImapClient {
static {
Security.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}

public static String [] getMail() throws MessagingException {
java.util.Properties props = new java.util.Properties();
props.setProperty(“mail.imap.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
props.setProperty(“mail.imap.socketFactory.fallback”, “false”);
props.setProperty(“mail.imap.socketFactory.port”, “993″);

Session session = Session.getDefaultInstance(props);

Store store = session.getStore(“imap”);
store.connect(“mail.mailserver.com”, “you”, “yourpassword”);

Folder folder = store.getFolder(“INBOX”);
folder.open(Folder.READ_ONLY);

Message[] message = folder.getMessages();
String [] titles = new String[message.length];
for (int i = 0, n = message.length; i < n; i++) {
titles[i] = message[i].getSubject();
}
// Close connection
folder.close(false);
store.close();
return titles;
}
}

8<———————————->8

3. Add the missing libraries ( Referenced Libraries -> build path -> configure build path -> Add External JARs… ). Y ou’ll need activation-1.1.jar and mail-1.4.jar from sun

4. Change the ShowInbox class to a ListActivity and add this code:
8<———————————->8
package de.eppleton.mail;

import javax.mail.MessagingException;

import de.eppleton.mail.ImapClient;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ShowInbox extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] messages = new String[] { “empty” };
try {
messages = ImapClient.getMail();
} catch (MessagingException e1) {
e1.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, messages));
}
}
8<———————————->8

5. Launch the application, and your done.

Update: here’s the next step, using a Message[] in the ListView

If you want to send mail check this out:

Android – Send email via GMail (actually via SMTP) « Show me the code!

NetBeans news in JavaTools Community Newsletter

Sonntag, 10. Februar 2008

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 JavaTools Community Newsletter has lots of NetBeans news. In this issue:

- NetBeans-Blogger (Embed blogging features into NetBeans)

- Mogwai ER-Designer NG NetBeans Plugin available

- Android plugin for NetBeans

- Solaris Express developer Edition (SXDE), The only OS a developer ever needs :-)

- NBPython (Python support for Netbeans)

… and many other tips and tricks, enjoy!

ARM Google phone & Developer contest deadline extended

Freitag, 08. Februar 2008

I started playing with the Undroid plugin recently. It works well and it’s fun to play around with the emulator, although I’d rather play with a real Google phone like the one that will be presented on monday. So you better speed up when you want to develop the killer app to finance your sabbatical. On the other hand, Google has extended the deadline for the developer contest.

icon.png

By the way, if you -like me- don’t like the steampunk default application gear, here’s how to change the default icon of your application:

Create a folder “drawable” in the resources dir of your android project and move your Icon there. I used this as an example:

mizu_icon.png

Now go to the files tab and add a reference to the application element (android:icon) in your AndroidManifest.xml :

<?xml version=”1.0″ encoding=”UTF-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”org.me.mizuandroid”>
<application android:icon=”@drawable/mizu_icon”>
<activity class=”.MainActivity” android:label=”Mizu”>
<intent-filter>
<action android:value=”android.intent.action.MAIN”/>
<category android:value=”android.intent.category.LAUNCHER”/>
</intent-filter>
</activity>
</application>
</manifest>

You can also change the application name here (android:label) When you run your application the default icon is replaced:

mizu.PNG

That’s it, happy coding!