Book Image

Android 3.0 Application Development Cookbook

By : Kyle Merrifield Mew
Book Image

Android 3.0 Application Development Cookbook

By: Kyle Merrifield Mew

Overview of this book

<p>Android is a mobile operating system that runs on a staggering number of smartphones and tablets. Android offers developers the ability to build extremely rich and innovative applications written using the Java programming language. Among the number of books that have been published on the topic, what&rsquo;s missing is a thoroughly practical, hands-on book that takes you straight to getting your job done without boring you with too much theory.<br /><br />Android 3.0 Application Development Cookbook will take you straight to the information you need to get your applications up and running. This book is written to provide you with the shortest possible route between an idea and a working application. <br /><br />Work through the book from start to finish to become an Android expert, or use it as a reference book by applying recipes directly to your project.<br /><br />This book covers every aspect of mobile app development, starting with major application components and screen layout and design, before moving on to how to manage sensors such as internal gyroscopes and near field communications. Towards the end, it delves into smartphone multimedia capabilities as well as graphics and animation, web access, and GPS. <br /><br />Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android 3.</p>
Table of Contents (18 chapters)
Android 3.0 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Starting a new activity with an intent object


The Android application model can be seen as a service-oriented one, with activities as components and intents as the messages sent between them. Here, an intent is used to start an activity that displays the user's call log, but intents can be used to do many things and we will encounter them throughout this book.

Getting ready

To keep things simple, we are going to use an intent object to start one of Android's built-in activities, rather than create a new one. This only requires a very basic application, so start a new Android project with Eclipse and call it ActivityStarter or something like that.

How to do it...

We are going to edit the Java subclass responsible for the main activity: the one declared in the manifest file. This class extends the activity class, and by overriding its onCreate() method we can introduce code that will be executed when the application is first launched:

  1. Using the Package Explorer, open the Java file inside the src folder of the project. It will have the same name as the activity, entered when the project was created:

  2. Add a new method to the class, similar to this one:

    void startActivity() {
      Intent myIntent = new Intent();
      myIntent.setAction(Intent.ACTION_CALL_BUTTON);
      startActivity(myIntent);
    }
  3. Now, call this method from the onCreate() method so that it executes when the application is launched:

    @Override
    public void onCreate(Bundle state) {
      super.onCreate(state);
      setContentView(R.layout.main);
    
      startActivity();
    }
  4. Save and run the project. The application now displays the user's call log.

  5. If this generates an error message, it may be that the correct libraries have not been imported. To use intents we have to import the relevant library, which can be done with import android.content.Intent; however it's easy to get Eclipse to import any missing libraries simply by pressing Shift + Ctrl + O.

  6. Press the back button on the device (or emulator) to see that the call log activity was actually called from our original main activity.

How it works...

Intents operate as asynchronous messages sent between application components and they are used to activate services and broadcast receivers as well as activities. Intents are passive data structures that provide an infrastructure for our activities and other components.

The onCreate() method is called as soon as the activity starts and so calling our startActivity() method from within it means that we are immediately taken to the call log activity. More often than not we would use a button or menu item to perform such an action, and we haven't done so in order to simplify the demonstration and make it easier to incorporate in your own application.

Again, note that this project was built against Android 2.2 (API level 8) but this choice was arbitrary as the libraries used have been available since Android 1.5 and you should, ideally, build against the target device that you are testing on.

There's more...

The previous example required only an action to be set but most intent objects make use of a setData() method as well as the setAction() method used.

Setting data and action

Replace the setAction() statement in the example with these two lines:

myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

This will open the device's image gallery when run and utilize both data and action parts of the intent.

Exploring other functions with auto-complete

Eclipse's auto-complete function allows us to explore Android's other baked-in activities. Simply start entering the code here and then scroll through the lists presented:

If the drop-down list fails to appear, press Ctrl + Space but note that when components share methods you may well see actions that correspond to other classes such as services or broadcasts, although the inline documentation is quite thorough and will mention when specific data or extra parameters are required.

See also

To start an activity from a menu selection, see the recipe Handling menu selections in Chapter 4, Menus.