Implement a BlackBerry
Dynamics event listener
BlackBerry
Dynamics
event listenerThe
Application
object manages the app’s global app state. Many of the sample apps that are included with the BlackBerry
Dynamics
Bindings demonstrate the event handling lifecycle. This topic demonstrates one approach to implementing the lifecycle of events.
- Include the following code at the top of yourAppDelegate.csfile to implement a skeletonGDiOSDelegateinterface, set the app’s window as aGDWindow(this example relies on a_startedboolean variable), useGDAppEventto process events, and move the app launch code fromFinishedLaunchingto theHandleEventhandler method.//AppDelegate.cs using System; using UIKit; using Foundation; using System.Diagnostics using GoodDynamics; namespace MyApplication { // The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to application // events from iOS. [Register ("AppDelegate")] public class AppDelegate : GDiOSDelegate { private bool _started; public GDiOS GDLibrary { get; private set; } public NSMutableArray Providers {get; set;} public override UIWindow Window { get; set; } public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) { GDLibrary = GDiOS.GDSharedInstance; GDLibrary.Delegate = this; GDLibrary.Authorize(); Window = UIApplication.SharedApplication.Delegate.GetWindow(); Window.MakeKeyAndVisible (); return true; } public override void HandleEvent (GDAppEvent anEvent) { switch (anEvent.Type) { case GDAppEventType.Authorized: OnAuthorized (anEvent); break; case GDAppEventType.NotAuthorized: OnNotAuthorized (anEvent); break; case GDAppEventType.RemoteSettingsUpdate: //handle app config changes break; case GDAppEventType.ServicesUpdate: Debug.WriteLine ("Received Service Update Event"); OnServiceUpdate (anEvent); break; default: Debug.WriteLine ("Event Not Handled"); break; } } ... } }
- Verify that your app can handle problems such as authorization errors or actions like remote wipe, lockout, or blocking events. Implement these events in theOnNotAuthorizedfunction.private void OnNotAuthorized(GDAppEvent anEvent) { switch (anEvent.Code) { case GDAppResultCode.ErrorActivationFailed: case GDAppResultCode.ErrorProvisioningFailed: case GDAppResultCode.ErrorPushConnectionTimeout: case GDAppResultCode.ErrorSecurityError: case GDAppResultCode.ErrorAppDenied: case GDAppResultCode.ErrorAppVersionNotEntitled: case GDAppResultCode.ErrorBlocked: case GDAppResultCode.ErrorWiped: case GDAppResultCode.ErrorRemoteLockout: case GDAppResultCode.ErrorPasswordChangeRequired: Console.WriteLine ("OnNotAuthorized {0}", anEvent.Message); break; case GDAppResultCode.ErrorIdleLockout: break; default: Debug.Assert (false, "Unhandled not authorized event"); break; } }
- On authorization, start the app. Initialize and start the app UI with theOnAuthorizedfunction. TheGDAppResultCode.ErrorNoneevent is returned by theBlackBerry Dynamics Runtimewhen a container is opened and no error occurs (you can test for it as an alternative to the boolean_startedshown here).private void OnAuthorized(GDAppEvent anEvent) { switch (anEvent.Code) { case GDAppResultCode.ErrorNone: if (!_started) { _started = true; //Launch Application UI Here } break; default: Debug.Assert (false, "Authorized startup with an error"); break; } } }