• 沒有找到結果。

Project Design

在文檔中 Learning Android (頁 98-105)

If you remember from Chapter 5, an Android application is a loose collection of activ‐

ities, services, content providers, and broadcast receivers. These are the components from which we put together an application. Figure 6-4 shows the design of the entire Yamba application, which incorporates most of the main Android building blocks.

Figure 6-4. Yamba design diagram

To help understand the diagrams as we keep moving through the the design, Figure 6-5 provides a quick legend of the design language that we have developed specifically for purposes of illustrating how Yamba comes together.

Figure 6-5. Design language

Part 1: Android User Interface

This part, covered in Chapters 7 and 8, will focus on developing the first component of the Yamba application: the Status Update screen. Our tasks are building an activity, networking and multithreading, and debugging:

Building an activity

We are going to start by introducing the Android user interface (UI) model. In its UI, Android is quite different from some other paradigms that you might be familiar with. This is done with a dual approach to UI using both Java and XML.

In this chapter, you will learn how to develop the user interface, as shown in Figure 6-2, where the user updates her status. Through this process, you will use XML and Java to put together a working UI. You will learn about views and layouts, units in Android, how to work with images, and how to make the UI look pretty.

Our approach will focus on best practices in UI development so that your applica‐

tion looks good and works well on any Android device, regardless of screen size and resolution. We’re going to develop a single app that will look great on phones, tablets, and TVs.

Networking and multithreading

Once we have a working screen, we will want to post the user input to the cloud service. For that purpose, we are going to use a library to help us with the Twitter API web service calls.

Part 1: Android User Interface | 81

Making the network calls is subject to the unpredictable nature of the network. To address that, we will introduce multithreading in Android and explain how to de‐

velop an app that works well regardless of external circumstances.

Debugging Android apps

A few things are going to go wrong in this section of the book. This is by design, because debugging is a normal part of application development. We’ll show you how to use the Android SDK tools to quickly find and fix problems. Debugging will become second nature to you.

Fragments

Android 3.0 called for a newer approach to the user interface. The need to handle multiple screen sizes and orientations led to the introduction of fragments. We will tackle this UI framework by taking what we have done before and converting them over to this new approach.

Part 2: Intents, ActionBar, and More

This part, covered in Chapter 9, is about using Android intents as a way to connect multiple parts together. At the end of this part, your Yamba application will have two screens: one for status updates and the other for setting up the preferences. At this point, Yamba is configurable for various users and starts being a useful app. The elements we’ll create at this stage are the activity, the menu system, and intents to glue them all together:

The Preference activity

First, we’ll create the screen as an activity, one of Android’s basic building blocks.

You will see the steps involved and understand what it takes to create new screens.

Intents, ActionBar, and menu system

Next, we’ll need a way to get to that screen. For that purpose, we’ll introduce Action Bar as a menu system in Android and show how it works. You will also learn about intents and how to send these to open up a specific activity.

Filesystem

Finally, we’ll learn about the filesystem on a typical Android device. You will gain a deeper understanding of how the operating system is put together, and you will also learn more about Android security.

Part 3: Android Services

This part, covered in Chapter 10, introduces background services. By the end of this part, your Yamba application will be able to periodically connect to the cloud and pull down your friends’ status updates:

Services

Android services are very useful building blocks. They allow a process to run in the background without requiring any user interface. This is perfect for Yamba, because we’ll have an update process connect to the cloud periodically and pull the data. In this section, you will also learn about multithreading considerations as they apply to background services.

Intent services

These are a convenient way to run a task off the main thread so that thread can continue to handle user interaction. We’ll use one to get updates from Twitter.

Part 4: Content Providers

We now have the data from our refresh service, so we need a place to store it. In this part, covered in Chapter 11, we’ll introduce you to Android’s support for content pro‐

viders, and data sources in general. By the end of that chapter, our data from the cloud will be persisted in the database:

SQLite and Android’s support

Android ships with a built-in database called SQLite. In addition to this cool little database, the Android framework offers a rich API that makes SQLite easier for us to use. In this section, you will learn how to use SQLite and the API for it. You do not have to be a SQL buff to understand what is going on, but some basic under‐

standing of SQL always helps.

The dbHelper class

To let you invoke the most common database operations without using SQL, An‐

droid rovides a class with the common insert(), query(), update(), and de lete() operations.

ContentProvider

We’ll implement a new Android component to hold cached data and connect our app to it.

Part 5: Lists and Adapters

It might sound like we’re back in UI mode, but lists and adapters are more organizational aids than user interface elements in Android. They form very powerful components that allow our tiny UI to connect to very large datasets in an efficient and scalable manner. In other words, users will be able to use Yamba in the real world without any performance hits in the long run.

Part 4: Content Providers | 83

Currently the data is all there in the database, but we have no way to view it. In this part, covered in Chapter 12, the Yamba application will get the much-needed TimelineAc tivity and a way for the user to see what his friends are chatting about online.

Part 6: Broadcast Receivers

Here we develop a third activity, doing so in multiple stages. First, we’ll use our existing knowledge of the Android UI and put something together. It will work, but will not be as optimal as it could be. Finally, we’ll get it right by introducing Lists and Adapters to the mix to use them to tie the data to our user interface.

In this part, covered in Chapter 13, we’ll equip Yamba with receivers so it can react to events around it in an intelligent way. For that purpose, we’ll use broadcast receivers.

We show how to use Android permissions to make sure other people can’t post statuses under the user’s name:

Boot and network receivers

In our example, we want to start our updates when the device is powered up. We also want to stop pulling the data from the cloud when the network is unavailable, and start it again only when we’re back online. This goal will introduce us to one type of broadcast receiver.

Timeline receiver

This type of receiver will exist only at certain times. Also, it won’t receive messages from the Android system, but from other parts of our own Yamba application. This will demonstrate how we can use receivers to put together loosely coupled com‐

ponents in an elegant and flexible way.

Permissions

At this point in the development process you know how to ask for system permis‐

sions, such as access to the Internet or filesystem. In this section, you’ll learn how to define your own permissions and how to enforce them. After all, Yamba com‐

ponents might not want to respond to any other application for some Yamba-specific actions.

Part 7: App Widgets

In this part, covered in Chapter 14, we’ll look at how to use Android app widgets to create a home screen widget that displays the latest tweets:

Android widgets

But who will remember to pull up our app? To demonstrate the usefulness of our new status data, we’ll put together an app widget. App widgets are those little com‐

ponents that the user can put on the home screen to see weather updates and such.

We’ll create a widget that will pull the latest status update from the Yamba database via the status data content provider and display it on the home screen.

Part 8: Networking and the Web (HTTP)

Up till now we have provided the underlying communication piece to our example application via a library. Here we want to take a brief step back and talk about how this communication is done and what Android’s APIs provide to communicate via HTTP.

Part 9: Live Wallpaper and Handlers

As a final piece to the application, we wanted to provide some more interaction at a system level. One of those ways is a fun, recently added concept in Android called Live Wallpaper, which runs on the home screen of the device. We build out a basic Live Wallpaper that interacts with the user and displays the messages communicated through the backend service. We also cover an important class called the Handler that enables another means to interact with the main UI thread from a different thread.

Summary

This chapter is intended as a road map for the next eight chapters. By the end of all these iterations, you will have built a medium-size Android app from scratch. Even more, you will understand various constructs and how to put them together into a meaningful whole. The hope is that you’ll start developing a way of thinking in Android.

Part 8: Networking and the Web (HTTP) | 85

CHAPTER 7

Android User Interface

In this chapter, you will learn how to build a user interface in Android. You will create your first activity, create an XML layout for it, and see how to connect it to your Java code. You will learn about views (a.k.a. widgets) and layouts, and learn how to handle Java events, such as button clicks. Additionally, you’ll add support for a Twitter-like API into your project as an external .jar file so your app can make web service calls to the cloud.

By the end of this chapter, you will have written your own Twitter-like Android app.

The app will feature a single screen that will prompt the user for her current status update and post that update online.

在文檔中 Learning Android (頁 98-105)