Android Development Marc Mc Loughlin
Android Development Android Developer Website:h:p://developer.android.com/ Dev Guide Reference Resources Video / Blog SeCng up the SDK h:p://developer.android.com/sdk/ installing.html Install Eclipse Download SDK Set system PATH to point to C:<path to sdk>\tools Allow shell wide access to SDK tools Android Emulator adb, etc Install the Android Development Tools (ADT) Plugin Windows: Install USB driver to connect to external device
Development Tools ADT Lets you access development tools from Eclipse Provides Android Project wizard, exports your project to APK Allows you to create an Android Virtual Device (AVD) AVD Device configurayon that lets you model device specificayons Hardware (memory) Android OS version Storage Created using either the ADT or the android tool (SDK- directory/tools/) Runs on the Emulator
Development Tools Emulator Included in SDK (SDK- DIRECTORY/tools/) Load AVD configurayon to specify Android version, hardware for the emulator instance Uses disk images on development machine to simulate flash images on device Has networking capabiliyes but requires setup Android Debug Bridge (adb) Allows access to the emulator instance or Android Device Installing applicayons Copying files to and from device Provides shell to run commands on device
Android Architecture ApplicaYons Core applicayons shipped with device (email client, calendar, maps, browser, etc) All user installed applicayons ApplicaYon Framework Set of services and systems that your applicayon can use AcYvity Manager, Content providers, Resource Manager Core Libraries Subset of J2SE Android RunYme Dalvik virtual machine opymized for mobile devices Runs class files (dex files) Libraries C/C++ libraries but called through java interfaces Linux kernel Underlying operayng system
Manifest file Each applicayon runs on an individual Dalvik virtual machine inside a linux process To access other applicayons and specific parts of the API it must have permission An applicayons permissions are set in it s manifest file Manifest file: Tells the android operayng system about the applicayon Permissions Its components (AcYviYes, Services, Content Providers, etc)
Android ApplicaYon Components AcYviYes AcYvity: A window to draw user interface components on Numerous acyviyes = Task = Android App Each window is a subclass of the acyvity class Services Background processes Carries out any long process Broadcast Receivers Listens for events Receives broadcast announcements (Intents) and reacts to them (Ymezone changes, etc) Content providers Persist data SQLite database Shared preferences
Intents AcYviYes, Services and Broadcast Receivers talk to each other via Intents Intents = messages among the applicayon components Can be within your applicayon: calling an acyvity or service in your applicayon Or System wide: accessing the address book acyvity An intent contains informayon AcYon Data
Android AcYviYes
Android - AcYviYes AcYvity class Each UI Window extends the acyvity class acyvity is a frame or window to hold the GUI Each acyvity loads a view Handles user inputs AcYvity has three states AcYve or Running: AcYvity is the focus of users acyons Paused: AcYvity has lost focus but syll visible Stopped: AcYvity is completely hidden from user
AcYviYes AcYvity Manager Manages the lifecycle of acyviyes As it moves between states specific methods are called Hooks you can override to do work when the state changes Manages back stack for user navigayon
All acyviyes should implement the oncreate (Bundle) method Called every Yme an acyvity is launched This is where acyvity is iniyalised Loading views CreaYng database connecyons GeCng intent informayon Nearly all acyviyes will implement the onpause() method This is where you clean up ager your acyvity Android AcYviYes
Android AcYvity User interface User Interface defined Create layouts at runyme Declare UI elements in xml and loaded as a resource (Preferred) Create UI in xml and then reference them in your code At runyme android parses the xml resource files and instanyates them Allows you to separate the presentayon from code Resources Stored in res directory in an applicayon Android applicayons have a resource table where it keeps all the resource informayon R.java file managed by ADT creates new resource ID when resource added to applicayon Types of resources Color informayon Layout (UI) informayon Strings (ApplicaYon text).
AcYviYes Declaring views Layout declared in xml Resource files Each layout must have a root view (LinearLayout) All other views are child elements of this root Loaded into acyvity by calling the setcontentview() method Each view can be given Unique idenyfiers id Instances of the view objects can be created can be captured from the layout using the findviewbyid(int) method These instances can then be used to perform operayons on the view For example: responding to user interacyons, changing content views <?xml version="1.0" encoding="up- 8"?> <LinearLayout xmlns:android="h:p://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientayon="verycal" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, I am a TextView" /> <Bu:on android:id="@+id/bu:on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello, I am a Bu:on" /> </LinearLayout> public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview.(r.layout.main_layout); Bu:on mybu:on = (Bu:on) findviewbyid(r.id.bu:on) }
AcYviYes User InteracYon Capturing events from view objects (Bu:ons, etc) Event listeners Captures user interacyon Interfaces that contains methods that are called when a user interacts This allows you to tailor what your applicayon does depending on user interacyon private OnClickListener bu:onlistener = new OnClickListener() { public void onclick(view v) { // do something when the bu:on is clicked } }; protected void oncreate(bundle savedvalues) { } Bu:on bu:on = (Bu:on)findViewById(R.id.bu:on); bu:on.setonclicklistener(bu:onlistener);...
Intents StarYng a new acyvity Intents are messages or events They can be used to interact with acyviyes, services and broad cast receivers Explicit intents Targets specific components Implicit intents EXPLICIT INTENT Intent i = new Intent(this, NextAc1vity.class); startac1vity(i); Intent intent = new Intent("com.google.zxing.client.android.SCAN");
Advice Refresh your knowledge of Java (Do some tutorials) Learn how to use the Debugger! Look at the tutorials on the Android Developer site h:p://developer.android.com/resources/tutorials/ hello- world.html h:p://developer.android.com/resources/tutorials/ views/index.html h:p://developer.android.com/resources/tutorials/ notepad/index.html