我正在尝试从我从备用入口点开始的后台线程中弹出一个全局对话框。
public static void main(String[] args) {
MyApp theApp = new MyApp();
if (args != null && args.length > 0 && args[0].equals("test")) {
new Thread(new Runnable() {
public void run() {
try {
synchronized (UiApplication.getEventLock()) {
UiEngine ui = Ui.getUiEngine();
Screen screen = new Dialog(Dialog.D_OK, "Test", Dialog.OK,
Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
Manager.VERTICAL_SCROLL);
ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_MODAL);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}).start();
} else {
theApp.enterEventDispatcher();
}
}
我尝试了很多变化以使其有效,但它仍然没有显示出来。我试过了
同步Application.getEventLock()
,我也试过了
UiApplication.getUiApplication().invokeLater
,
UiApplication.getUiApplication().invokeAndWait
。我甚至在调用synchronizing the eventlock
之前先尝试invokeLater
(我认为这是多余的,但我仍然尝试过......)。我不确定我做错了什么。
答案 0 :(得分:2)
好的,我正在给你一个示例演示......
首先编辑 BlackBerry_App_Descriptor.xml 点击Application Tab
在ApplicationArgument中写入alternate
并在启动时选中自动运行
点击Alternate Entry Points
点击添加并写入标题BackgroundApp
创建一个类,它将像这样扩展Application类而不是UiApplication类
import net.rim.device.api.system.Alert; import net.rim.device.api.system.Application; import net.rim.device.api.system.Bitmap; import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.Screen; import net.rim.device.api.ui.Ui; import net.rim.device.api.ui.UiEngine; import net.rim.device.api.ui.component.Dialog; public class BackGroundApp extends Application { // this class is used for the background processing ..... public void startBackgroundThread() { new Thread(){ public void run() { while (true) { try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (getEventLock()) { //with this UiEngine pushGlobal dialogs //whenever with the app in background UiEngine ui = Ui.getUiEngine(); Screen screen = new Dialog(Dialog.D_OK, "You have updates!", Dialog.OK, Bitmap .getPredefinedBitmap(Bitmap.EXCLAMATION), Manager.VERTICAL_SCROLL); ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE); } } } }.start(); } }
创建一个像这样扩展UiApplication类的类
public class GuiTest extends UiApplication { static Timer t; public static void main(String[] args) { if(args.length>0&&"alternate".equals(args[0])){ BackGroundApp app = new BackGroundApp(); app.startBackgroundThread(); app.enterEventDispatcher(); } else{ GuiTest test = new GuiTest(); test.enterEventDispatcher(); } } public GuiTest(){ Myscreen screeMyscreen = new Myscreen(); pushScreen(screeMyscreen); } }
现在制作一个MyScreen类并在其中添加所有Ui ....并按下屏幕
public class Myscreen extends MainScreen { public Myscreen(){ CreateGui(); } public void CreateGui(){ // Your Ui goes here ....... } }
运行您将在一分钟后看到的样本,无论您是在应用程序中还是在应用程序外,都会在屏幕上显示一个对话框。谢谢,这可能是有帮助的。