Hosting a public web site (ASP.NET) and an internal services site (WCF) in one Windows Azure web role – sample code
You bet: there will be no distributed application without security - and the "new security" is claims-based identity: Learn it from the master of claims, WIF and ACS

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.

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.

Your Information

(Name is required. Email address will not be displayed with the comment.)