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.
Alternatively, you can use the GDStateAction interface to implement an event handling mechanism. The GreetingsServer sample app demonstrates the use of this API.
  1. Implement the
    IGDAppEventListener
    and
    IGDStateListener
    interfaces:
    using System.Collections.Generic; using Com.Good.GD; using Android.Util; namespace Example.Droid { public class GDEventListener : Java.Lang.Object, IGDAppEventListener { private static GDEventListener _instance; private bool _started; string tag = "GDEventListener"; public static GDEventListener Instance { get { if (_instance == null) { _instance = new GDEventListener(); } return _instance; } } public void OnGDEvent(GDAppEvent anEvent) { if (anEvent.EventType == GDAppEventType.GDAppEventAuthorized) { OnAuthorized(anEvent); } else if (anEvent.EventType == GDAppEventType.GDAppEventNotAuthorized) { OnNotAuthorized(anEvent); } else if (anEvent.EventType == GDAppEventType.GDAppEventRemoteSettingsUpdate) { //handle app config changes ... } else if (anEvent.EventType == GDAppEventType.GDAppEventServicesUpdate) { //handle event service update ... } else if (anEvent.EventType == GDAppEventType.GDAppEventEntitlementsUpdate) { //handle event entitlement update ... } else { Log.Info(tag, "Unhandled GDEvent " + anEvent.EventType); } } private void OnNotAuthorized(GDAppEvent anEvent) { GDAppResultCode code = anEvent.ResultCode; if (code == GDAppResultCode.GDErrorActivationFailed || code == GDAppResultCode.GDErrorProvisioningFailed || code == GDAppResultCode.GDErrorPushConnectionTimeout || code == GDAppResultCode.GDErrorIdleLockout || code == GDAppResultCode.GDErrorRemoteLockout || code == GDAppResultCode.GDErrorPasswordChangeRequired || code == GDAppResultCode.GDErrorSecurityError || code == GDAppResultCode.GDErrorBlocked || code == GDAppResultCode.GDErrorAppDenied || code == GDAppResultCode.GDErrorWiped) { Log.Info(tag, "OnNotAuthorized " + anEvent.Message); } else if (code == GDAppResultCode.GDErrorIdleLockout) { Log.Info(tag, "OnNotAuthorized"); } else { Log.Info(tag, "Unhandled not authorized event"); } } private void OnAuthorized(GDAppEvent anEvent) { GDAppResultCode code = anEvent.ResultCode; if (code == GDAppResultCode.GDErrorNone) { if (!_started) { _started = true; //Launch app UI Here ... } } else { Log.Info(tag, "Authorized startup with an error"); } } } public class GDStateListener : Java.Lang.Object, IGDStateListener { private static GDStateListener _instance; public static GDStateListener Instance { get { if (_instance == null) _instance = new GDStateListener(); return _instance; } } public void OnAuthorized() { //Handle Authorized ... } public void OnLocked() { //Handle Locked ... } public void OnUpdateConfig(IDictionary<String, Java.Lang.Object> settings) { //Handle UpdateConfig ... } public void OnUpdatePolicy(IDictionary<String, Java.Lang.Object> policyValues) { //Handle UpdatePolicy ... } public void OnUpdateServices() { //Handle UpdateServices ... } public void OnWiped() { //Handle Wiped ... } public void OnUpdateEntitlements() { //Handle Wiped ... } } }
  2. In your app file (
    <project_path>
    /Application.cs
    ), assign the static
    IGDEventListener
    and
    IGDStateListener
    implementations to the
    GDAndroid
    class. Authorization occurs after the
    OnCreate
    method completes. Any interaction with the
    BlackBerry Dynamics Runtime
    outside of authorization within this method results in an exception.
    [Application] public class GoodApplication : Application { public GoodApplication (IntPtr handle, JniHandleOwnership transfer) : base (handle, transfer) { // do any initialisation you want here (for example initialising properties) } public override void OnCreate () { base.OnCreate (); GDAndroid.Instance.SetGDAppEventListener (GDEventListener.Instance); GDAndroid.Instance.SetGDStateListener(GDStateListener.Instance); … } }
  3. Initialize each
    Activity
    of your app for use with the
    BlackBerry Dynamics Runtime
    . In the
    OnCreate
    method, pass the current instance of the activity to the
    GDAndroid.Instance.ActivityInit
    method.
    protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); GDAndroid.Instance.ActivityInit (this); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); //Rest of Activity code below. … }