Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Create an auto-start MIDlet using the PushRegistry

by Retired ‎02-16-2010 10:44 AM - edited ‎09-16-2010 02:44 PM (4,313 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphone
  • Mobile Information Device Profile (MIDP) 2.0

Procedure

 

A MIDlet can be set to register with the PushRegistry as soon as it is installed and each time the BlackBerry smartphone starts up. Once registered with the PushRegistry, the MIDlet will be launched whenever data arrives on the registered connection.

 

To specify a MIDlet to auto-register with the PushRegistry, an additional line is added to the JAD file. Once this line has been added, the JAD file is added to the project in the BlackBerry® Java® Development Environment (JDE). After the modified JAD file has been added to the project, the COD file must be recompiled for auto-registration with the PushRegistry to be applied to the MIDlet.

 

The following sample illustrates the use of a MIDlet that auto-registers with the PushRegistry:

 

JAD File

 

 

Manifest-Version: 1.0
RIM-COD-Module-Name: PushRegistryApp
RIM-COD-Module-Dependencies: net_rim_cldc
MIDlet-Jar-Size: 5393
MIDlet-1: MIDlet PushRegistryApp,,test.javax.microedition.midlet.PushRegistryApp
RIM-COD-Creation-Time: 1108578018
MIDlet-Jar-URL: PushRegistryApp.jar
RIM-COD-URL: PushRegistryMidlet01.cod
RIM-COD-SHA1: 09 f7 57 1a cc 30 ba 25 19 79 ed 44 0d 07 04 c7 db 50 79 74
RIM-COD-Size: 5060
MicroEdition-Configuration: CLDC-1.1
MIDlet-Push-1: sms://:5678, test.javax.microedition.midlet.PushRegistryApp, *
MIDlet-Version: 0.9
MIDlet-Name: PushRegistryMidlet01
MIDlet-Vendor: Research In Motion Ltd.
MicroEdition-Profile: MIDP-2.0
RIM-MIDlet-Flags-1: 0

 

MIDlet Java Source Code

 

 

/*
* PushRegistryApp.java
*
*/

package test.javax.microedition.midlet;

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.util.*;
import java.io.*;
import javax.wireless.messaging.*;

public final class PushRegistryApp extends MIDlet
{
//The data we expect to receive.
private static final String TEST_DATA = "This is just a test";

private Display theDisplay;
private Form theForm;
private StringItem messages;
private MessageConnection mc;

public PushRegistryApp()
{
}

//Signals the MIDlet that it has entered the Active state.
public void startApp()
{
//Get the display object for this MIDlet.
theDisplay = Display.getDisplay(this);

//Instantiate the form.
theForm = new Form("PushRegistryApp");

//Istantiate the StringItem.
messages = new StringItem("", "starting...");

//Add the StringItem to the form.
theForm.append(messages);

//Open the SMS connection on port 5678.
try
{
mc = (MessageConnection)Connector.open("sms://:5678");
}
catch(IOException ioe){
System.out.println("Failed to open connection: "
+ ioe.toString());
}

//Create and start a new WorkerThread.
Thread t = new WorkerThread();
t.start();

theDisplay.setCurrent(theForm);
}

private class WorkerThread extends Thread
{
public void run() {
//Open the connection.
//Incoming data should be waiting.
String connection = getAppProperty("MIDlet-Push-1");

//The property is '<uri>, <midlet>, <filter>'
//so get just the uri.
connection = connection.substring(0, connection.indexOf(','));

//Get all connections registered with the PushRegistry
//for the current MIDlet suite.
String[] connectionsWithData =
PushRegistry.listConnections(true);

//Test all connections to locate the one matching "sms://5678".
for ( int i = connectionsWithData.length - 1; i >= 0; --i)
{
if ( connectionsWithData[i].equals(connection) )
{
//Found the matching connection.
//Fetch the incoming data.
fetchData(connection);
}
}

//Shut down to wait for data.
message("Done");
}
}

//Fetches the incoming data.
private void fetchData(String connection)
{
try
{
//Open the connection.
Connection c = Connector.open(connection);

if ( c instanceof MessageConnection )
{
//Open a MessageConnection
MessageConnection mc = (MessageConnection)c;
TextMessage txtMsg = (TextMessage)mc.receive();

message("Received: " + txtMsg.getPayloadText());
}

} catch (IOException e)
{
message(e.toString());
}

}

//Display a String message to the user.
private void message(String msg)
{
//Display the message in the StringItem.
messages.setText(messages.getText() + "\n" + msg);
}

//Signals the MIDlet to stop and enter the Pause state.
public void pauseApp()
{
}

//Signals the MIDlet to terminate and enter the Destroyed state.
//Unconditional when set to true. The MIDlet must cleanup and release
//all resources.
//Otherwise, the MIDlet may throw a MIDletStateChangeException to
//indicate it does not want to be destroyed at this time.
public void destroyApp(boolean unconditional)
{
try
{
//Close the connection.
mc.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}

//Throws an IllegalStateException with the supplied error message.
final class Assert
{
private Assert()
{
}

static public void assertTrue(boolean exp, String failmsg)
{
if ( !exp) throw new IllegalStateException(failmsg);
}
}

 

 

 

 



 

Contributors
Users Online
Currently online: 15 members 2,602 guests
Please welcome our newest community members: