Professional Documents
Culture Documents
Android Demo
Android Demo
Android Phones
Sony X10
• Developed by Google
• Software stack for mobile devices that includes an
operating system, middleware and key
applications.
• Open Source (but not too open)
• Reuse and Replacement of components
• Linux Kernel
Includes a Java API for developing applications
Modern os , controlled and governed by Google
What is Android?
Direct install vs. Marketplace vs. App Store
Open source?
Mobile Devices
Pr E A
D m Mi
oc
e be
dd p
es dd
le
vi wa p
so ed
ce re
r OS s
7
Brief History
• 2005
– Google acquires startup Android Inc. to start Android platform
– Work on Dalvik VM begins
• 2007
– Open Handset Alliance announced
– Early look at SDK
• 2008
– Google sponsors 1st Android Developer Challenge
– T-Mobile G1 announced
– SDK 1.0 released
– Android released open source (Apache License)
– Android Dev Phone 1 released
9
Brief History cont.
• 2011
– SDK 3.0 (Honeycomb) for tablets only
• New UI for tablets, support multi-core processors
– SDK 3.1 and 3.2
• Hardware support and UI improvements
– SDK 4.0 (Ice Cream Sandwich)
• For Q4, combination of Gingerbread and Honeycomb
10
Android Features #1
11
Android Features #2
21
Libraries
22
Core Libraries
• System C library, the standard C system library, tuned for embedded Linux-
based devices
• Media Libraries, support playback and recording of many popular audio
and video formats, as well as image files, including MPEG4, H.264, MP3,
AAC, AMR, JPG, and PNG
• Surface Manager, manages access to the display subsystem and seamlessly
composites 2D and 3D graphic layers from multiple applications
• WebKit, a modern web browser engine which powers both the Android
browser and an embeddable web view
• SGL, the underlying 2D graphics engine
• 3D libraries, an implementation based on OpenGL ES 1.0 APIs
• FreeType , bitmap and vector font rendering
• SQLite , a powerful and lightweight relational database engine
23
Andoid Runtime
24
Android Runtime (cont.)
25
Dalvik Virtual Machine
26
DVM vs. JVM
• DVM
– Google
– Dalvik executable
– Only supports a subset of standard Java Library
• JVM
– Sun
– Java bytecode
• Some worries that Java world may be divided into
different communities, each has its own Java standard
27
Application Framework
28
Application Framework (cont.)
29
Applications
30
Android Lifecycle
Activity Lifecycle
Activity = methods +
Layout + Dialogs
Layout = Views,
Buttons, etc.
App = many Activities
Task = stack of
Activities from one app
Also: Services,
Broadcast Receivers,
Content Providers
Android development
Java Source
Android
Manifest
Generated Java .dex Dalvik
Class Compiler File VM
Resource
XML
Android
Libraries
Producing an Android App
javac
Java code Byte code
dx
.java .class Dalvik exe
classes.dex aapt
<str>
Resources
Android applications are compiled to Dalvik
bytecode
Write
Write app
app in
in Java
Java
Compiled
Compiled in
in Java
Java
Transformed
Transformed to
to Dalvik
Dalvik bytecode
bytecode
Loaded
Loaded into
into Dalvik
Dalvik VM
VM
Linux OS
The Dalvik runtime is optimized for mobile
applications
50
Setup Emulators
53
Android Emulator: 1.6 Device
54
Android Emulator: 2.2 Device
55
Android Emulator: 3.0 Device
56
Emulator Limitations
• No support for placing or receiving actual phone calls
– Simulate phone calls (placed and received) through the emulator
console
• No support for USB connections
• No support for camera/video capture (input)
• No support for device-attached headphones
• No support for determining connected state
• No support for determining battery charge level and AC charging
state
• No support for determining SD card insert/eject
• No support for Bluetooth
• No support for simulating the accelerometer
– Use OpenIntents’s Sensor Simulator
57
You may test app with other Android Emulator
also.
You may test app with other Android Emulator
also.
Android Debugging
Debugging
• Instead of using traditional System.out.println, use the Log class
– Imported with android.util.Log
– Multiple types of output (debug, warning, error, …)
– Log.d(<tag>,<string>)
• Can be read using logcat.
– Print out the whole log, which auto-updates
• adb logcat
– Erase log
• adb logcat –c
– Filter output via tags
• adb logcat <tag>:<msg type> *:S
• can have multiple <tag>:<msg type> filters
• <msg type> corresponds to debug, warning, error, etc.
• If use Log.d(), then <msg type> = D
Where We Monitor
1.camera application
2.sensor application
3.GPS Application
import android.app.Activity;
import android.os.Bundle;
Solution:
import android.app.Activity;
import android.os.Bundle;
} In this case the particular section program only thrown exception,
} remaining parts working fine .
Android Project Inner Structure
file structure for applications
Android Manifest
@2011 Mihail L. Sichitiu 70
UI layouts are in Java and XML
72
src/HelloAndroid.java
73
res/layout/main.xml
74
res/values/strings.xml
75
Strings
• In res/values
– strings.xml
• Application wide available strings
• Promotes good software engineering
• UI components made in the UI editor should
have text defined in strings.xml
Do not
modify!
77
R Class
79
Core Components-Activity #1
• Basically, An activity presents a visual user interface for
one focused endeavor the user can undertake
• An application might consist of just one activity or several,
each Activity is derived from android.app.Activity and should
be declared in AndroidManifest.xml file
• Each activity is given a default window to draw in, the
window may be full screen or smaller and on top of other
window
• The visual content of the window is provided by a hierarchy
of views — objects derived from the base View class
• Activity.setContentView() method is used to set a certain
hierarchy of view objects
80
Core Components-Activity #2
• Activities are activated by asynchronous messages called
intents
– An intent is an Intent object that holds the content of the message
– The action being requested or the URI of the data to act on
• The <intent-filter> label in AndroidManifest.xml file specifies
the Intent that can start the Activity
84
Elements and layouts
• Table Layout
– Like the HTML div tag
/* table.xml */
<?xml version=“1.0” encoding=“utf-8”?>
<TableLayout android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:stretchColumns=“1”>
<TableRow>
<TextView android:layout_column=“1”
android:text=“Open...”
android:padding=“3dip” />
<TextView android:text=“Ctrl-O”
android:gravity=“right”
android:padding=“3dip” />
</TableRow>
</TableLayout>
Elements and layouts
• Grid View
/* grid.xml */
<?xml version=“1.0” encoding=“utf-8”?>
<GridView
android:id=“@+id/gridview”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:columnWidth=“90dp”
android:numColumns=“auto_fit”
android:verticalSpacing=“10dp”
android:horizontalSpacing=“10dp”
android:stretchMode=“columnWidth”
android:gravity=“center”
/>
Elements and layouts
• Grid View
/* GridExample.java */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
gridview.setOnItemClickListener(
new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int pos, long id) {
Toast.makeText(
GridPrimer.this, "" + pos, Toast.LENGTH_SHORT).show();
}});
}
Elements and layouts
• Grid View
/* AdapterForGridView.java */
public class AdapterForGridView extends BaseAdapter {
private Context mContext;
public AdapterForGridView(Context c) { mContext = c; }
public int getCount() { return mThumbIDs.length; }
public Object getItem(int position) { return null;}
public long getItemId(int position) { return 0; }
/* selector2.xml */
/* selector3.xml */
Elements and layouts
• Tab Layout
/* Tab1.java */
public class Tab1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Tab2.java */
/* Tab3.java */
Elements and layouts
• Tab Layout
/* TabExample.java */
public class TabExample extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost = getTabHost();
//--- tab 1 ---
Intent intent = new Intent().setClass(this, Tab1.class);
TabHost.TabSpec spec = tabHost.newTabSpec(“tab1”).setIndicator(
“Artists”, getResources().getDrawable(R.drawable.selector1))
.setContent(intent);
tabHost.addTab(spec);
//--- tab 1 ---
tabHost.setCurrentTab(2);
}
Elements and layouts
• List View
/* list_item.xml */
<?xml version=“1.0” encoding=“utf-8”?>
<TextView
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:padding=“10dp”
android:textSize=“16sp” />
Elements and layouts
• List View
/* ListViewExample.java */
public class ListViewExample extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}});
}
Elements and layouts
• Button
• ImageButton
• EditText
• CheckBox
• RadioButton
• ToggleButton
• RatingBar
Elements and layouts
• DatePicker
• TimePicker
• Spinner
• AutoComplete
• Gallery
• MapView
• WebView
Menus
• Options Menu: MENU button, tied to an Activity
• Context Menu: View LongPress
• Submenu
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ADD, 0, “Add”)
.setIcon(R.drawable.icon);
menu.add(0, MENU_WALLPAPER, 0, “Wallpaper”);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case MENU_ADD: //... ; return true;
case MENU_WALLPAPER: //... ; return true;
default: return false;
}
}
Apps | intent
• Allows communication between components
– Message passing
– Bundle
Code:
Toast.makeText(getApplication
Context(), "Button is clicked",
Toast.LENGTH_LONG).show();
AlertDialog
AlertDialog
• AlertDialog alertDialog = new AlertDialog.Builder(
• AlertDialogActivity.this).create();
•
• // Setting Dialog Title
• alertDialog.setTitle("Alert Dialog");
•
• // Setting Dialog Message
• alertDialog.setMessage("Welcome to AndroidHive.info");
•
• // Setting Icon to Dialog
• alertDialog.setIcon(R.drawable.tick);
•
• // Setting OK Button
• alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
• public void onClick(DialogInterface dialog, int which) {
• // Write your code here to execute after dialog closed
• Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
• }
• });
•
• // Showing Alert Message
• alertDialog.show();
Android Example
Create a new Android Project
• File->New-
>Android project
– Project name
– Build Target
– Application
name
– Package name
– Create Activity
106
Hello World Project
107
Hello World Tutorial
Hello World
Add the following lines marked by “// <-----”
Hello World Tutorial
Hello World
Run > Run > Android Application
Say Hello World
• modify HelloWorld.java
110
Run Hello World
111
Behind HelloWorld #1
• R.java, generated by Android SDK, represents all the resources of the
app. resources are all in res folder
• resources are pre-compiled into binary format
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package sample.hello;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
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;
}
112
}
Behind HelloWorld #2
Linear Layout
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:orientation="vertical"
TextView, display
android:layout_width="fill_parent"
android:layout_height="fill_parent"> static text
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
A reference to
String resource
‘hello’ 113
Behind HelloWorld #3
referenced in referenced in
res/layout/activity_main.xml AndroidManifest.xml
114
Behind HelloWorld #4
115
Behind HelloWorld #5
116
HelloWorld Example 2
• Build up an app that you can input your greetings
and display your greetings
– Input: EditText
– Display: TextView
– Of course, we have to add an button
• Edit res/layout/activity_main.xml file to add these
components
117
Beyond HelloWorld #2
• modify HelloWorld.java
– firstly get the references declared in activity_main.xml
118
Beyond HelloWorld #3
• Finished!
• Run->Run as->Android Application
• API Reference
– http://developer.android.com/reference/packages.html
Thank You