android development tutorial...title android development tutorial author diego milano subject japan...
TRANSCRIPT
![Page 1: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/1.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
japan linux symposiumandroid development tutorial
diego torres [email protected]
tokyo, october 2009
![Page 2: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/2.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
“I have always wished that my computer would be as easy to use as my telephone.
My wish has come true.
I no longer know how to use my telephone.”
Bjarne Stroustrup
![Page 3: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/3.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
agenda
● introduction to android
● development environment
● building blocks
● your first android application
● testing and performance
![Page 4: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/4.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
introduction to android
● identify unique features of android platform
● compare android against other platforms
● understand android building blocks
after this section you will...
![Page 5: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/5.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
what is android ?● android is the first complete, open
and free mobile platform● developed by Open Handset
Alliance● software stack than includes
– operating system– middleware– key applications– rich set of APIs
Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative
Commons 2.5 Attribution License.
![Page 6: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/6.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
is android linux ?
● no native windowing system
● no glibc support● no GNU/Linux utilities
android is based on a linux kernel but it's not GNU/Linux
NO, android is not linux !
![Page 7: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/7.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
so is android java ?
● uses the java language● implements part of the
Java5 SE specification● runs on a dalvik virtual
machine instead of JVM
android is not an implementation of any of the Java variants
NO, android is not java !
![Page 8: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/8.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
android linux kernel
● security● memory management● process management● network stack● driver model● abstraction layer
android is based on a linux 2.6 kernel, prnel, providing
kernel source: source.android.com
![Page 9: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/9.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
linux kernel enhancements
● alarm● ashmem● binder● power management● low memory killer (no swap space available)
● logger
android introduces some linux kernel patches
![Page 10: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/10.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
unique platform characteristics
● open source
● “all applications are equal” model
● dalvik virtual machine
android characteristics not found on other platforms
![Page 11: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/11.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
other characteristics
● application framework enabling reuse of components
● integrated browser based on WebKit OSS engine
● 3D graphics based on the OpenGL ES
● SQLite for structured data storage
● media support for common audio, video, and still images
● camera, GPS, compass, and accelerometer
interesting features as well, but they are more common across other mobile platforms
![Page 12: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/12.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
android architectureco
urte
sy o
f Goo
gle
![Page 13: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/13.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
android development environment
● be aware of the components needed to start developing
● know the tools available on the android sdk
after this section you will...
![Page 14: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/14.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
development environment
● supported and tested by Google
● all the necessary tools available
● 32bit installation highly recommended
ubuntu is the perfect choice
![Page 15: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/15.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
development environment
● much more than an IDE● unfortunately no Eclipse
3.4 packages● download from
http://eclipse.org and install
● android ADT plugin
Eclipse is an extensible development platform
![Page 16: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/16.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
java
● installed from ubuntu repositories● packages matching sunjava6*
diego@bruce:\~$ java -versionjava version "1.6.0_16"Java(TM) SE Runtime Environment (build 1.6.0_16-b01)Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)
![Page 17: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/17.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
android SDK 1.6 and ADT
● download from http://developer.android.com and install to /opt directory
● create symbolic link /opt/android-sdk to point to installed SDK
● add site https://dlssl.google.com/android/eclipse/to eclipse software updates for ADT
![Page 18: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/18.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
android sdk updater & avd
$ android
![Page 19: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/19.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
emulator
$ emulator @1.5_r3
![Page 20: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/20.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
dalvik debug monitor
$ ddms
![Page 21: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/21.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
zipalign● optimizes the way an application is packaged● potentially makes your application and system
run faster● android accesses resources efficiently where
they are aligned on 4byte boundaries● ADT automatically align release packages
$ zipalign -c -v 4 my.apk
![Page 22: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/22.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
sqlite database browser
$ sudo apt-get install sqlitebrowser
![Page 23: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/23.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
after this section you will...
android building blocks
● recognize the fundamental building blocks
● use these building blocks to create applications
● understand applications lifecycle
![Page 24: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/24.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
building blocks
![Page 25: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/25.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Activities
● Activities are stacked like a deck of cards
● only one is visible
● only one is active
● new activities are placed on top
![Page 26: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/26.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Activities lifecycle
rectangles are callbacks wherewe can implement operationsperformed on state changes
![Page 27: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/27.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Activities states● active
– at the top of the stack
● paused– lost focus but still visible– can be killed by LMK
● stopped– not at the top of th stack
● dropped– killed to reclaim its memory
![Page 28: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/28.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Views
● Views are basic building blocks
● know how to draw themselves
● respond to events
● organized as trees to build up GUIs
● described in XML in layout resources
![Page 29: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/29.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
pattern: load layout
android compiles the XML layout code that is later loaded in code usually by
public void onCreate(Bundle savedInstanceState) { ...setContentView(R.layout.filename); ...}
![Page 30: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/30.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Views and Viewgroups● Views and
Viewgroups trees build up complex GUIs
● android framework is responsible for– measuring– laying out– drawing
![Page 31: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/31.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
pattern: ids
using an unique id in a XML View definition permits locating it later in Java code
private View name;
public void onCreate(Bundle savedInstanceState) { ... name = (View) findViewById(R.id.name); ...}
![Page 32: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/32.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Intents● Intents are used to move from Activity to Activity● describes what the application wants● provides late runtime binding
primary attributes
attribute description
action
data
the general action to be performed, such as VIEW, EDIT, MAIN, etc.
the data to operate on, such as a person record in the contacts database, as URI
![Page 33: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/33.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
intent playground
http://codtech.com/android/IntentPlayground.apk
install from android market or from
![Page 34: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/34.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Services
● services run in the background
● don't interact with the user
● run on the main threadof the process
● is kept running as long as
– is started
– has connections
![Page 35: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/35.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
Notifications
● notify the user about events
● sent through NotificationManager
● types
– persistent icon
– turning leds
– sound or vibration
![Page 36: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/36.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
ConentProviders
● ContentProviders are objects that can
– retrieve data
– store data
● data is available to all applications
● only way to share data across packages
● usually the backend is SQLite
● they are loosely linked to clients
● data exposed as a unique URI
![Page 37: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/37.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
AndroidManifest.xml● control file that tells
the system what to do and how the toplevel components are related
● it's the “glue” that actually specifies which Intents your Activities receive
● specifies permissions
![Page 38: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/38.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
after this section you will...
your first android application
● create your own android map project
● design the UI● externalize resources● react to events● run the application
![Page 39: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/39.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
create project
androidtarget
versionunique
packagename
![Page 40: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/40.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
create test project
testpackage
name
![Page 41: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/41.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
run configurations
androidtarget
version
if phone isconnected
![Page 42: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/42.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
default application● autogenerated
application template● default resources
– icon– layout– strings
● default AndroidManifest.xml
● default run configuration
![Page 43: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/43.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
designing the UIthis simple UI designs contains● the window title
● a spinner (drop down box) containing the available locations
● a map displaying the selected location
● zoom buttons
![Page 44: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/44.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
create the layout
● remove layout● add a RelativeLayout● add a View (MapView not
supported by ADT)
● replace View by com.google.android.maps.MapView
● change id to MapView● add a Spinner filling
parent width
![Page 45: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/45.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
run the application
● com.google.android.maps it's an optional library not included by default
● edit Android Manifest● add Uses Library com.google.android.maps as Application node
![Page 46: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/46.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
google maps api key
● checking DDMS logcat we find
● to access Google Maps we need a key● application must be signed with the same key● key can be obtained from Google● MapView should include
java.lang.IllegalArgumentException: You need to specify an API Key for each MapView.
android:apiKey="0GNIO0J9wdmcNm4gCV6S0nlaFE8bHa9WXXXXXX"
![Page 47: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/47.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
obtaining google maps api key
● $ keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore
● http://code.google.com/android/mapsapisignup.html
yourfingerprint
![Page 48: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/48.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
MapActivity
● checking DDMS logcat again
● change base class to MapActivity
● fix imports
● add unimplemented methods
java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
![Page 49: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/49.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
where is the map ?
● still no map displayed
● check DDMS logcat
● errors displayed
● a permission is missing
– INTERNET
● add it to Manifest
![Page 50: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/50.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
finally our map
still some problems ...
● spinner is covered
● spinner has no prompt
● externalize resource value Select a location
android:layout_alignParentTop="true"
prompt: @string/prompt
![Page 51: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/51.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
pattern: adapters
an Adapter object acts as a bridge between an AdapterView and the underlying data for that view
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.array, android.R.layout.layout);
view.setAdapter(adapter);
The Adapter is also responsible for making a View for each item in the data set.
![Page 52: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/52.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
pattern: resources
resources are external files (that is, noncode files) that are used by your code and compiled into your application at build time.
<resources> <string-array name=”array”> <item>item</item> </string-array></resources>
res = getResources().getType(id);
![Page 53: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/53.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
locations.xml<?xml version="1.0" encoding="UTF-8"?>
<resources><!-- No support for multidimensional arrays or complex objects yet (1.6r1) -->
<string-array name="location_names"><item>Akihabara Convention Hall</item><item>Hotel Villa Fontaine</item><item>Narita Intl. Airport</item>
</string-array>
<string-array name="locations"><item>35.699434,139.772079,16</item><item>35.669012,139.765384,14</item><item>35.773118,140.379181,12</item>
</string-array></resources>
http://android.codtech.com/jls2009/locations.xml
![Page 54: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/54.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
complete the class● create the locations array field
● find the views (ids pattern)
● create the adapter
locations = getResources().getStringArray(R.array.locations);
spinner = (Spinner) findViewById(R.id.Spinner01);mapView = (MapView) findViewById(R.id.MapView);
final ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.location_names, android.R.layout.simple_spinner_item);spinner.setAdapter(adapter)
![Page 55: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/55.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
almost there● map is displayed● spinner is displayed● drop down is
displayed● but there's no
selection button ...adapter. setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
![Page 56: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/56.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
respond to events● when an item is
selected map should be centered at that location
● in the listener invoke goToSelectedLocation(arg2);
spinner. setOnItemSelectedListener( new OnItemSelectedListener() { });
![Page 57: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/57.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
goToSelectedLocation
protected void goToSelectedLocation(int position) {String[] loc = locations[position].split(",");
double lat = Double.parseDouble(loc[0]);double lon = Double.parseDouble(loc[1]);int zoom = Integer.parseInt(loc[2]);
GeoPoint p = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
Log.d(TAG, "Should go to " + p);
mapController.animateTo(p);mapController.setZoom(zoom);
}
http://android.codtech.com/jls2009/goToSelectedLocation
mapController should be
initialized in onCreate
![Page 58: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/58.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
more events● turn map clickable
● override onKeyDownandroid:clickable="true”
switch (keyCode) {case KeyEvent.KEYCODE_I:
mapController.zoomIn();break;
case KeyEvent.KEYCODE_O:mapController.zoomOut();break;
case KeyEvent.KEYCODE_S:mapView.setSatellite(
!mapView.isSatellite());break;
}
http://android.codtech.com/jls2009/onKeyDown
![Page 59: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/59.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
zoom controls● add a FrameLayout● align to parent bottom● in onCreate
...zoomControls = (ViewGroup)findViewById( R.id.FrameLayout01);
...
zoomControls.addView( mapView.getZoomControls());
![Page 60: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/60.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
we did it !● Some things to try
– select a location
– pan
– zoom in
– zoom out
– toggle satellite
![Page 61: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/61.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
japanese localization
existingresource
new language
![Page 62: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/62.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
japanese application
● resources automatically selected
● match phone locale
● no programming needed
![Page 63: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/63.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
hierarchyviewer – tree
selectedview
selectedview
highlighted
properties
![Page 64: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/64.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
hierarchyviewer zoom
pixelcolor
selected pixel
![Page 65: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/65.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
“Remember that there is no code faster than no code”
Taligent's Guide to Designing Programs
![Page 66: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/66.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
after this section you will...
testing and performance
● understand the best practices to develop for android
● identify the alternatives to test units, services and applications
● performance
![Page 67: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/67.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
best practices
● consider performance, android is not a desktop● avoid creating objects● use native methods● prefer virtual over interface● prefer static over virtual● avoid internal getter/setters● declares constants final● avoid enums
![Page 68: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/68.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
testing
● android sdk 1.6 features
– creation of test projects– ActivityUnitTestCase to run isolated unit tests– ServiceTestCase to test services– ActivityInstrumentationTestCase2 to run functional
tests of activities
● ApiDemos includes some test samples
● monkey, generates pseudorandom of user events
![Page 69: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/69.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
performance
Action Time
1
4
5
5
12
12.5
15
165
600
22000
25000
100000
135000
3000000
Add a local variable Add a member variable
Call String.length()Call empty static native methodCall empty static method
Call empty virtual method Call empty interface method Call Iterator:next() on a HashMap Call put() on a HashMapInflate 1 View from XML Inflate 1 LinearLayout with 1 TextView Inflate 1 LinearLayout with 6 ViewInflate 1 LinearLayout with 6 TextView
Launch an empty activity
Add a
loca
l variable
Add a
mem
ber
variable
Call
Str
ing.le
ngth
()
Call
em
pty
sta
tic
native
meth
od
Call
em
pty
sta
tic
meth
od
Call
em
pty
virtu
al m
eth
od
Call
em
pty
inte
rface
meth
od
Call
Itera
tor:
nex
t()
on a
Hash
Map
Call
put(
) on a
Hash
Map
Infla
te 1
Vie
w f
rom
XM
L
Infla
te 1
Lin
earL
ayo
ut
with 1
Tex
tVie
w
Infla
te 1
Lin
earL
ayo
ut
with 6
Vie
w
Infla
te 1
Lin
earL
ayo
ut
with 6
Tex
tVie
w
Launch
an e
mpty
act
ivity
0
500000
1000000
1500000
2000000
2500000
3000000
Time
![Page 70: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/70.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
traceview
// start tracing to sdcardDebug.startMethodTracing(“mytrace”);
![Page 71: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/71.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
dmtracedump
$ sudo aptget install graphviz
![Page 72: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/72.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
“If things seem under control, you're not going fast enough.”
Mario Andretti
![Page 73: android development tutorial...Title android development tutorial Author Diego Milano Subject japan linux symposium Keywords android Created Date 10/9/2009 3:08:04 PM](https://reader034.vdocuments.mx/reader034/viewer/2022042215/5ebd639caca0be17d01d5475/html5/thumbnails/73.jpg)
copyright © 2009 cod technologies ltd www.codtech.com
thank youandroid development tutorial
diego torres [email protected]
tokyo, october 2009