Thursday, October 4, 2007

j2me push registery

Static Registration
To statically declare a push connection, a connection string must be specified in the Java Application Descriptor (JAD) file or the JAR manifest (depending on how the platform supports the application descriptor). Connections declared in the application descriptor follow the format:
MIDlet-Push-: , , 
MIDlet-Push-1: datagram://:444, com.gearworks.PushExample, 12.23.101.02
MIDlet-Push-1: sms://:50001, com.gearworks.PushExample,*

Dynamic Registration
Dynamic registration of push connections requires the same elements as static registration but they are specified as parameters in a call to PushRegistry.registerConnection(). The following example dynamically registers a datagram connection and is identical to the static example shown previously.PushRegistry.registerConnection("datagram://:444", "com.gearworks.PushExample", "12.23.101.02").

Dynamic registration, on the other hand, has the advantage of installing without failure. However, the user must run the application at least once to invoke the dynamic registration. Because dynamic registration is done at runtime there is more flexibility in how connections are registered. For example an application can scan for an available connection to better ensure push registration success. The following code shows how this might be accomplished, by attempting up to four different connection strings:


String base = "55";
String name = "PushMIDlet";
for(int ccnt=5; ccnt < 9; ccnt++) {
try {
String temp = base +
Integer.toString(ccnt);
PushRegistry.registerConnection(
"datagram://:"+temp, name, "*");
break;
} catch (IOException x) {
continue;
}

Condition

Dynamic Registration Response

Connection string syntax is not valid

An IllegalArgumentException is thrown

The device does not support the specified connection type (e.g. socket) or has not made the connection type available to push

A ConnectionNotFoundException is thrown

The connection is already registered by another MIDlet suite or there are insufficient resources to handle the registration request

An IOException is thrown

The MIDlet name specified in the push connection string is not declared in the application descriptor of the MIDlet suite

A ClassNotFoundException is thrown

The MIDlet suite does not have permission to register or use the specified connection

A SecurityException is thrown

The port specified by the connection string is reserved by the device

Undefined by the MIDP specification, but an IOException is most likely.

A list of connections can be obtained by calling 
PushRegistry.listConnections(boolean)

This code handles inbound datagram connections in response to a pushed datagram connection.
protected void startApp(){
if (!init) {
init = true;
String[] conns =
PushRegistry.listConnections(false);
System.out.println("Found " + conns.length +
" connections.");
for(int ccnt=0; ccnt < conns.length; ccnt++){
DatagramHandler handler =
new DatagramHandler(conns [ccnt]);
connectionHandlers.addElement(handler);
handler.start();
}
}
}

class DatagramHandler extends Thread
{
private DatagramConnection dgc;

public DatagramHandler(String c){
try {
dgc = (DatagramConnection)Connector.open(c);
} catch (IOException x){
x.printStackTrace();
}
}

public void run(){
while (true){
try{
Datagram datagram = dgc.newDatagram(20);
dgc.receive(datagram);
byte[] data = datagram.getData();
String s = new String(data);
System.out.println(s);
} catch (InterruptedIOException ignore) {
System.out.println("Done listening");
break;
} catch (IOException x) {
x.printStackTrace();
}
}
}

or

public class MySamplePushRegistry extends MIDlet
implements CommandListener, Runnable, MessageListener {

//....

public void startApp() {
smsPort = getAppProperty("SMS-Port");
String smsConnection = "sms://:" + smsPort;
if (smsconn == null) {
try {
smsconn = (MessageConnection)
Connector.open(smsConnection);


smsconn.setMessageListener(this);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
display.setCurrent(resumeScreen);
}
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void run() {
try {
msg = smsconn.receive();
if (msg != null) {
if (msg instanceof TextMessage) {


content.setString(((TextMessage)msg).getPayloadText());
}
display.setCurrent(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//other methods to follow
}

Setting Alarms
Hidden away in the API of the PushRegistry is a simple little feature called an Alarm. An Alarm can be registered for a MIDlet suite much like a connection. The difference, however, is that the MIDlet associated with an Alarm is started at a certain point in time, rather than in response to network activity. Each MIDlet suite can register, at most, one Alarm at a time. Alarms are registered by making a call to PushRegistry.registerAlarm(). The following code registers an Alarm to start the MIDlet "PushDemo" 24 hours from now.

long day = 1000*60*60*24; long t = new Date().getTime()+day; PushRegistry.registerAlarm( "PushDemo", t);



http://www.devx.com/wireless/Article/20154/1954?pf=true
http://www.javaworld.com/javaworld/jw-04-2006/jw-0417-push.html?page=1
http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/java/p_tips_java_1201.jsp

No comments: