Android Development GeekCamp Singapore 22nd August 2009 Leong Hean Hong (CC) BY-SA Some rights preserved. Stolen (with permission :) and edited by Peter Svensson, GTUG.

Download Report

Transcript Android Development GeekCamp Singapore 22nd August 2009 Leong Hean Hong (CC) BY-SA Some rights preserved. Stolen (with permission :) and edited by Peter Svensson, GTUG.

Android Development
GeekCamp Singapore
22nd August 2009
Leong Hean Hong
(CC) BY-SA Some rights preserved.
Stolen (with permission :) and edited by Peter Svensson, GTUG Stockholm
About
•
•
•
•
Usernames: hongster, ahhong
DOB: 490204800 (GMT +8)
Work: LAMP | Android | iPhone
Interest: Python, Rubik Cube, Number Theory, Web
Technologies
• Groups: SG PHP User Group, KL Google
Technology User Group
Content
•
•
•
•
•
•
•
Intro
Development Tools
Setup
4 Component Types
Views
Intents
Demo
Intro
• Android != OS
“Android™ delivers a complete set of software for
mobile devices: an operating system, middleware and
key mobile applications.”
• Android Architecture (http://bit.ly/s73P2)
• Dalvik_VM != JVM
Designed and written by Dan Bornstein with contributions
from other Google engineers
o Bytecode on which it operates is not Java bytecode.
o
Development Tools
• Android 1.5 SDK
• Optional, recommended
o
o
Eclipse (Java or RCP version recommended)
Android Development Tools (ADT) eclipse plugin
(https://dl-ssl.google.com/android/eclipse/)
Setup
1.Download and unzip Android SDK.
2.Install Eclipse.
3.Install ADT.
4.Set SDK path in Eclipse preference section.
5.Create Android Virtual Device (AVD).
6.RTFM and start coding.
Reference: http://bit.ly/3T24gE
Linux Tips
• Don’t use package manger, download Eclipse and
unzip it.
• Use Sun Java (“requires plug-in
org.eclipse.wst.sse.ui” error)
• If installed Eclipse using root, update using root.
• Run <android-sdk>/tools/ddms once, before
installing ADT. (solved in ADT 0.9.1
http://bit.ly/15FdZt)
Application Components
• Activity
o Presents a visual UI for user to interact with.
o Similar to UIViewController.
• Service
o No visual UI, run in background for an indefinite period of time.
o Similar to daemon.
• Broadcast Receiver
o Does nothing but receive and react to broadcast announcements (E.g. Timezone
changed, battery low, ...).
• Content Provider
o Makes a specific set of the application's data available to other applications. (E.g.
Wrapper for data in file system or DB)
Android Project Folders
• src/
o
Source code.
• Android 1.5/
o
Library you will use.
• gen/
o
AIDL (Android Interface Definition Language), auto
generated.
• asset/
o
Save data as asset when you need to read raw bytes.
Android Project Files
• res/ (application resources access through R class)
o drawable/ (bitmap files)
o layout/ (design Views, similar to HTML)
o values/ (predefined values, can be referenced in application)
• AndroidManifest.xml
o Similar to iPhone’s Info.plist.
o Contains application settings (E.g. Intent filters, permission,
activities, …)
• default.properties
o Contains project settings such as build target.
/res/layout/main.xml
• Layout for main activity.
• Contains:
o
o
o
LinearLayout for arranging subviews
TextView for displaying text
Button
• An ID (helloButton) is assigned to the button. Can be
referenced through R.id.helloButton.
• Button text is defined in /res/values/string.xml.
Add Button (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/helloButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_button" />
</LinearLayout>
Launch Image Gallery
private static final int REQUEST_PICK_IMAGE = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.helloButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent pickImage = new Intent(Intent.ACTION_PICK);
pickImage.setType("image/*");
startActivityForResult(pickImage, REQUEST_PICK_IMAGE);
}
});
}
Intent
• Abstract description of an action to be preformed.
• Intent messaging is a facility for late run-time binding
between components in the same or different
applications.
• Activity, Service, BroadcastReceiver are activated
through Intent.
• Intent Resolution
New Activity
• Create a new Activity called PhotoViewer
• Create a Layout to display the photo
(photo_viewer.xml)
• Use a ImageView to display photo
• GoodByeWorld launch PhotoViewer by sending it an
Intent
• The Intent contains the URI of the selected image
• ImageView load image from the URI
Sample Android App
Intent
SMS
Sample Android App
Intent
(Start other program)
Sample Android App