Geekcamp Android

download Geekcamp Android

If you can't read please download the document

Transcript of Geekcamp Android

Android Development

GeekCamp Singapore22nd August 2009Leong Hean Hong

(CC) BY-SA Some rights preserved.

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 != JVMDesigned and written by Dan Bornstein with contributions from other Google engineers

Bytecode on which it operates is not Java bytecode.

Development Tools

Android 1.5 SDK

Optional, recommendedEclipse (Java or RCP version recommended)

Android Development Tools (ADT) eclipse plugin
(https://dl-ssl.google.com/android/eclipse/)

Setup

Download and unzip Android SDK.

Install Eclipse.

Install ADT.

Set SDK path in Eclipse preference section.

Create Android Virtual Device (AVD).

RTFM and start coding.Reference: http://bit.ly/3T24gE

Linux Tips

Dont 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 /tools/ddms once, before installing ADT. (solved in ADT 0.9.1 http://bit.ly/15FdZt)

Application Components

ActivityPresents a visual UI for user to interact with.

Similar to UIViewController.

ServiceNo visual UI, run in background for an indefinite period of time.

Similar to daemon.

Broadcast ReceiverDoes nothing but receive and react to broadcast announcements (E.g. Timezone changed, battery low, ...).

Content ProviderMakes 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/Source code.

Android 1.5/Library you will use.

gen/AIDL (Android Interface Definition Language), auto generated.

asset/Save data as asset when you need to read raw bytes.

Android Project Files

res/ (application resources access through R class)drawable/ (bitmap files)

layout/ (design Views, similar to HTML)

values/ (predefined values, can be referenced in application)

AndroidManifest.xmlSimilar to iPhones Info.plist.

Contains application settings (E.g. Intent filters, permission, activities, )

default.propertiesContains project settings such as build target.

/res/layout/main.xml

Layout for main activity.

Contains: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)

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

/res/layout/photo_viewer.xml

Layout for the PhotoViewer Activity

Contains an ImageView for displaying image

The ImageView is given an ID (photoView)

photo_viewer.xml

PhotoViewer.java

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.photo_viewer);ImageView photoView = (ImageView)findViewById(R.id.photoView);photoView.setImageURI(getIntent().getData());}

AndroidManifest.xml

Create Menu

private static final int MENU_RED = Menu.FIRST;private static final int MENU_GREEN = Menu.FIRST + 1;private static final int MENU_BLUE = Menu.FIRST + 2;private static final int MENU_RAND = Menu.FIRST + 3;

public boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);menu.add(Menu.NONE, MENU_RED, Menu.NONE, "Filter Red");menu.add(Menu.NONE, MENU_GREEN, Menu.NONE, "Filter Green");menu.add(Menu.NONE, MENU_BLUE, Menu.NONE, "Filter Blue");menu.add(Menu.NONE, MENU_RAND, Menu.NONE, "Random Filter");return true;}

Response to Menu Selection

public boolean onMenuItemSelected(int featureId, MenuItem item) {super.onMenuItemSelected(featureId, item);switch(item.getItemId()) {case MENU_RED:photoView.setColorFilter(redFilter());break;case MENU_GREEN:photoView.setColorFilter(greenFilter());break;case MENU_BLUE:photoView.setColorFilter(blueFilter());break;case MENU_RAND:photoView.setColorFilter(randFilter());break;}return true;}

Color Filter Matrix

ColorMatrixColorFilter

private ColorMatrixColorFilter redFilter() {return new ColorMatrixColorFilter(new float[] {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0});}

Q & A

Any question?

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso