DBPedias

Your Database Knowledge Community

Christian Weyer's Blog

Cross-device/cross-location Pub-Sub (part 3): Using Windows Azure Service Bus Topics Subscriptions in Android with Mono for Android

And here we go with the final piece of blogging around the Windows Azure Service Bus and its cross-device, cross-location pub/sub features.

There have already been two articles about this topic (pun intended Winking smile)


Today I have built a very simple Android app with MonoDroid (aka Mono for Android) in C#. The code is essentially the same as for the iOS demo shown earlier (with MonoTouch), I just used Monodroid.Dialog to programmatically wire up the UI.

image

This is the entire code for the app:

   1:  [Activity(Label = "DroidServiceBusSubscriber",
   2:    MainLauncher = true, Icon = "@drawable/icon")]
   3:  public class MainActivity : Activity
   4:  {
   5:      private ListView lv;
   6:      private DialogAdapter da;
   7:      private RootElement menu;
   8:      private Section messages;
   9:      private BrokeredMessaging broker;
  10:      private string sbNamespace = "YOUR_SB_NS";
  11:      private string issuerName = "owner";
  12:      private string issuerSecret = "YOUR_OWNER_ISSUERKEY";
  13:      private string topicName = "newstopic";
  14:      private string subscriptionName = "androidsubscription";
  15:   
  16:      protected override void OnCreate(Bundle bundle)
  17:      {
  18:          base.OnCreate(bundle);
  19:   
  20:          InitBroker();
  21:          SetupUI();
  22:          RegisterMessagesHandler();
  23:      }
  24:   
  25:      private void InitBroker()
  26:      {
  27:          broker = new BrokeredMessaging(sbNamespace);
  28:          broker.GetToken(issuerName, issuerSecret);
  29:      }
  30:   
  31:      private void SetupUI()
  32:      {
  33:          messages = new Section("Messages");
  34:          menu = new RootElement("Service Bus Subscriber")
  35:          {
  36:              messages
  37:          };
  38:   
  39:          da = new DialogAdapter(this, menu);
  40:          lv = new ListView(this) { Adapter = da };
  41:              
  42:          SetContentView(lv);            
  43:      }
  44:   
  45:      private void RegisterMessagesHandler()
  46:      {
  47:          Task.Factory.StartNew(() =>
  48:          {
  49:              while (true)
  50:              {
  51:                  try
  52:                  {
  53:   
  54:                      var message = broker.ReceiveAndDeleteMessage(topicName + 
  55:                        "/Subscriptions/" + subscriptionName);
  56:   
  57:                      if (!String.IsNullOrWhiteSpace(message))
  58:                      {
  59:                          RunOnUiThread(delegate
  60:                          {
  61:                              messages.Elements.Add(new StringElement(message));
  62:                              da.NotifyDataSetChanged();
  63:                          });
  64:                      }
  65:   
  66:                  }
  67:                  catch (Exception ex)
  68:                  {
  69:                      Console.WriteLine(ex);
  70:                  }
  71:              }
  72:          });
  73:      }
  74:  }

 

And the ServiceBusBrokeredMessaging class is exactly copied and pasted from the MonoTouch project to the VS 2010 project for MonoDroid.

Here is the client app solution (you will need Mono for Android for Visual Studio):


Hope this helps.

Item Information

Published
Contributor
Christian Weyer
Comments
0 comments
Tags
azure, sql-azure
Content Type
Entry

Comments

Speak Your Mind

  • No HTML is allowed.