一个典型的模式是构建一个有两个入口点的应用程序。也就是说,它可以以两种不同的方式启动。一种方法是普通的UiApplication。这是标准的 BlackBerry 应用程序,只需按下主屏幕图标即可启动。
另一种方法是定义一个后台服务,处理推送通知,并在设备启动时由操作系统启动。
您将通过在应用的 BlackBerry_App_Descriptor.xml 文件中添加备用入口点来定义后台/推送入口点。确保选中启动时自动运行和不显示应用程序图标...。除了 UiApplication 的正常入口点之外,您的应用程序描述符 xml 文件应该包含类似这样的内容:
<AlternateEntryPoints>
<AlternateEntryPoint Title="PushService" MainMIDletName=""
ArgumentsForMain="-push" HomeScreenPosition="0"
StartupTier="7" IsSystemModule="true"
IsAutostartup="true" hasTitleResource="false"
TitleResourceBundleKey="" TitleResourceBundleName=""
TitleResourceBundleClassName="" TitleResourceBundleRelativePath="">
<Icons/>
<KeywordResources KeywordResourceBundleName="" KeywordResourceBundleRelativePath="" KeywordResourceBundleClassName="" KeywordResourceBundleKey=""/>
</AlternateEntryPoint>
</AlternateEntryPoints>
然后,你将有一个这样的主程序:
public class MyApp extends UiApplication
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("-push")) {
// this is the push service
PushAgent pa = new PushAgent();
pa.enterEventDispatcher();
} else {
// UiApplication
MyApp app = new MyApp();
app.enterEventDispatcher();
}
}
}
其中PushAgent 是extends Application 的类,而不是UiApplication。
然后,当您的推送代理收到通知并且您决定要显示 UI 时,请使用以下内容:
ApplicationDescriptor ad = ApplicationDescriptor.currentApplicationDescriptor();
// String[] used for command line args, but we don't pass any to the UI app
ApplicationDescriptor ui = new ApplicationDescriptor(ad, new String[] { });
ApplicationManager.getApplicationManager().runApplication(ui);