introducing android - chapter 1
DESCRIPTION
Nowadays good User Interface is very essential for the success of any application in this competitive market.There are a lot of Android books on the market, but most of them are aimed at professional users and non-zero, there are few books on the market that deals in depth about Android and sometimes puts the user in total confusion. The purpose of this book is to teach the user how to create user interfaces with XML which is much easier than Java and can achieve similar results.TRANSCRIPT
1
Chapter 1: Introducing Android
What is Android?
Android is a software stack for mobile devices that includes an operating system,
middleware and key applications. The Android SDK provides the tools and APIs necessary to
begin developing applications on the Android platform using the Java programming
language.
Android is officially guided by the Open Handset Alliance but in reality Google leads the project and it’s first truly open and comprehensive platform for mobile devices
Android relies on Linux version 2.6 for core system services such as security, memory
management, process management, network stack, and driver model. The kernel also acts
as an abstraction layer between the hardware and the rest of the software stack.
Android Sdk Features
• Application Application Application Application frameworkframeworkframeworkframework enabling reuse and replacement of components
• Dalvik virtual machineDalvik virtual machineDalvik virtual machineDalvik virtual machine optimized for mobile devices
• Integrated browserIntegrated browserIntegrated browserIntegrated browser based on the open source WebKit engine
• Optimized graphicsOptimized graphicsOptimized graphicsOptimized graphics powered by a custom 2D graphics library; 3D graphics based on
the OpenGL ES 1.0 specification (hardware acceleration optional)
• SQLiteSQLiteSQLiteSQLite for structured data storage
• Media supportMedia supportMedia supportMedia support for common audio, video, and still image formats (MPEG4, H.264,
MP3, AAC, AMR, JPG, PNG, GIF)
• GSM TelephonyGSM TelephonyGSM TelephonyGSM Telephony (hardware dependent)
• Bluetooth, EDGE, 3G, aBluetooth, EDGE, 3G, aBluetooth, EDGE, 3G, aBluetooth, EDGE, 3G, and Wind Wind Wind Wi----FiFiFiFi (hardware dependent)
• Camera, GPS, compass, and accelerometerCamera, GPS, compass, and accelerometerCamera, GPS, compass, and accelerometerCamera, GPS, compass, and accelerometer (hardware dependent)
Rich development environmentRich development environmentRich development environmentRich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE
2
Android Application Architecture
The following diagram shows the major components of the Android operating system. Each
section is described in more detail below.
Application components
Application components are the essential building blocks of an Android application. Each
component is a different point through which the system can enter your application. Not all
components are actual entry points for the user and some depend on each other, but each
one exists as its own entity and plays a specific role each one is a unique building block that
helps define your application's overall behavior.
There are four different types of application components. Each type serves a distinct
purpose and has a distinct lifecycle that defines how the component is created and
destroyed.
Here are the four types of application components:
3
Activities
An activity represents a single screen with a user interface. For example, an email
application might have one activity that shows a list of new emails, another activity to
compose an email, and another activity for reading emails. Although the activities work
together to form a cohesive user experience in the email application, each one is
independent of the others. As such, a different application can start any one of these
activities (if the email application allows it). For example, a camera application can start the
activity in the email application that composes new mail, in order for the user to share a
picture.
Services
A service is a component that runs in the background to perform long-running operations or
to perform work for remote processes. A service does not provide a user interface. For
example, a service might play music in the background while the user is in a different
application, or it might fetch data over the network without blocking user interaction with an
activity. Another component, such as an activity, can start the service and let it run or bind to
it in order to interact with it.
Content providers
A content provider manages a shared set of application data. You can store the data in the
file system, a SQLite database, on the web, or any other persistent storage location your
application can access. Through the content provider, other applications can query or even
modify the data (if the content provider allows it). For example, the Android system provides
a content provider that manages the user's contact information. As such, any application
with the proper permissions can query part of the content provider (such as
ContactsContract.Data) to read and write information about a particular person.
Content providers are also useful for reading and writing data that is private to your
application and not shared
Broadcast receivers
A broadcast receiver is a component that responds to system-wide broadcast
announcements. Many broadcasts originate from the system—for example, a broadcast
announcing that the screen has turned off, the battery is low, or a picture was captured.
Applications can also initiate broadcasts, for example, to let other applications know that
some data has been downloaded to the device and is available for them to use. Although
broadcast receivers don't display a user interface, they may create a status bar notification
to alert the user when a broadcast event occurs. More commonly, though, a broadcast
receiver is just a "gateway" to other components and is intended to do a very minimal
amount of work. For instance, it might initiate a service to perform some work based on the
event.
4
A unique aspect of the Android system design is that any application can start another
application’s component. For example, if you want the user to capture a photo with the
device camera, there's probably another application that does that and your application can
use it, instead of developing an activity to capture a photo yourself. You don't need to
incorporate or even link to the code from the camera application. Instead, you can simply
start the activity in the camera application that captures a photo. When complete, the photo
is even returned to your application so you can use it. To the user, it seems as if the camera
is actually a part of your application.
When the system starts a component, it starts the process for that application (if it's not
already running) and instantiates the classes needed for the component. For example, if
your application starts the activity in the camera application that captures a photo, that
activity runs in the process that belongs to the camera application, not in your application's
process. Therefore, unlike applications on most other systems, Android applications don't
have a single entry point (there's no main() function, for example).
Because the system runs each application in a separate process with file permissions that
restrict access to other applications, your application cannot directly activate a component
from another application. The Android system, however, can. So, to activate a component in
another application, you must deliver a message to the system that specifies your intent to
start a particular component. The system then activates the component for you.
5
Installing Android
The development environment used for the sample applications in this book includes:
� Microsoft Windows XP/Vista/7
� Java Development Kit (JDK) version 6
� The Android SDK available for download at the google developer website:
http://developer.android.com/sdk/index.html
� Eclipse IDE (Integrated Development Environment) version 3.7
� ADT plugin for Eclipse
Then create a directory ““““My FolderMy FolderMy FolderMy Folder””””, extract the content of the Eclipse file (Zip) and copy the JDK and the Android SDK installer.
Supported Operating SystemsSupported Operating SystemsSupported Operating SystemsSupported Operating Systems
Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)
Mac OS X 10.5.8 or later (x86 only)
Linux (tested on Ubuntu Linux, Lucid Lynx) ◦GNU C Library (glibc) 2.7 or later is required.
On Ubuntu Linux, version 8.04 or later is required.
64-bit distributions must be capable of running 32-bit applications.
6
Installing the JDK
1- Double click the jdkjdkjdkjdk----6u266u266u266u26----windowswindowswindowswindows----x64x64x64x64 to launch the Installation Wizard
2- Click NextNextNextNext
3- Click NextNextNextNext to choose your installation folder
7
4- Click NextNextNextNext to begin the installation
5- Once the progression ended, the Product Registration window appears
8
6- Click FinishFinishFinishFinish
9
Installing the SDK
1- Double click installer_r16installer_r16installer_r16installer_r16----windowswindowswindowswindows,,,, the Welcome to the Android SDK Tools window
appears
2- Click NextNextNextNext
3- Click NextNextNextNext and choose the Install Location
10
4- Click NextNextNextNext
5- Click InstallInstallInstallInstall
11
6- Then uncheck “Start SDK Manager”“Start SDK Manager”“Start SDK Manager”“Start SDK Manager” cause you will install it later and click FinishFinishFinishFinish
Note:Note:Note:Note: You can choose not to install the SDK Manager, you can do it later in the book
during the installation of platforms and other components
12
Installing Eclipse
To install Eclipse you must first download and extract the file in “My Folder”, then create a shortcut of the Ecplise executable on the desktop
Installing the ADT Plugin for Eclipse
1- Double click on the EclipseEclipseEclipseEclipse icon to start Eclipse for the first time
The Workspace Launcher window appears, and allows you to specify a workspace directory; You can use the default Workspace.
2- Click OKOKOKOK, then the Eclipse welcome screen appears
13
3- select Help > Install New SoftwareHelp > Install New SoftwareHelp > Install New SoftwareHelp > Install New Software
4- Click AddAddAddAdd, in the top-right corner.
14
5- In the Add Repository dialog that appears, enter "ADT Plugin""ADT Plugin""ADT Plugin""ADT Plugin" for the Name and
“https://dl-ssl.google.com/android/eclipse/ ” for the Location:
6- Click OKOKOKOK
15
7- In the Available Software dialog, select the checkbox next to Developer Tools and
click NextNextNextNext
16
8- In the next window, you will see a list of items to be downloaded. Click NextNextNextNext.
17
9- Read and accept the license agreements, then click FinishFinishFinishFinish.
Note:Note:Note:Note: If you get a security warning saying that the authenticity or validity of the software can't be established, click OKOKOKOK.
10- A new window appears which shows the progress of the installation
11- When the installation completes, Restart Eclipse.
18
Adding platforms and other components
Now you have finished with the installation, add platforms and other components.
1- Select HELP > Install New Software…HELP > Install New Software…HELP > Install New Software…HELP > Install New Software…
2- A new Window appears allowing you to select the platforms and components to
install
19
Configuring the ADT Plugin
Now you must modify the ADT preferences in Eclipse to point to the Android SDK directory:
1- Select WindowWindowWindowWindow > Preferences...Preferences...Preferences...Preferences... to open the Preferences panel
20
2- Select AndrAndrAndrAndroid oid oid oid from the left panel.
3- For the SDK Location in the main panel, click Browse...Browse...Browse...Browse... and locate your downloaded
SDK directory.
4- Click ApplyApplyApplyApply, then click OKOKOKOK
21
Create our first project
1- Double click on the eclipse icon
2- Choose File > New >File > New >File > New >File > New > OtherOtherOtherOther to create a new project
3- Select Android ProjectAndroid ProjectAndroid ProjectAndroid Project and click Next
22
4- In Project Name enter Hello AndroidHello AndroidHello AndroidHello Android
23
5- Select the Build Target Build Target Build Target Build Target (Android 2.2)
6- In Package Name Package Name Package Name Package Name enter “com.UInterface.book.helloandroid” and “8” in Minimum Minimum Minimum Minimum
SDKSDKSDKSDK
24
7- Click FinishFinishFinishFinish button
Exploring your first android project files in the Package Explorer
HelloAndroidActivity.java 1
Double click in HelloAndroidActivity.java and you will only see java code
package com.UInterface.book.helloandroid; import android.app.Activity;
import android.os.Bundle; public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main); }
}
Resources folder: Contains all the resources used by your Android application such as drawable files, layout
files, and string values 2222
25
Layout folder Res:
Contains an XML file used to represent the user interface of your Android application 3333
Main.xml 4
This file allow you to create the GUI of your application
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/hello" /> </LinearLayout> Values folder: For XML files that are compiled into many kinds of resource, resources written to XML files
in this folder are not referenced by the file name. 5555
Strings Res:
Used to display text in your application 6666 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="hello">Hello World, HelloAndroidActivity!</string> <string name="app_name">Hello Android</string>
</resources>
AndroidManifest.xml 7
The control file that describes the nature of the application and each of its components, for instance, it describes certain qualities about the activities, services, intent receivers, and content providers; what permissions are requested; what external libraries are needed; what device features are required, what API Levels are supported or required; and others. <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androui.hello" android:versionCode="1"
android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
26
<activity android:name=".HelloAndroidActivity" android:label="@string/app_name">
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </activity>
</application> </manifest> Gradually as you move forward in this book you will study in detail these various files. Now compile your first application, above all create your first emulator.
Creating an android virtual device
The Menu Bar of the Eclipse software looks like the following:
You can create as many AVDs as you would like to test on. It is recommended that you test your applications on all API levels higher than the target API level for your application.
1- In the eclipse menu select window window window window > AVD Manager,AVD Manager,AVD Manager,AVD Manager, or click AVD Manager icon in the
Eclipse toolbar
27
2- In the Android Virtual Devices Manager. Click NewNewNewNew to create a new AVD. The Create Create Create Create
New AVDNew AVDNew AVDNew AVD dialog appears.
Fill in the details for the AVD.
NameNameNameName = YourAVD, platform targetplatform targetplatform targetplatform target = Android 2.2-API Level8, SD card sizeSD card sizeSD card sizeSD card size, and a skinskinskinskin
(HVGA is default).
3- Click Create AVDCreate AVDCreate AVDCreate AVD.
28
Running an Android application
1- To run (or debug) your application, select RunRunRunRun > RunRunRunRun (or RunRunRunRun > DebugDebugDebugDebug) from the
Eclipse menu bar, or right click the project in the package explorer Run AsRun AsRun AsRun As > Android Android Android Android
ApplicationApplicationApplicationApplication
29
2- Click and drag the padlock to the right
3- Result should look like this: