10
Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 1 Android Developers Winners Google’s Android mobile phone software platform is the next big opportunity for application software developers. With the existing mobile development build on proprietary operating systems that restrict the development and deployment of third-party applications, Android offers an open alternative. Android developers are free to write applications that enjoys full advantage of powerful mobile hardware and distribute them in an open market. Built on an open source framework, and featuring powerful SDK libraries and a much open philosophy, Android has opened mobile phone development to thousands of developers who haven’t had access to tools for building mobile applications. Experienced mobile developers can now expand into the Android platform, leveraging the unique features to enhance existing products or create innovative ones. Using the Android Market for distribution, developers can take advantage of an open marketplace, with no review process, for distributing free and paid apps to all compatible Android devices. What are the problems that Android improves? Fragmentation: Till now there are a lot of phones available which can run applications, but each brand has a different application environment. This is mostly true in case of Linux based phones, where each handset vendor has had to assemble plenty of pieces of third-party software to create a viable mobile phone platform. Java was supposed to help this situation. Unfortunately, almost every handset that supports J2ME also supports vendor- proprietary extensions that limit the portability of applications. Proprietary Software Stacks: Most existing smartphones such as Nokia’s Series 60 with Symbian OS, or Microsoft’s Windows Mobile use proprietary & relatively closed software stacks. Modifications to these stacks (example - adding a device driver) have to be done either by the stack owner or by the handset manufacturer. The stacks are not open source, so changing anything in the stack is difficult. Most Linux-based phones to date have an open source kernel, but keep other details of the software stack (application framework, multimedia framework, applications) proprietary. Closed Networks: Mobile operators often lock the handsets so applications cannot be added. The operators claim this is needed to preserve the integrity of their mobile networks. It also suits the operator’s business model. Android includes an open catalog of applications, Android Market, which users can download over the air to their Android phones. It also allows direct loading of applications via USB connection. Android gives developers a way to develop unique, creative applications and get those applications in the hands of customers.

Android workshop handout

Embed Size (px)

Citation preview

Page 1: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 1

Android Developers – Winners

Google’s Android mobile phone software platform is the next big opportunity for application software developers.

With the existing mobile development build on proprietary operating systems that restrict the development and

deployment of third-party applications, Android offers an open alternative. Android developers are free to write

applications that enjoys full advantage of powerful mobile hardware and distribute them in an open market.

Built on an open source framework, and featuring powerful SDK libraries and a much open philosophy, Android has

opened mobile phone development to thousands of developers who haven’t had access to tools for building mobile

applications. Experienced mobile developers can now expand into the Android platform, leveraging the unique

features to enhance existing products or create innovative ones.

Using the Android Market for distribution, developers can take advantage of an open marketplace, with no review

process, for distributing free and paid apps to all compatible Android devices.

What are the problems that Android improves?

Fragmentation: Till now there are a lot of phones available which can run applications, but each brand has a

different application environment. This is mostly true in case of Linux based phones, where each handset vendor has

had to assemble plenty of pieces of third-party software to create a viable mobile phone platform. Java was

supposed to help this situation. Unfortunately, almost every handset that supports J2ME also supports vendor-

proprietary extensions that limit the portability of applications.

Proprietary Software Stacks: Most existing smartphones such as Nokia’s Series 60 with Symbian OS, or Microsoft’s

Windows Mobile use proprietary & relatively closed software stacks. Modifications to these stacks (example - adding

a device driver) have to be done either by the stack owner or by the handset manufacturer. The stacks are not open

source, so changing anything in the stack is difficult. Most Linux-based phones to date have an open source kernel,

but keep other details of the software stack (application framework, multimedia framework, applications)

proprietary.

Closed Networks: Mobile operators often lock the handsets so applications cannot be added. The operators claim

this is needed to preserve the integrity of their mobile networks. It also suits the operator’s business model. Android

includes an open catalog of applications, Android Market, which users can download over the air to their Android

phones. It also allows direct loading of applications via USB connection.

Android gives developers a way to develop unique, creative applications and get those applications in the hands of

customers.

Page 2: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 2

Component LifeCycle

Component Services

Component – Content Providers

Producing an Android App

Page 4: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 4

Screen Characteristic

Screen characteristic Qualifier Description

Size

small Resources for small size screens.

normal Resources for normal size screens. (This is the baseline size.)

large Resources for large size screens.

xlarge Resources for extra large size screens.

Density

ldpi Resources for low-density (ldpi) screens (~120dpi).

mdpi Resources for medium-density (mdpi) screens (~160dpi).

(This is the baseline density.)

hdpi Resources for high-density (hdpi) screens (~240dpi).

xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).

nodpi

Resources for all densities. These are density-independent

resources. The system does not scale resources tagged with

this qualifier, regardless of the current screen's density.

tvdpi

Resources for screens somewhere between mdpi and hdpi;

approximately 213dpi. This is not considered a "primary"

density group.

Orientation

land Resources for screens in the landscape orientation (wide

aspect ratio).

port Resources for screens in the portrait orientation (tall aspect

ratio).

Aspect ratio

long

Resources for screens that have a significantly taller or wider

aspect ratio (when in portrait or landscape orientation,

respectively) than the baseline screen configuration.

notlong Resources for use screens that have an aspect ratio that is

similar to the baseline screen configuration.

Page 5: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 5

Components of an Android Application

Activities: Activities are pieces of executable code that come and go in time, instantiated by either the user or the

operating system and running as long as they are needed. They can interact with the user and request data or

services from other activities or services via queries or Intents.

Services: They are executable pieces of code that usually run in the background from the time of their instantiation

until the mobile handset is shut down. They generally don’t expose a user interface.

Broadcast and Intent Receivers: These respond to requests for service from another application. A Broadcast

Receiver Broadcast Receiver responds to a system-wide announcement of an event. These announcements can come

from Android itself (e.g., battery low) or from any program running on the system. An Activity or Service provides

other applications with access to its functionality by executing an Intent Receiver, a small piece of executable code

that responds to requests for data or services from other activities.

Content Providers: These are created to share data with other activities or services. A content provider uses a

standard interface in the form of a URI to fulfil requests for data from other applications that may not even know

which content provider they are using.

Android Activity Lifecycle

The mechanisms are evident in the Android Activity Lifecycle, which defines the states or events that an activity goes

through from the time it is created until it finishes running.

To understand these concepts, you should Google about:

onCreate, onStart, onResume, onPause, onStop and onDestroy

It is important to take advantage of these methods to provide the best user experience possible.

Page 6: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 6

Hello, World

As a developer, you know that the first impression of a development framework is how easy it is to write "Hello,

World." Well, on Android, it's pretty easy. It's particularly easy if you're using Eclipse as your IDE, because we've

provided a great plugin that handles your project creation and management to greatly speed-up your development

cycles.

Install a Platform

To run the Hello World application, you need to install at least one Android platform in your SDK environment. If you

have not already performed this step, you need to do it now. To install a platform in Eclipse:

1. In the Android SDK and AVD Manager, choose Available Packages in the left panel.

2. Click the repository site checkbox to display the components available for installation.

3. Select at least one platform to install, and click Install Selected. If you aren't sure which platform to install, use the

latest version.

Create an AVD

In this tutorial, you will run your application in the Android Emulator. Before you can launch the emulator, you must

create an Android Virtual Device (AVD). An AVD defines the system image and device settings used by the emulator.

To create an AVD:

1. In Eclipse, choose Window > Android SDK and AVD Manager.

2. Select Virtual Devices in the left panel.

3. Click New. The Create New AVD dialog appears.

4. Type the name of the AVD, such as "my_avd".

5. Choose a target. The target is the platform (that is, the version of the Android SDK, such as 2.1) you want to run on

the emulator.

You can ignore the rest of the fields for now.

6. Click Create AVD.

Create a New Android Project

After you've created an AVD, the next step is to start a new Android project in Eclipse.

1. From Eclipse, select File > New > Project.

If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled

"Android" which should contain "Android Project". (After you create one or more Android projects, an entry for

"Android XML File" will also be available.)

2. Select "Android Project" and click Next.

Page 7: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 7

3. Fill in the project details with the following values:

o Project name: HelloAndroid

o Application name: Hello, Android

o Package name: com.example.helloandroid (or your

own private namespace)

o Create Activity: HelloAndroid

Click Finish.

Here is a description of each field:

Project Name

This is the Eclipse Project name — the name of the

directory that will contain the project files.

Application Name

This is the human-readable title for your application — the

name that will appear on the Android device.

Package Name

This is the package namespace (following the same rules

as for packages in the Java programming language) that

you want all your source code to reside under. This also

sets the package name under which the stub Activity will

be generated.

Your package name must be unique across all packages

installed on the Android system; for this reason, it's

important to use a standard domain-style package for your

applications. The example above uses the "com.example"

namespace, which is a namespace reserved for example

documentation — when you develop your own

applications, you should use a namespace that's

appropriate to your organization or entity.

Create Activity

This is the name for the class stub that will be generated by the plugin. This will be a subclass of

Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it

Page 8: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 8

chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always

used as the basis for an application.

Min SDK Version

This value specifies the minimum API Level required by your application.

Other fields: The checkbox for "Use default location" allows you to change the location on disk where the project's files

will be generated and stored. "Build Target" is the platform target that your application will be compiled against (this

should be selected automatically, based on your Min SDK Version).

Notice that the "Build Target" you've selected uses the Android 1.1 platform. This means that your application will be

compiled against the Android 1.1 platform library. If you recall, the AVD created above runs on the Android 1.5

platform. These don't have to match; Android applications are forward-compatible, so an application built against the

1.1 platform library will run normally on the 1.5 platform. The reverse is not true.

Your Android project is now ready. It should be visible in the Package Explorer on the left. Open

the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid). It should look like this:

package com.example.helloandroid;

import android.app.Activity;

import android.os.Bundle;

public class HelloAndroid extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

Notice that the class is based on the Activity class. An Activity is a single application entity that is used to perform

actions. An application may have many separate activities, but the user interacts with them one at a time.

The onCreate() method will be called by the Android system when your Activity starts — it is where you should

perform all initialization and UI setup. An activity is not required to have a user interface, but usually will.

Now let's modify some code!

Construct the UI

Take a look at the revised code below and then make the same changes to your HelloAndroid class. The bold items

are lines that have been added.

package com.example.helloandroid;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class HelloAndroid extends Activity {

Page 9: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 9

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

tv.setText("Hello, Android");

setContentView(tv);

}

}

An Android user interface is composed of hierarchies of objects called Views. A View is a drawable object used as an

element in your UI layout, such as a button, image, or (in this case) a text label. Each of these objects is a subclass of

the View class and the subclass that handles text is TextView.

In this change, you create a TextView with the class constructor, which accepts an Android Context instance as its

parameter. A Context is a handle to the system; it provides services like resolving resources, obtaining access to

databases and preferences, and so on. The Activity class inherits from Context, and because your HelloAndroid class

is a subclass of Activity, it is also a Context. So, you can pass this as your Context reference to the TextView.

Next, you define the text content with setText(). Finally, you pass the TextView to setContentView() in order to

display it as the content for the Activity UI. If your Activity doesn't call this method, then no UI is present and the

system will display a blank screen. There it is — "Hello, World" in Android! The next step, of course, is to see it

running.

Run the Application

The Eclipse plugin makes it easy to run your applications:

1. Select Run > Run.

2. Select "Android Application".

The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android

Emulator. Depending on your environment, the Android emulator might take

several minutes to boot fully, so please be patient. You should now see

something like this:

The "Hello, Android" you see in the grey bar is actually the application title. The

Eclipse plugin creates this automatically (the string is defined in

the res/values/strings.xml file and referenced by

your AndroidManifest.xml file). The text below the title is the actual text that

you have created in the TextView object.

That concludes the basic "Hello World" tutorial, but you should continue reading

for some more valuable information about developing Android applications.

Page 10: Android workshop handout

Questions? Visit www.iwillstudy.com/group/android/ for 24x7 Free Support Page 10

NOTES

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________

________________________________________