Why Android? ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android Dr Dimitris C. Dracopoulos A truly open, free development platform based on Linux and open source A component-based architecture Tons of built-in services out of the box (GPS, SQL, browser and map views) Automatic management of the application life cycle High-quality graphics and sound 3D-accelerated OpenGL graphics various codecs for audio and video formats built-in Portability across a wide range of current and future hardware (apps in Java) 2 / 28 Architecture 3 / 28 1 2
Native Libraries Surface Manager: drawing commands go into off-screen bitmaps that are then combined with other bitmaps to form the display the user sees. 2D and 3D graphics: Two- and three-dimensional elements can be combined in a single user interface with Android. The library will use 3D hardware if the device has it or a fast software renderer if it doesn t. Media codecs: Android can play video and record and play back audio in a variety of formats including AAC, AVC (H.264), H.263, MP3, and MPEG-4. SQL database: Android includes the lightweight SQLite database engine, the same database used in Firefox and the Apple iphone. Browser engine: For the fast display of HTML content, Android uses the WebKit library. Same engine used in the Google Chrome browser, Apple s Safari browser, the Apple iphone, and Nokia s S60 platform. 4 / 28 Application Framework Provides the high-level building blocks you will use to create your applications. Activity Manager Controls the life cycle of applications Maintains a common backstack for user navigation Content providers: Objects encapsulate data that needs to be shared between applications (e.g. contacts) Resource manager: Resources are anything that goes with your program that is not code Location manager: An Android phone always knows where it is Notification manager: Events such as arriving messages, appointments, proximity alerts, alien invasions, and more can be presented to the user 6 / 28 Android Runtime Core Java Libraries (different than Java SE but there is a substantial amount of overlap) Dalvik: Java virtual machine optimized for low memory requirements runs.dex files, which are converted at compile time from standard.class and.jar files..dex files are more compact and efficient than class files, an important consideration for the limited memory and battery-powered devices that Android targets. 5 / 28 Applications and Widgets End users will see only these programs. Applications: Programs that can take over the whole screen and interact with the user Widgets: Operate only in a small rectangle of the Home screen application Android phones preprepackaged with a number of standard system applications, including: Phone dialer Email Contacts Web browser Android Market 7 / 28 3 4
It s Alive! Standard Linux or Windows desktop, you can have many applications running and visible at once in different windows. One of the windows has keyboard focus, but otherwise all the programs are equal. Android doesnt work that way: There is one foreground application, which typically takes over the whole display except for the status line. When the user turns on their phone, the first application they see is the Home application. When the user runs an application, Android starts it and brings it to the foreground. From that application, the user might invoke another application, or another screen in the same application, and then another and another. All these programs and screens are recorded on the application stack by the system s Activity Manager. At any time, the user can press the Back button to return to the previous screen on the stack. Building Blocks of an Application Activities: A user interface screen. Applications can define one or more activities to handle different phases of the program.each activity is responsible for saving its own state so that it can be restored later as part of the application life cycle. Intents: A mechanism for describing a specific action, such as pick a photo, or phone home. Example: there is an intent for send an email.if youre writing a new email application, you can register an activity to handle that intent and replace the standard mail program.the next time somebody tries to send an email, theyll get the option to use your program instead of the standard one. 10 / 28 8 / 28 Building Blocks of an Application (cont d) Process!= Application Each user interface screen is represented by an Activity class. Each activity has its own life cycle. An application is one or more activities plus a Linux process to contain them. In Android, an application can be alive even if its process has been killed (the activity life cycle is not tied to the process life cycle). Processes are just disposable containers for activities. 9 / 28 Services: A task that runs in the background without the users direct interaction, similar to a Unix daemon. Example: a music player. The music may be started by an activity, but you want it to keep playing even when the user has moved on to a different program. Content Providers: a set of data wrapped up in a custom API to read and write it. This is the best way to share global data between applications. Example: Google provides a content provider for contacts. All the information there - names, addresses, phone numbers, and so forth, can be shared by any application that wants to use it. 11 / 28 5 6
Using Resources A resource is a localized text string, bitmap, or other small piece of noncode information that your program needs. At build time all your resources get compiled into your application. You create and store your resources in the res directory inside your project. The Android resource compiler (aapt) processes resources according to which subfolder they are in and the format of the file. For example: PNG and JPG format bitmaps should go in a directory starting with res/drawable XML files that describe screen layouts should go in a directory starting with res/layout 12 / 28 Safety and Security (cont d) Some of the most common permissions are: INTERNET: Access the Internet. READ CONTACTS: Read (but dont write) the user s contacts data. WRITE CONTACTS: Write (but dont read) the user s contacts data. RECEIVE SMS: Monitor incoming SMS (text) messages. ACCESS COARSE LOCATION: Use a coarse location provider such as cell towers or wifi. ACCESS FINE LOCATION: Use a more accurate location provider such as GPS. For example, to monitor incoming SMS messages, you would specify this in the manifest file: 14 / 28 Safety and Security Every application runs in its own Linux process. The hardware forbids one process from accessing another process s memory. Every application is assigned a specific user ID. Any files it creates cannot be read or written by other applications. In addition: Access to certain critical operations are restricted, and you must specifically ask for permission to use them in a file named Android-Manifest.xml. When the application is installed, the Package Manager either grants or doesn t grant the permissions based on certificates and, if necessary, user prompts. 13 / 28 Safety and Security (cont d) <manifest xmlns:android="http://schemas.android.com/ apk/res/android" package="com.google.android.app.myapp" > <uses-permission android:name= "android.permission.receive_sms" /> </manifest> 15 / 28 7 8
Installing the Android Software There are 5 different components to install (assuming you use Eclipse): Java JDK Eclipse (version Eclipse IDE for Java Developers) SDK Starter Package Adding Platforms and Other Components To do so run the android utility found in the tools directory of the Android SDK started package (see previous step) ADT Plugin for Eclipse Full instructions for all the above are given in: http://d.android.com/sdk/installing.html 16 / 28 Create a New Android Project 1. From Eclipse, select File New Project. 2. Select Android Project and click Next. 3. Fill in the project details with the following values: Project name: HelloAndroid Application name: Hello, Android Package name: com.example.helloandroid (or your own private namespace) Create Activity: HelloAndroid 4. Click Finish. 18 / 28 Create an AVD Before running an application in the Android emulator you need to create an Android Virtual Device (AVD). 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.3) you want to run on the emulator. You can ignore the rest of the fields for now. 6. Click Create AVD. 17 / 28 Writing a Hello Android After the project is created, Eclipse generates the following Java code found in file HelloAndroid.java (name of the activity). Open that with the Package Explorer. The file is located under HelloAndroid src com.example.helloandroid). 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); 19 / 28 9 10
Writing a Hello Android (cont d) Modify the code to look like the following: Running the Hello Android Program Select Run Run. package com.example.helloandroid; import android.app.activity; import android.os.bundle; import android.widget.textview; public class HelloAndroid extends Activity { /** 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); 20 / 28 The Android Emulator Home Page 22 / 28 Programmatic vs Declarative Design Android offers two ways to create use interface design: Programmatic: using Java classes Declarative: using XML-based layout files. 23 / 28 21 / 28 11 12
Declarative Approach for the Hello World Program Open the main.xml file under the res layout directory: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/ apk/res/android" android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/hello"/> 24 / 28 Declarative Approach for the Hello World Program (cont d) Now modify the HelloAndroid.java class to look like the following will display the string Hello, Android! I am a string resource!: 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); 26 / 28 Declarative Approach for the Hello World Program (cont d) Modify the file res/values/strings.xml to look like the following: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello, Android! I am a string resource!</string> <string name="app_name">hello, Android</string> </resources> 25 / 28 The R Class File R.java is autogenerated and contains an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you ve included in your project. You should never edit this file by hand. 27 / 28 13 14
The R Class (cont d) package com.example.helloandroid; public final class R { public static final class attr { public static final class drawable { public static final int icon=0x7f020000; public static final class id { public static final int textview=0x7f050000; public static final class layout { public static final int main=0x7f030000; public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; 28 / 28 15