说明:
1.BlackBerry_App_Descriptor.xml设置程序为Auto-run on startup,Do not display the application icon on the BlackBerry home screen
2.手机开机后自动运行 BackgroundApplication
3.主程序BackgroundApplication的main中,执行BackgroundThread.waitForSingleton().start();启动后台线程
4.BackgroundThread也是extends Application,里面有个Thread在后面运行,这里是死循环,该或者监听push数据或者其他系统事件--地理位置变化/手机radio网络连接等等。这里是每隔5秒钟在手机日志里面写点东西。
5.注意主程序和线程程序都是 extends Application
6. 要弹出窗口,必须调用 invokeLater( new Runnable() {
提示:
核心是要extends Application
但是我并不清楚是否主程序和线程程序都需要extends Application
细节还需要揣摩和试验,同志尚需努力,呵呵。周末愉快!
/******************************************************************************************/
主程序:
import net.rim.device.api.system.Application;
class BackgroundApplication extends Application {
public static void main(String[] args) {
BackgroundThread.loggerInit();
BackgroundThread.log("main() loggerInit");
BackgroundThread.waitForSingleton().start();
}
public BackgroundApplication() {
}
}
/******************************************************************************************/
后台运行程序:
class BackgroundThread extends Application {
public static String appName = "BackgroundThread";
public static long GUID = 0xcf891935c91e1987L;
private ListenerThread myThread;
public BackgroundThread() {
myThread = new ListenerThread();
}
/******************************************************************************************
* BackGround waitForSingleton() - returns an instance of a listening thread
******************************************************************************************/
public static BackgroundThread waitForSingleton(){
log("waitForSingleton");
//make sure this is a singleton instance
RuntimeStore store = RuntimeStore.getRuntimeStore();
Object o = store.get(GUID);
if (o == null){
log("inited");
store.put(GUID, new BackgroundThread());
return (BackgroundThread)store.get(GUID);
} else {
return (BackgroundThread)o;
}
}
/******************************************************************************************
* start() - starts the custom listen thread
******************************************************************************************/
public void start(){
invokeLater(new Runnable() {
public void run() {
myThread.start();
}
});
this.enterEventDispatcher();
}
/******************************************************************************************
* customer listening thread that is an extention of thread()
******************************************************************************************/
class ListenerThread extends Thread {
public void run() {
log("ListenerThread running");
for(int i=0;true;i++) {
try {
sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log("ListenerThread sleep 7 seconds");
log("ListenerThread sleep 1a");
invokeLater( new Runnable() {
public void run()
{
//InfoPopupScreen _screen = new InfoPopupScreen();
Dialog _screen = new Dialog(Dialog.D_OK_CANCEL, "关闭我", Dialog.D_OK, Bitmap.getPredefinedBitmap(Bitmap.QUESTION), Manager.VERTICAL_SCROLL);
Ui.getUiEngine().pushGlobalScreen(_screen, 1, UiEngine.GLOBAL_MODAL); //GLOBAL_MODAL would block
}
} );
}
}
}
/******************************************************************************************/
public static void loggerInit() {
EventLogger.register(GUID, appName, EventLogger.VIEWER_STRING);
log("LoggerInited");
}
public static void log(String value) {
EventLogger
.logEvent(GUID, value.getBytes(), EventLogger.ALWAYS_LOG);
}
}
/******************************************************************************************/