Skip Navigation

Implement a 
BlackBerry Dynamics
 event listener

The 
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.
  1. Include the following code at the top of your 
    AppDelegate.cs
     file to implement a skeleton 
    GDiOSDelegate
     interface, set the app’s window as a 
    GDWindow
     (this example relies on a 
    _started
     boolean variable), use 
    GDAppEvent
     to process events, and move the app launch code from 
    FinishedLaunching
     to the 
    HandleEvent
     handler 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; } } ... } }
  2. Verify that your app can handle problems such as authorization errors or actions like remote wipe, lockout, or blocking events. Implement these events in the 
    OnNotAuthorized
     function.
    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; } }
  3. On authorization, start the app. Initialize and start the app UI with the 
    OnAuthorized
     function. The 
    GDAppResultCode.ErrorNone
     event is returned by the 
    BlackBerry Dynamics Runtime
     when a container is opened and no error occurs (you can test for it as an alternative to the boolean 
    _started
     shown 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; } } }