Você está na página 1de 66

Android

Topics
Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Smart Phones
Smartphones combine the mobile phone with another stream of technology - the computer, which adds the smart in smartphone. MainFrame -> PC -> Laptops -> PDA -> Smartphone Like PDAs, smartphones can run applications such as organizers, games,and communications programs (e.g. email, browser). The smartphones goal, however, is not just to limit the number of devices you carry, but also to combine mobile phone and computing technologies in a synergistic way.

Mobile OS Structure
A mobile OS is a software platform on top of which other programs called application programs, can run on mobile devices such as PDA, cellular phones, smartphone and etc.
Applications
OS Libraries

Device Operating System Base, Kernel


Low-Level Hardware, Manufacturer Device Drivers

Mobile OS Platforms
There are many mobile operating systems. The followings demonstrate the most important ones: Java ME Platform Palm OS Symbian OS Linux OS Windows Mobile OS BlackBerry OS iPhone OS Google Android Platform

Symbian OS Structure

Symbian OS Libraries
Application Engines

KVM

Servers
Symbian OS Base- Kernel Hardware

Windows Mobile OS
Windows Mobile is a compact operating system designed for mobile devices and based on Microsoft Win32. It is run on Pocket PCs, Smartphones and Portable media centers. It provides ultimate interoperability. Users with various requirements are able to manipulate their data.

iPhone OS
iPhone OS is an operating system run on iPhone and iPod.

It is based on Mach Kernel and Drawin core as Mac OS X.


The Mac OS X kernel includes the following component: Mach Kernel BSD I/O component File Systems Networking components

Topics
Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Background
Software platform from Google and the Open Handset Alliance July 2005, Google acquired Android, Inc. November 2007, Open Handset Alliance formed to develop open standards for mobile devices October 2008, Android available as open source December 2008, 14 new members joined Android project April 30, 2009: Official 1.5 Cupcake release September 15, 2009: 1.6 SDK Donut release October 26, 2009: 2.0 SDK clair release Updates to the clair release: 2.0.1 on December 3, 2009 2.1 on January 12, 2010

Features
Reuse and replacement of components. Dalvik virtual machine. Integrated browser. Optimized graphics. SQLite. Media support. GSM Telephony. Bluetooth, EDGE, 3G, and WiFi. Camera, GPS, compass, and accelerometer. Rich development environment.

Application Fundamentals
Apps are written in Java Bundled by Android Asset Packaging Tool Every App runs its own Linux process Each process has its own Java Virtual Machine Each App is assigned a unique Linux user ID Apps can share the same user ID to see each others files

Android Architecture

Android Architecture
Linux Kernel: Android relies on Linux for core system services such as security, memory management, process management. Android Runtime: it provides a set of core libraries which supports most of the functionality in the core libraries of Java. The Android Virtual Machine known as Dalvik VM relies on the linux kernel for some underlying functionality such as threading, Libraries: Android includes a set of C/C++ libraries. These libraries are exposed to developers through the Android application framework. They include media libraries, system C libraries, surface manager, 3D libraries, SQLite and etc. Application Framework: it provides an access layer to the framework APIs used by the core applications. It allows components to be used by the developers.

Challenges
CPU typically runs 500-600 Mhz RAM available to an App may only be a few megabytes Disk (flash) access is very slow Lifecycle - apps must pause/quit often, and restore to give the illusion that they are always running Typical screen may be HVGA (320x480) may be in portrait or landscape

Very high DPI - small text may not be readable


Touch resolution is very low (~25 pixel) Network access may be slow and (very) intermittent

Topics
Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Downloading and Installing Eclipse


Before downloading and installing Eclipse, make sure Java Runtime Environment (JRE) is downloaded and installed on the machine. Eclipse requires JRE to run Download JDK (includes JRE) from www.oracle.com/technetwork/java/javase/downloads/index.html Download Eclipse from www.eclipse.org/downloads/ Unzip the package to a folder. The editors executable is eclipse.exe.( There is no installation wizard) Create a shortcut for the eclipse.exe and copy it to the desktop

Topics
Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Downloading SDK
Download the Android SDK from
http://developer.android.com/sdk/index.html

Extract it and note down the location. Add the SDK path to the Environment variable.

Installing ADT Plug in for Eclipse


1. Start Eclipse, then select Help > Install New Software.... 2. Click Add, in the top-right corner.

3. In the Add Repository dialog that appears, enter "ADT Plugin" for theName and the following URL for the Location:https://dlssl.google.com/android/eclipse/.
4. Click OK. 5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next. 6. In the next window, you'll see a list of the tools to be downloaded. Click Next. 7. Read and accept the license agreements, then click Finish. 8. When the installation completes, restart Eclipse.

Configuring the ADT Plugin


1. Select Window > Preferences... to open the Preferences panel. 2. Select Android from the left panel. 3. For the SDK Location in the main panel, click Browse... and locate your downloaded SDK directory. 4. Click Apply, then OK.

Adding Components
1. From within Eclipse, select Window > Android SDK and AVD Manager.
2.

Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

What is in the Android SDK


The Android SDK consists of
1. Docs,
Contains all of the accompanying Android documentation.

2. Samples Contains six sample applications that you can compile and test from
within Eclipse.

3. Tools Contains all of the development, compilation, and debugging tools.

Tools in the Android SDK


The Android Emulator is used to run your applications in a pseudo-Android environment.

Android Debug Bridge or adb (adb.exe) allows you to issue commands to the Emulator.exe tool.
MKSDCARD.exe is a tool if you are testing an application that will need to read / write files on SD Memory Card. DX.exe is the compiler of the Android SDK. When run against Java files, will create files with .dex extensions -Dalvik executable format.

Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Creating an Android Project

Creating an Android Project

Name of the project file

Name of the Application Name of the Package

Name of the Activity

Examining the Android-Created Files


Root Directory : It is the home, or repository, for all of project files. Src: contains all the source files for your project Library: A list of the Referenced Libraries. android.jar, the Android SDK Assets: to hold raw asset files, audio files for streaming and animation assets Res: project resources are held.

Drawable-***: contains actual image files that your application can use and reference. Layout: Holds main.xml, that is referenced by the application when building its interface..

Manifest: XML file where global settings are made.

Hello world XML Based


package android_programmers_guide.HelloWorldText; import android.app.Activity; import android.os.Bundle; public class HelloWorldText extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); }

Hello world XML Based


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, HelloWorldText" /> </LinearLayout>

Hello world Code Based

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>

Hello world Code Based


package android_programmers_guide.HelloWorldText; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloWorldText extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { TextView HelloWorldTextView = new TextView(this); HelloWorldTextView.setText("Hello World!"); setContentView(HelloWorldTextView); } }

Hello world Using an Image


Example application that shows Hello world with an Image by

XML Layout based UI. Code based UI.

Topics
What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Layouts
Controls how Views are laid out LinearLayout RelativeLayout TableLayout AbsoluteLayout : single row or column : relative to other Views : rows and columns : < x, y > coordinates

Linear Layout
All elements are arranged in a descending column from top to bottom or left to right. Each element can have gravity and weight properties that denote how they dynamically grow and shrink to fill space. Elements arrange themselves in a row or column notation based on the android:orientation parameter.

Linear Layout - Example


All elements are arranged in a descending column from top to bottom or left to right. Each element can have gravity and weight properties that denote how they dynamically grow and shrink to fill space. Elements arrange themselves in a row or column notation based on the android:orientation parameter.

Linear Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element One" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Two" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Three" /> </LinearLayout>

Relative Layout

Each child element is laid out in relation to other child elements. Relationships can be established so that children will start themselves where a previous child ends. Children can relate only to elements that are listed before them. Note that IDs are required so that widgets can reference each other.

Relative Layout - Example


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/EL01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element One" /> <TextView android:id="@+id/EL02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Two" android:layout_below="@id/EL01" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Three" android:layout_toRight="@id/EL02" /> </RelativeLayout>

Absolute Layout

Each child must be given a specific location within the bounds of the parent layout object.

The Absolute Layout object is probably the easiest to build and visualize but the hardest to migrate to a new device or screen size.

Absolute Layout - Example


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element One"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Two" android:layout_x="30px" android:layout_y="30px"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Three" android:layout_x="50px" android:layout_y="50px/> </AbsoluteLayout>

Table Layout

Table Layout is a layout object that allows you to specify table rows. Android tries to arrange each of the child elements into the correct row and columns.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element One A"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element One B"/> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Two A"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Element Two B"/> </TableRow>

Table Layout - Example

Topics
What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

Labels
The simplest widget is the label, referred to in Android as a TextView. Bits of text not editable directly by users, used to identify adjacent widgets (e.g., a "Name:" label before a field where one fills in a name).

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="You were expecting something profound?" />

Button
<Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=Button />

import android.view.View; import android.widget.Button; public class Helloworld extends Activity implements View.OnClickListener { Button btn; btn = (Button)findViewById(R.id.ok); btn.setOnClickListener(this); } public void onClick(View view) { btn.setText("New Text"); }

Check Box
<CheckBox android:id="@+id/testCheckBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is the test CheckBox" />

import android.widget.CheckBox; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main ); CheckBox checkbox; checkbox = (CheckBox)findViewById(R.id.testCheckBox); checkbox.setOnClickListener(this); } public void onClick(View v) { }

Edit Text
<EditText android:id="@+id/testEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" />

import android.widget.EditText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main ); }

Radio Button

<RadioGroup android:id="@+id/testRadioGroup" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:text="Radio 1" android:id="@+id/radio1"/> <RadioButton android:text="Radio 2" android:id="@+id/radio2" /> </RadioGroup>

Topics
Smart phones What Is Android? Downloading and Installing Eclipse Downloading and Installing the Android SDK Exploring the Android SDK Application: Hello World! View Layouts Android Widgets Using Intents and the Phone Dialer

What are Intents?


An Intent is Androids method for relaying certain information from one Activity to another. You can think of an Intent as a message passed between Activities.

Intents are broken up into two main categories:


Activity Action Intents : Intents used to call Activities outside of your application. Only one Activity can handle the Intent. Broadcast Intents : Intents that are sent out for multiple Activities to handle. An example of a Broadcast Intent would be a message sent out by Android about the current battery level. Any Activity can process this Intent and react accordingly.

Broadcast Intents
Broadcast Intent
CALL_FORWARDING_STATE_CHANGED_ACTION CAMERA_BUTTON_ACTION

Message
The phones call forwarding state has changed The camera button has been pressed

DATA_ACTIVITY_STATE_CHANGED_ACTION DATA_CONNECTION_STATE_CHANGED_ACTION DATE_CHANGED_ACTION FOTA_CANCEL_ACTION FOTA_INSTALL_ACTION MEDIA_BAD_REMOVAL_ACTION MESSAGE_WAITING_STATE_CHANGED PACKAGE_ADDED_ACTION SCREEN_OFF_ACTION SIGNAL_STRENGTH_CHANGED_ACTION SIM_STATE_CHANGED_ACTION TIME_TICK_ACTION WALLPAPER_CHANGED_ACTION

The devices data activity state has changed There has been a change in the data connection state The phones system date has changed Cancel pending system update downloads An update has been downloaded and must be installed immediately (sent by the system) Sent when an SD Memory Card was removed but unsuccessfully unmounted from the system The message waiting state on the phone has changed Sent when a new package has been installed on the device The screen has been shut off (sent by the device) The signal strength has changed The state of the SIM card has changed The current time has changed The devices wallpaper has been changed

Activity Intents
Activity Intent
ADD_SHORTCUT_ACTION ALL_APPS_ACTION

Message
Add a function shortcut to the Android Home Screen List all the applications available on the device

BUG_REPORT_ACTION
CALL_ACTION DELETE_ACTION DIAL_ACTION EDIT_ACTION

Open the Bug Reporting Activity


Answer an incoming call Delete the specified data Open the Dial Activity and dial the specified number Provide editable access to the supplied data

EMERGENCY_DIAL_ACTION
FACTORY_TEST_ACTION GET_CONTENT_ACTION INSERT_ACTION MAIN_ACTION PICK_ACTION PICK_ACTIVITY_ACTION RUN_ACTION

Dial an emergency number


Retrieve factory test settings Select and return specified data Insert an empty item Establish the Activity start point Pick an item and return the selection Pick a given Activity (returns a class) Execute the given data

Example - Using the Dialer


package android_programmers_guide.AndroidPhoneDialer; import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.net.Uri;

public class AndroidPhoneDialer extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Intent DialIntent = new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));

/** Use NEW_TASK_LAUNCH to launch the Dialer Activity */ DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); /** Finally start the Activity */ startActivity(DialIntent);
} }

Example Calling a Number


import android.content.Intent; import android.os.Bundle; import android.net.Uri;

public class AndroidPhoneDialer extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Intent CallIntent = new Intent(Intent.CALL_ACTION,Uri.parse("tel:5551212")); /** Use NEW_TASK_LAUNCH to launch the Call Activity */ CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); /** Finally start the Activity */ startActivity(CallIntent); } }

Activity Permissions
Most Activity Action Intents fall into the category of requiring that the proper permission be set before Android will allow the action. Android just needs to make sure that only Activities with the correct credentials be allowed to perform actions with Activities that are outside of their base.

Permissions:
ACCESS_ASSISTED_GPS ACCESS_CELL_ID ACCESS_GPS ACCESS_LOCATION ACCESS_SURFACE_FLINGER ADD_SYSTEM_SERVICE BROADCAST_PACKAGE_REMOVED BROADCAST_STICKY CALL_PHONE CHANGE_COMPONENT_ENABLED_STATE SET_PREFERRED_APPLICATIONS INTERNAL_SYSTEM_WINDOW RAISED_THREAD_PRIORITY READ_CONTACTS READ_FRAME_BUFFER RECEIVE_BOOT_COMPLETED RECEIVE_SMS RECEIVE_WAP_PUSH RUN_INSTRUMENTATION SET_ACTIVITY_WATCHER SIGNAL_PERSISTENT_PROCESSES DELETE_PACKAGES

Application Lifecycle
Android Applications have no control over their own life cycle. Android aggressively manages its resource doing what ever it takes to ensure that the device remains responsive. This means that processes will be killed without warning if necessary to free resources for higher priority applications.

Activity states
The state of each Activity is determined by its position on the Activity stack, a last-infirst-out collection of all the currently running Activities. When a new Activity starts, the current foreground screen is moved to the top of the stack.

If the user navigates back using the Back button, or the foreground Activity is closed, the next Activity on the stack moves up and becomes active.

Activity states
State transitions are nondeterministic and are handled entirely by the Android memory manager.

Android will start by closing applications that contain inactive Activities, followed by those that are stopped, and in extreme cases, it will remove those that are paused. For seamless user experience, transitions between these states should be invisible to the user. It is important to save all UI state changes and persist all data when an Activity is paused or stopped. Once an Activity does become active, it should restore those saved values.
Active Paused Stopped Inactive

State Changes

What makes an Android Application


Android Application consists of loosely coupled components bound using a project Manifest. Building blocks of Android Application are: 1. 2. 3. 4. 5. 6. Activities Services Content Providers Intents Broadcast Receivers Notifications

What makes an Android Application


Activities: Applications presentation layer. Every screen in the application will be an extension of the Activity class. Activities use views to form GUI. Activities are equivalent to Forms in Desktop world. Services: These components run invisibly. They are used to perform regular processing that needs to continue even when the application Activities arent visible.

What makes an Android Application


Content Providers: A sharable database. Preferred way of sharing data across process boundaries. It is possible to configure a content provider to permit access from other applications. Use Content providers exposed by other applications to access their stored data. Intents: Message passing Framework. Broadcast message system wide or to a target activity or service.

What makes an Android Application


Broadcast consumers: Intent Broadcast consumers By creating and registering a Broadcast receiver, an application can listen for broadcast intents. Notifications: Notifies user without stealing focus or interrupting their current activists.

Você também pode gostar