Beruflich Dokumente
Kultur Dokumente
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 its 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.
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:
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 systemfor 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.
A unique aspect of the Android system design is that any application can start another applications 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.
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 Folder, extract the content of the Eclipse file (Zip) and copy the Folder JDK and the Android SDK installer.
Supported 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.
2- Click Next
6- Click Finish
2- Click Next
4- Click Next
5- Click Install
10
6- Then uncheck Start SDK Manager cause you will install it later and click Finish
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
11
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
The Workspace Launcher window appears, and allows you to specify a workspace directory; You can use the default Workspace.
12
13
5- In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and https://dl-ssl.google.com/android/eclipse/ for the Location:
6- Click OK
14
7- In the Available Software dialog, select the checkbox next to Developer Tools and click Next
15
Next. 8- In the next window, you will see a list of items to be downloaded. Click Next
16
9- Read and accept the license agreements, then click Finish. Finish Note: If you get a security warning saying that the authenticity or validity of the software can't be established, click OK OK. 10- A new window appears which shows the progress of the installation
17
2- A new Window appears allowing you to select the platforms and components to install
18
19
3- For the SDK Location in the main panel, click Browse... and locate your downloaded SDK directory. Apply 4- Click Apply, then click OK
20
21
22
23
7- Click Finish 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 2
24
Layout folder Res: Contains an XML file used to represent the user interface of your Android application 3 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. 5 Strings Res: Used to display text in your application
<?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">
25
<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.
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 > AVD Manager, or click AVD Manager icon in the Eclipse toolbar
26
2- In the Android Virtual Devices Manager. Click New to create a new AVD. The Create New AVD dialog appears.
Fill in the details for the AVD. size, Name = YourAVD, platform target = Android 2.2-API Level8, SD card size and a skin (HVGA is default).
27
28
29