Upload
dhrubajyoti-bhattacharjee
View
222
Download
0
Embed Size (px)
Citation preview
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 smartphone’s 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.
Low-Level Hardware, Manufacturer Device Drivers
Device Operating System Base, Kernel
OS Libraries
Applications
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
Hardware
Symbian OS Base- Kernel
Symbian OS Libraries
Servers
Application EnginesKVM
Embedded Linux OS
• It is a Real-Time Operating System (RTOS). It meets deadlines and switch context
• It has relatively a small footprint. Today, mobile phones can ship with a small memory. Thus, OS must not seek to occupy a large amount of available storage. It should have a small foot print. Theoretically, they deploy in a footprint of 1MB or less.
• It is open source. It has no cost for licensing.
• Examples: Motorola Mobile phones such as RAZR V8, RAZR V9, A1200 are based on MontaVista Linux.
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 it’s own Java Virtual Machine• Each App is assigned a unique Linux user ID• Apps can share the same user ID to see each other’s 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 Eclipse1. 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://dl-ssl.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 SDKThe 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 FilesRoot Directory : It is the home, or repository, for all of project files.
Manifest: XML file where global settings are made.
Library: A list of the Referenced Libraries. android.jar, the Android SDK
Res: project resources are held.
Drawable-***: contains actual image files that your application can use andreference.
Layout: Holds main.xml, that is referenced by the application when building its interface..
Assets: to hold raw asset files, audio files for streaming and animation assets
Src: contains all the source files for your project
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. */@Overridepublic 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/androidandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent" >
<TextViewandroid: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/androidandroid: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. */@Overridepublic 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 : single row or column• RelativeLayout : relative to other Views• TableLayout : rows and columns• AbsoluteLayout : < 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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element One"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element Two"/><TextViewandroid: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">
<TextViewandroid: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 tobuild 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">
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element One"/>
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element Two"android:layout_x="30px"android:layout_y="30px"/>
<TextViewandroid: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.
Table Layout - Example<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRow>
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element One A"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element One B"/>
</TableRow><TableRow>
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element Two A"/>
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Element Two B"/>
</TableRow></TableLayout>
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" >
<RadioButtonandroid:text="Radio 1"android:id="@+id/radio1"/>
<RadioButtonandroid:text="Radio 2"android:id="@+id/radio2" /></RadioGroup>
Spinner Control<Spinner android:id="@+id/testSpinner"android:layout_width="fill_parent"android:layout_height="wrap_content"/>
public class Helloworld extends Activity{ boolean value;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main ); final Spinner spinner = (Spinner) findViewById(R.id.testSpinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Months); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); } static final String[] Months = new String[] { "January","February","March","April","May","June","July","August", "September","October","November","December" };}
Menupublic class Helloworld extends Activity{boolean value;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main ); }
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, 0,0, "AutoComplete"); menu.add(0, 1,0, "Button"); menu.add(0, 2,0, "CheckBox"); menu.add(0, 3,0, "EditText"); menu.add(0, 4,0, "RadioGroup"); menu.add(0, 5,0, "Spinner");
return true; } }
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 Android’s 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 Message
CALL_FORWARDING_STATE_CHANGED_ACTION The phone’s call forwarding state has changed
CAMERA_BUTTON_ACTION The camera button has been pressed
DATA_ACTIVITY_STATE_CHANGED_ACTION The device’s data activity state has changed
DATA_CONNECTION_STATE_CHANGED_ACTION There has been a change in the data connection state
DATE_CHANGED_ACTION The phone’s system date has changed
FOTA_CANCEL_ACTION Cancel pending system update downloads
FOTA_INSTALL_ACTION An update has been downloaded and must be installed immediately (sent by the system)
MEDIA_BAD_REMOVAL_ACTION Sent when an SD Memory Card was removed but unsuccessfully unmounted from the system
MESSAGE_WAITING_STATE_CHANGED The “message waiting” state on the phone has changed
PACKAGE_ADDED_ACTION Sent when a new package has been installed on the device
SCREEN_OFF_ACTION The screen has been shut off (sent by the device)
SIGNAL_STRENGTH_CHANGED_ACTION The signal strength has changed
SIM_STATE_CHANGED_ACTION The state of the SIM card has changed
TIME_TICK_ACTION The current time has changed
WALLPAPER_CHANGED_ACTION The device’s wallpaper has been changed
Activity Intents
Activity Intent Message
ADD_SHORTCUT_ACTION Add a function shortcut to the Android Home Screen
ALL_APPS_ACTION List all the applications available on the device
BUG_REPORT_ACTION Open the Bug Reporting Activity
CALL_ACTION Answer an incoming call
DELETE_ACTION Delete the specified data
DIAL_ACTION Open the Dial Activity and dial the specified number
EDIT_ACTION Provide editable access to the supplied data
EMERGENCY_DIAL_ACTION Dial an emergency number
FACTORY_TEST_ACTION Retrieve factory test settings
GET_CONTENT_ACTION Select and return specified data
INSERT_ACTION Insert an empty item
MAIN_ACTION Establish the Activity start point
PICK_ACTION Pick an item and return the selection
PICK_ACTIVITY_ACTION Pick a given Activity (returns a class)
RUN_ACTION Execute the given data
Example - Using the Dialerpackage android_programmers_guide.AndroidPhoneDialer;
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.net.Uri;
public class AndroidPhoneDialer extends Activity {
@Overridepublic void onCreate(Bundle icicle) {
super.onCreate(icicle);setContentView(R.layout.main);
Intent DialIntent = newIntent(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 Numberimport 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 ● INTERNAL_SYSTEM_WINDOW● ACCESS_CELL_ID ● RAISED_THREAD_PRIORITY● ACCESS_GPS ● READ_CONTACTS● ACCESS_LOCATION ● READ_FRAME_BUFFER● ACCESS_SURFACE_FLINGER ● RECEIVE_BOOT_COMPLETED● ADD_SYSTEM_SERVICE ● RECEIVE_SMS● BROADCAST_PACKAGE_REMOVED ● RECEIVE_WAP_PUSH● BROADCAST_STICKY ● RUN_INSTRUMENTATION● CALL_PHONE ● SET_ACTIVITY_WATCHER● CHANGE_COMPONENT_ENABLED_STATE ● SIGNAL_PERSISTENT_PROCESSES● SET_PREFERRED_APPLICATIONS ● 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.
The state of each Activity is determined by its position on the Activity stack, a last-in–first-out collection of all the currently running Activities.
When a new Activity starts, the current foreground screen ismoved to the top of the stack.
If the user navigates back using the Back button, or the foreground Activity is closed, the next Activityon the stack moves upand 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
Activity states
State Changes
What makes an Android ApplicationAndroid Application consists of loosely coupled components
bound using a project Manifest.Building blocks of Android Application are:
1. Activities2. Services3. Content Providers4. Intents5. Broadcast Receivers6. Notifications
What makes an Android ApplicationActivities:• 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 aren’t visible.
What makes an Android ApplicationContent 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 ApplicationBroadcast 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.