Skip to main content

2011 | Buch

Beginning Android 3

insite
SUCHEN

Über dieses Buch

The vibrant and rich Android development platform, created by Google and the Open Handset Alliance, continues to be a platform in its truest sense, encompassing hundreds of classes beyond the traditional Java classes and open source components that ship with the software development kit. Android's continued growth includes support for Flash and Flash gaming apps, Wi-Fi tethering, improved performance, WebM or WebMedia integration for HTML5-based video and other multimedia APIs, Chrome OS (WebOS) integration, and more.

With Beginning Android 3, you’ll learn how to develop applications for Android 3 mobile devices using simple examples that are ready to run with your copy of the software development kit. Author, Android columnist, developer, and community advocate Mark L. Murphy will show you what you need to know to get started programming Android applications, including how to craft graphical user interfaces, use GPS, multi-touch, multi-tasking, and access web services.

Inhaltsverzeichnis

Frontmatter

Core Concept

Frontmatter
Chapter 1. The Big Picture

Android is everywhere. Phones. Tablets. TVs and set-top boxes powered by Google TV. Soon, Android will be in cars and all sort of other places as well.

Mark Murphy
Chapter 2. How to Get Started

Without further ado, let’s get you set up with the pieces and parts necessary to build an Android app.

Mark Murphy
Chapter 3. Your First Android Project

Now that you have the Android SDK, it is time to make your first Android project. The good news is that this requires zero lines of code—Android’s tools create a “Hello, world!” application for you as part of creating a new project. All you need to do is build it, install it, and watch it open on your emulator or device.

Mark Murphy
Chapter 4. Examining Your First Project

The previous chapter stepped you through creating a stub project. This chapter describes what is inside of this project, so you understand what Android gives you at the outset and what the roles are for the various directories and files.

Mark Murphy
Chapter 5. A Bit About Eclipse

Eclipse is an extremely popular integrated development environment (IDE), particularly for Java development. It is also designed to be extensible via an add-in system. To top it off, Eclipse is open source. That combination made it an ideal choice of IDE for the core Android developer team.

Mark Murphy
Chapter 6. Enhancing Your First Project

The AndroidManifest.xml file that Android generated for your first project gets the job done. However, for a production application, you may wish to consider adding a few attributes and elements, such as those described in this chapter.

Mark Murphy

Activities

Frontmatter
Chapter 7. Rewriting Your First Project

The project you created in Chapter 3 is composed of just the default files generated by the Android build tools—you did not write any Java code yourself. In this chapter, you will modify that project to make it somewhat more interactive. Along the way, you will examine the basic Java code that comprises an Android activity.

Mark Murphy
Chapter 8. Using XML-Based Layouts

While it is technically possible to create and attach widgets to your activity purely through Java code, as we did in the preceding chapter, the more common approach is to use an XML-based layout file. Dynamic instantiation of widgets is reserved for more complicated scenarios, where the widgets are not known at compile time (e.g., populating a column of radio buttons based on data retrieved from the Internet).

Mark Murphy
Chapter 9. Employing Basic Widgets

Every GUI toolkit has some basic widgets: fields, labels, buttons, and so forth. Android’s toolkit is no different in scope, and the basic widgets provide a good introduction to how widgets work in Android activities.

Mark Murphy
Chapter 10. Working with Containers

Containers pour a collection of widgets (and possibly child containers) into specific structures you like. If you want a form with labels on the left and fields on the right, you need a container. If you want OK and Cancel buttons to be beneath the rest of the form, next to one another, and flush to right side of the screen, you need a container. Just from a pure XML perspective, if you have multiple widgets (beyond RadioButton widgets in a RadioGroup), you need a container just to have a root element in which to place the widgets.

Mark Murphy
Chapter 11. The Input Method Framework

Android 1.5 introduced the input method framework (IMF), which is commonly referred to as

soft keyboards

. However, this term is not necessarily accurate, as IMF could be used for handwriting recognition or other means of accepting text input via the screen.

Mark Murphy
Chapter 12. Using Selection Widgets

In Chapter 11, you saw how fields could have constraints placed on them to limit possible input, such as numeric-only or phone-number-only. These sorts of constraints help users “get it right” when entering information, particularly on mobile devices with cramped keyboards.

Mark Murphy
Chapter 13. Getting Fancy with Lists

The humble ListView is one of the most important widgets in all of Android, simply because it is used so frequently. Whether choosing a contact to call, an e-mail message to forward, or an e-book to read, ListView widgets are employed in a wide range of activities. Of course, it would be nice if they were more than just plain text.

Mark Murphy
Chapter 14. Still More Widgets and Containers

This book has covered a number of widgets and containers so far. This chapter is the last that focuses exclusively on widgets and containers, covering a number of popular options, from date and time widgets to tabs. After this chapter, we introduce new widgets occasionally, but in the context of some other topic, such as introducing the ProgressBar in Chapter 20 (covering threads).

Mark Murphy
Chapter 15. Embedding the WebKit Browser

Other GUI toolkits let you use HTML for presenting information, from limited HTML renderers (e.g., Java/Swing and wxWidgets) to embedding Internet Explorer into .NET applications. Android is much the same, in that you can embed the built-in web browser as a widget in your own activities, for displaying HTML or full-fledged browsing. The Android browser is based on WebKit, the same engine that powers Apple’s Safari web browser.

Mark Murphy
Chapter 16. Applying Menus

Like applications for the desktop and some mobile operating systems, Android supports activities with application menus. Most Android phones have a dedicated menu key for popping up the menu; other devices offer alternate means for triggering the menu to appear, such as the onscreen button used by the Archos 5 Android tablet.

Mark Murphy
Chapter 17. Showing Pop-Up Messages

Sometimes, your activity (or other piece of Android code) will need to speak up.

Mark Murphy
Chapter 18. Handling Activity Lifecycle Events

As you know, Android devices, by and large, are phones. As such, some activities are more important than others—taking a call is probably more important to users than playing Sudoku. And, since it is a phone, it probably has less RAM than your current desktop or notebook.

Mark Murphy
Chapter 19. Handling Rotation

Some Android devices offer a slide-out keyboard that triggers rotating the screen from portrait to landscape orientation. Other devices use accelerometers to determine when the screen rotates. As a result, it is reasonable to assume that switching from portrait to landscape orientation and back again may be something that users of your application will want to do.

Mark Murphy
Chapter 20. Dealing with Threads

Users like snappy applications. Users do not like applications that feel sluggish. The way to help make your application feel snappy to users is to use the standard threading capabilities built into Android. This chapter will walk you through the issues involved with thread management in Android and some of the options for keeping the UI crisp and responsive.

Mark Murphy
Chapter 21. Creating Intent Filters

Up to now, the focus of this book has been on activities opened directly by the user from the device’s launcher. This is the most obvious case for getting your activity up and visible to the user. And, in many cases, it is the primary way the user will start using your application.

Mark Murphy
Chapter 22. Launching Activities and Subactivities

The theory behind the Android UI architecture is that developers should decompose their application into distinct activities. For example, a calendar application could have activities for viewing the calendar, viewing a single event, editing an event (including adding a new one), and so forth. This implies that one of your activities has the means to start up another activity. For example, if a user selects an event from the view-calendar activity, you might want to show the view-event activity for that event. This means that you need to be able to cause the view-event activity to launch and show a specific event (the one the user chose).

Mark Murphy
Chapter 23. Working with Resources

Resources are static bits of information held outside the Java source code. You have seen one type of resource—the layout—frequently in the examples in this book. There are many other types of resources, such as images and strings, that you can take advantage of in your Android applications.

Mark Murphy
Chapter 24. Defining and Using Styles

Every now and then, you will find some code with a cryptic style attribute in a layout element. For example, in the chapter on threading, the following ProgressBar was presented: <?xml version=“1.0” encoding=“utf-8”?> < LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:orientation=“vertical” android:layout_width=“fill_parent” android:layout_height=“fill_parent” > <ProgressBar android:id=“@+id/progress” style=“?android:attr/progressBarStyleHorizontal” android:layout_width=“fill_parent” android:layout_height=“wrap_content” /> </LinearLayout>

Mark Murphy
Chapter 25. Handling Multiple Screen Sizes

For the first year or so after Android 1.0 was released, all production Android devices had the same screen resolution (HVGA, 320×480) and size (around 3.5 inches, or 9 centimeters). Starting in the fall of 2009, though, devices started arriving with widely disparate screen sizes and resolutions, from tiny QVGA (240×320) screens to much larger WVGA (480×800) screens. And, in the fall of 2010, tablets and Google TV devices appeared, offering yet more screen sizes.

Mark Murphy

Honeycomb and Tablets

Frontmatter
Chapter 26. Introducing the Honeycomb UI

February 2011 saw the introduction of Android 3.0 and a UI paradigm that, for now, we will refer to by the Android 3.0 codename, Honeycomb. Android 3.0 itself is exclusively targeted at tablets, though elements of the Honeycomb UI system will make it into future versions of Android that support phones as well. The Honeycomb UI is perhaps the biggest single change in Android since Android 0.9, before the first phones were available. The impacts of Honeycomb will resound throughout the Android ecosystem for a long time as people adjust to make use of its features.

Mark Murphy
Chapter 27. Using the Action Bar

One of the easiest ways to make your application blend in better with the Honeycomb UI is to enable the action bar, introduced in Chapter 26. What makes it “easy” is that most of the basic functionality of the action bar is backward compatible—the Honeycomb settings will not cause the application to crash on earlier versions of Android.

Mark Murphy
Chapter 28. Fragments

Perhaps the largest change in Android 3.0 facing Android developers is the introduction of the fragment system. This is an optional layer you can put between your activities and your widgets, designed to help you reconfigure your activities to support screens both large (e.g., tablets) and small (e.g., phones). However, the fragment system also adds an extra layer of complexity, one that will take the Android developer community some time to adjust to. Hence, for the time being, you will find few blog posts or sample apps using fragments because they were introduced so long after Android itself was.

Mark Murphy
Chapter 29. Handling Platform Changes

Android has been rapidly evolving since its initial release, and will continue to do so over the next few years. Perhaps, in time, the rate of change will decline some. However, for the present, you should assume that there will be significant Android releases every 6 to 12 months, and changes to the lineup of possible Android hardware on an ongoing basis. So, while right now the focus of Android is phones, soon you will see Android netbooks, Android tablets, Android media players, and so on.

Mark Murphy
Chapter 30. Accessing Files

While Android offers structured storage, via preferences and databases, sometimes a simple file will suffice. Android offers two models for accessing files: one for files prepackaged with your application and one for files created on-device by your application.

Mark Murphy

Data Stores, Network Services, and APIs

Frontmatter
Chapter 31. Using Preferences

Android has many different ways for you to store data for long-term use by your activity. The simplest to use is the preferences system, which is the focus of this chapter.

Mark Murphy
Chapter 32. Managing and Accessing Local Databases

SQLite is a very popular embedded database, as it combines a clean SQL interface with a very small memory footprint and decent speed. Moreover, it is public domain, so everyone can use it. Many firms (e.g., Adobe, Apple, Google, Sun, and Symbian) and open source projects (e.g., Mozilla, PHP, and Python) ship products with SQLite.

Mark Murphy
Chapter 33. Leveraging Java Libraries

Java has as many third-party libraries as any other modern programming language, if not more. These third-party libraries are the innumerable JARs that you can include in a server or desktop Java application—the things that the Java SDKs themselves do not provide.

Mark Murphy
Chapter 34. Communicating via the Internet

The expectation is that most, if not all, Android devices will have built-in Internet access. That could be Wi-Fi, cellular data services (EDGE, 3G, etc.), or possibly something else entirely. Regardless, most people—or at least those with a data plan or Wi-Fi access— will be able to get to the Internet from their Android phone.

Mark Murphy

Services

Frontmatter
Chapter 35. Services: The Theory

As noted previously, Android services are for long-running processes that may need to keep running even when decoupled from any activity. Examples include playing music even if the player activity gets garbage-collected, polling the Internet for RSS/Atom feed updates, and maintaining an online chat connection even if the chat client loses focus due to an incoming phone call.

Mark Murphy
Chapter 36. Basic Service Patterns

Now that you have seen the pieces that make up services and their clients, let us examine a few scenarios that employ services and how those scenarios might be implemented.

Mark Murphy
Chapter 37. Alerting Users via Notifications

Pop-up messages, tray icons and their associated “bubble” messages, bouncing dock icons...you are no doubt used to programs trying to get your attention, sometimes for good reason. Your phone also probably chirps at you for more than just incoming calls: low battery, alarm clocks, appointment notifications, incoming text and e-mail messages, and so on.

Mark Murphy

Other Android Capabilities

Frontmatter
Chapter 38. Requesting and Requiring Permissions

In the late 1990s, a wave of viruses spread through the Internet, delivered via e-mail, using contact information culled from Microsoft Outlook. A virus would simply e-mail copies of itself to each of the Outlook contacts that had an e-mail address. This was possible because, at the time, Outlook did not take any steps to protect data from programs using the Outlook API, since that API was designed for ordinary developers, not virus authors.

Mark Murphy
Chapter 39. Accessing Location-Based Services

A popular feature on current mobile devices is GPS capability, so the device can tell you where you are at any point in time. While the most popular uses of GPS service are for mapping and getting directions, there are other things you can do if you know your location. For example, you might set up a dynamic chat application based on physical location, so users can chat with those people who are nearest to them. Or, you could automatically geo-tag posts to Twitter or similar services.

Mark Murphy
Chapter 40. Mapping with MapView and MapActivity

One of Google’s most popular services—after Search, of course—is Google Maps, which enables you to map everything from the location of the nearest pizza parlor to directions from New York City to San Francisco (only 2,905 miles!), and includes street views and satellite imagery.

Mark Murphy
Chapter 41. Handling Telephone Calls

Many, if not most, Android devices will be phones. As such, not only will users be expecting to place and receive calls using Android, but you will have the opportunity to help them place calls, if you wish.

Mark Murphy
Chapter 42. Fonts

The question you’ll inevitably get when developing any type of application is, “Hey, can we change this font?” The answer depends on which fonts come with the platform, whether you can add other fonts, and how to apply them to the widget or whatever needs the font change. Android is no different. It comes with some fonts, plus a means for adding new fonts. However, as with any new environment, there are a few idiosyncrasies to deal with, as described in this chapter.

Mark Murphy
Chapter 43. More Development Tools

The Android SDK is more than a library of Java classes and API calls. It also includes a number of tools to assist in application development. Eclipse, of course, tends to dominate the discussion. However, that is not the only tool at your disposal, so, let’s take a quick tour of what else is available to you.

Mark Murphy

Alternative Application Environments

Frontmatter
Chapter 44. The Role of Alternative Environments

You might think that Android is all about Java. The official Android Software Development Kit (SDK) is for Java development, the build tools are for Java development, the Android discussion groups and blog posts are all about Java, and, yes, most Android books are for Java development. Heck, most of this book is about Java.

Mark Murphy
Chapter 45. HTML5

Prior to the current wave of interest in mobile applications, the technology

du jour

was web applications. A lot of attention was paid to AJAX, Ruby on Rails, and other techniques and technologies that made the experience of using web applications close to, and sometimes even superior to, the experience of using a desktop application.

Mark Murphy
Chapter 46. PhoneGap

PhoneGap is perhaps the original alternative application framework for Android, arriving on the scene in early 2009. PhoneGap (www.phonegap.com/) is open source, backed by Nitobi, which offers a mix of open source and commercial products, along with consulting and training services.

Mark Murphy
Chapter 47. Other Alternative Environments

The alternative application environments described in the preceding chapters represent but a few of the growing flood of such technologies. Here, we will take a brief look at a few other alternative application environments.

Mark Murphy

The Ever-Evolving Android

Frontmatter
Chapter 48. Dealing with Devices

Android is “free as in beer” for device manufacturers, as it is an open source project. Hence, device manufacturers have carte blanche to do what they want with Android as they put it on their devices. This means a breadth of choices for device users, who can choose among Android devices in a variety of shapes, sizes, and colors. This also means developers have some device differences and idiosyncrasies to take into account.

Mark Murphy
Chapter 49. Where Do We Go from Here?

Obviously, this book does not cover everything. And while your primary resource (besides the book) is the Android SDK documentation, you are likely to need information from additional sources.

Mark Murphy
Backmatter
Metadaten
Titel
Beginning Android 3
verfasst von
Mark Murphy
Copyright-Jahr
2011
Verlag
Apress
Electronic ISBN
978-1-4302-3298-8
Print ISBN
978-1-4302-3297-1
DOI
https://doi.org/10.1007/978-1-4302-3298-8