All about Intents

Dublin Buzz uses the Android Intent system to either launch other internal activities (e.g. when viewing a sight) or launching external ones (e.g. launching a web browser). Intents can be thought of as a very general, flexible, dynamic messaging system. Activities can be very loosely coupled, meaning that you just declare that you want some other activity to handle some action for you and – little bit of magic dust sprinkled here – it launches.
Okay, it ain’t quite that easy. But it is certainly straightforward.

I’m going to show you how I use internal activities in my Dublin Buzz application. For that, you’ll need to look at the code in SightViewer.java, available as a text file here: SightViewer. (You cannot append .java files in WordPress, so you’ll have to cut-and-paste it into your favourite editor…).

Let’s start by looking at the click event handler in Dublin Buzz. This is where the activity is launched from. (A user has clicked a sight button, say Kilmanham Jail).

v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
    	Intent launch = new Intent(c, SightViewer.class);
    	launch.putExtra("Sight", s);
    	startActivity(launch);
}});

The variable “v” (I know, bad name…) refers to the Button that was pressed, and was declared earlier. setOnClickListener assigns a method to be the listener for click events. I am using Java’s anonymous class mechanism to declare this method in-line. This saves some time and space, but can be a little odd if you’re seeing it for the first time. But just focus on the lines starting at:

    public void onClick(View arg0) {

There are three steps (in this specific example) to launching an activity via an intent: Step 1 creates an object to encapsulate the intent, Step 2 bundles up some data for the intent to use, and Step 3 actually launches the intent.

The first Step is where I declare my intention to launch an explicitly-named activity:

    Intent launch = new Intent(c, SightViewer.class);

The “c” variable is the current context, that is, this activity itself. It had been declared earlier using this simple line of code:

    final Context c = this;

The second step is to send some data to the called Intent, to allow it to display a Sight.

launch.putExtra(“Sight”, s);

Data being sent to an Intent is called an “Extra” in Android-speak. All extras a bundled into the intent using key/value pairs. In this case, “Sight” is the key, and the data is the current Sight, “s” (I love short, simple names. If this sends a shiver down your spine, I apologise!).
Finally, you launch the activity by calling the startActivity() method:

startActivity(launch);

Now we need to look at the receiver. This is where the activity begins. First, it must unbundle the extra data sent to it (the Sight).

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sightview);

    Intent sight = this.getIntent();
    if (sight!=null)
    {
      	if (sight.hasExtra("Sight"))
      	    theSight = (Sight)sight.getSerializableExtra("Sight");
    }
}

This code – after the standard call to the superclasses’ onCreate() method and setting the content view, pulls the extras down. It retrieves the intent that launched it, checks that this intent has some extra data with the key “Sight”, and then assigns that extra data to the variable theSight, using the getSerializableExtra() method, passing in the relevant key.
To make this code work, of course, the application’s manifest must explicity declare the SightViewer activity as being capable of handling a View action.

  <activity android:name=".SightViewer"
        android:label="@string/app_name">
   <intent-filter>
       <action android:name="android.intent.action.VIEW" />
       <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
  </activity>

So next time you click a button on my Dublin Buzz application, you’ll know just exactly how “they” do that!

Advertisements
Posted in Android, Android development, Mobile Apps | Leave a comment

Source code explanation – Part 1

Let’s start by letting you download the source code. Feel free to do what you will with this code!

DublinBuzz

WordPress has forced me to upload a document file format, so you need to load this text into your favourite Java editor and save with a .java extension!

The opening bit of code in the main activity just declares some essential variables. An Activity is Android’s version of a form (kinda). The DublinBuzz activity is the display for the list of sights. So that’s our first variable – viewable – which always holds the current list of sights. The rest are fairly self-explanatory. DublinBuzzer is the name of the Application class. This is used to hold state between orientation changes. We’ll talk about that later.

OnCreate is the event handler called when the activity first starts up. It should only get called once in the lifetime of the activity, so it can be useful for setting things up initially. The method call setContentView just tells the activity where to find its XML layout page (in this case, called “main”). The app variable holds a singleton instance of the Application class.    

That’s the first bit…off now to enjoy a night out in….Dublin! Be back to explain (much) more later.
Tony

Posted in Android, Android development, Mobile Apps | Leave a comment

Dublin Buzz description

Here are some cool screen grabs of the running application to whet your appetite:
This is the opening screen, with all of the sights listed alphabetically.

Obviously, the main plus of using a mobile device is to be able to know your position. I live in Kilmainham and – with GPS on – I listed the sights by distance from my current location:

Note how the app lists everything in order of nearest to farthest. No more of that annoying situation when you’re visiting a city and of either missing a brilliant sight you happening to be 100 metres from, or travelling half way across the city to visit something, eh, disappointing. Now, you can list everything by distance, have a quick root around to see if it would interest you, perhaps pop off to their website to see when they’re open, then pop off to Google maps to find your way there. Frankly speaking, if this doesn’t spell the end of guide “books”, then I don’t know what will.

Dublin Buzz features pretty pictures of every sight you might want to see, to get a little taster of what to expect. Here’s Kilmainham Jail for instance:

If you’re not convinced by my short blurb, then you can quickly jump off to their own website instead…and if its a good website, like the Chester Beatty Library’s one, then it’ll display quite nicely in your browser:

Perhaps you like the look of something, but you’re thinking of doing it the next day. Then, Dublin Buzz lets you add sights to your list of favourites:

You can list all of your favourites at any time:

Still not convinced? Then you can also view different types of sight, for example historic, architectural, or go straight for a drinking expedition!

Finally, no quick description of Dublin Buzz could be complete without seeing where, exactly, the sight you want is. Google Maps (as long as you have it on your device) can do the hard work, while Dublin Buzz just adds a red dot to your target:

Posted in Android, Android development, Mobile Apps | Leave a comment

Dublin Buzz Now Available!

The Dublin Buzz app is now available on the Google Marketplace for Android apps. Search for “Dublin Buzz”. It is 100% free and 100% ad-free. Hopefully you’ll find it of use or fun. And I will be publishing the entire code base to this blog over the next day or two, to illuminate and describe all of my code, plus all non-code-related twists and turns in getting the app published.

Posted in Android, Android development, Mobile Apps | Leave a comment

Introducing Dublin Buzz

Dublin Buzz is a fantastic new Android app for tourists visiting Dublin who want to know where the best Sights are. It will be published very soon on the Google App Market, so look out for it! (Expected to be released around end of April 2011).

Highlights

  • Completely free, no catches!
  • Completely ad-free, no catches!
  • Features Dublin’s top 70 or so sights, including historic, architecture, museums, bars, clubs, and parks
  • You can read a short account of the sight including address, opening hours, and vital information
  • The interface is extremely easy to use
  • There are links to the each sight’s website 
  • View each sight on Google maps
  • When you’re in Dublin and GPS is on, you can list the sights by distance from where you currently are, which can be updated in real time

This is an essential Android app for all visitors to Dublin looking for something to do near where they are. This app was developed as part of a master’s degree in software engineering, so I’m not selling anything or promoting anybody. You’ll receive useful information in jig-time!

Posted in Android, Android development, Mobile Apps | Leave a comment