App Development for Android Prabhaker Matet
Development Tools (Android) Java Java is the same. But, not all libs are included. Unused: Swing, AWT, SWT, lcdui Eclipse www.eclipse.org/ ADT Plugin for Eclipse developer.android.com/ Android SDK developer.android.com/ Android Device Emulator Development Platforms: Linux, Mac OSX, or Windows 2
(Other) Languages and IDEs IntelliJ Idea Android Studio Corona for Android Android Natve Development Kit (NDK) Scala 3
Applicaton Runtme Each applicaton is a different user. Each applicaton gets a unique Linux user ID. The system sets permissions for all the files in an applicaton so that only the user ID assigned to that applicaton can access them. Each process has its own Dalvik/Art VM. Every applicaton runs in its own Linux process. A process can have multple threads. 4
Applicaton Framework Views lists, grids, text boxes, butons, embeddable web browser Content Providers to access data from other applicatons, or to share their own data Resource Manager access non-code resources; e.g., strings, graphics, and layout files Notficaton Manager alerts in the status bar Actvity Manager lifecycle of applicatons and navigaton backstack 5
Applicaton Components Activity: (GUI) functons that the applicaton performs. Service: no UI run in the background; Long-running; for remote processes no user interface. Content Providers facilitate data transmission among different applicatons. Broadcast Receiver: respond to announcements. Groups of views define the applicaton s layout. Each component is a different entry point of the system. An applicaton can have multple instances of the above. 6
Actvity An applicaton typically consists of several screens: Each screen is implemented by one actvity. Moving to the next screen means startng a new actvity. An actvity may return a result to the previous actvity. 7
Actvity One of the actvites is marked as the main one. Presented on launch. An actvity is usually a single screen: Implemented as a single class extending Actvity. Displays user interface controls (views). Reacts on user input/events. 8
Life cycle of an Actvity 9
Services A service does not have a (visual) user interface. Runs in the background for an indefinite period tme. Examples: music player, network download, Similar to daemons in Linux/Unix or Windows services. Each service extends the Service base class. Communicate with the service through an interface defined in AIDL (Android Interface Definiton Language). 10
Services Interprocess communicaton (IPC). startservice(); stopself() ; stopservice() bindservice(). Multple components can bind to the service at once. When all of them unbind, the service is destroyed. onstartcommand() onbind() oncreate() ondestroy() 11
Broadcast Receivers Broadcast announcements: Intents. All receivers extend the BroadcastReceiver base class. Many broadcasts originate in the System. Ex: the tme zone has changed Ex: the batery is low Applicatons can also initate broadcasts. 12
Content Providers Enables sharing of content across applicatons E.g., address book, photo gallery the only way to share data between applicatons. APIs for query, delete, update and insert. Use ContentResolver methods to do the above. Content is represented by URI and MIME type. 13
Content Providers Application Application Activity Activity Application Activity Content Resolver Service Content Resolver Content Provider Content Resolver Data SQLite XML Remote Store 14
Intent Examples ACTION_DIAL content://contacts/people/13 Display the phone dialer with the person #13 filled in. ACTION_VIEW content://contacts/people/ Display a list of people, which the user can browse through. startactvity(new Intent(Intent.VIEW_ACTION, Uri.parse( "htp://www.fhnw.ch")); startactvity(new Intent(Intent.VIEW_ACTION, Uri.parse("geo:47.480843,8.211293")); startactvity(new Intent(Intent.EDIT_ACTION, Uri.parse("content://contacts/people/1")); atributes: category, type, component, extras 15
Intent Intents are system messages: Actvity events ( launch app, press buton) Hardware state changes (acceleraton change, screen off, etc) Incoming data (Receiving call, SMS arrived) An intent object is an acton to be performed on some data URI. Provides binding between applicatons. 16
public class Intent startactvity to launch an actvity. broadcastintent to send it to a BroadcastReceiver Communicate with a Service startservice(intent) or bindservice(intent, ServiceConnecton, int) Explicit Intents specify a component to be run. setcomponent(componentname) or setclass(context, Class)) Implicit Intents match an intent against all of the <intent-filter>s in the installed applicatons. 17
IntentReceivers Components that respond to Intents Way to respond to external notficaton or alarms Apps can create and broadcast own Intents 18
Example App: Hello World! developer.android.com/resources/tutorials /hello-world.html
The Emulator QEMU-based ARM emulator Displays the same image as the device Limitatons: Camera GPS 20
Goal Create a very simple applicaton Run it on the emulator Examine its structure 21
Building HelloAndroid Create a Project htp://developer.android.com/training/basics/first app/creatng-project.html Generates several files Next few slides Modify HelloAndroid.java as needed Android-Develop-1 22
helloandroid Manifest 1. <?xml version="1.0" encoding="utf-8"?> 2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3. package="com.example.helloandroid" 4. android:versioncode="1" 5. android:versionname="1.0"> 6. <applicaton android:icon="@drawable/icon" android:label="@string/app_name"> 7. <actvity android:name=".helloandroid" 8. android:label="@string/app_name"> 9. <intent-filter> 10. <acton android:name="android.intent.action.main" /> 11. <category android:name="android.intent.category.launcher" /> 12. </intent-filter> 13. </actvity> 14. </applicaton> 15. </manifest> 23
HelloAndroid.java package com.example.helloandroid; import android.app.actvity; import android.os.bundle; public class HelloAndroid extends Actvity { /** Called when the actvity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } } Set the layout of the view as described in the main.xml layout 24
HelloAndroid.java package com.example.helloandroid; import android.app.actvity; import android.os.bundle; import android.widget.textview; Inherit from the Activity Class public class HelloAndroid extends Actvity { /** Called when the actvity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); TextView tv = new TextView(this); tv.settext("hello, Android by hand"); setcontentview(tv); } } Set the view by hand from the program 25
Run it! 26
Android Applicaton Package: APK res/layout: declaraton layout files res/drawable: intended for drawing res/anim: bitmaps, animatons for transitons res/values: externalized values strings, colors, styles, etc res/xml: general XML files used at runtme res/raw: binary files (e.g., sound) An applicaton consists of: Java Code Data Files Resources Files 27
APK Content All source code here Java code for our activity All non-code resources Images Generated Java code Helps link resources to Java code Layout of the activity Strings used in the program Android Manifest 28
Android Applicaton Package: APK Using Java/Eclipse/ADT develop source files. An Android applicaton is bundled by the aapt tool into an Android package (.apk) An.apk file is a zip file. Invoke unzip if you wish. Installing an Applicaton is a built-in op of Android OS. 29
.apk Internals 1. AndroidManifest.xml deployment descriptor for applicatons. 2. IntentReceiver as advertsed by the IntentFilter tag. 3. *.java files implement Android actvity 4. Main.xml visual elements, or resources, for use by actvites. 5. R.java automatcally generated by Android Developer Tools and "connects" the visual resources to the Java source code. 6. Components share a Linux process: by default, one process per.apk file. 7..apk files are isolated and communicate with each other via Intents or AIDL. 30
Applicaton Resources anything relatng to the visual presentaton of the applicaton images, animatons, menus, styles, colors, audio files, resource ID alternate resources for different device configuratons 31
AndroidManifest.xml Declares all applicaton components: <actvity> <service> <provider> for content providers <receiver> for broadcast receivers The manifest can also: Identfy any user permissions the applicaton requires, such as Internet access or read-access to the user's contacts. Declare hardware and software features used or required by the applicaton API libraries the applicaton needs 32
/res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientaton="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> Further redirection to /res/values/strings.xml 33
/res/values/strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello World, HelloAndroid by resources!</string> <string name="app_name">hello, Android</string> </resources> 34
/gen/r.java package com.example.helloandroid; public final class R { public statc final class atr { } public statc final class drawable { public statc final int icon=0x7f020000; } public statc final class id { public statc final int textview=0x7f050000; } public statc final class layout { public statc final int main=0x7f030000; } public statc final class string { public statc final int app_name=0x7f040001; public statc final int hello=0x7f040000; } } R.java is auto generated on build. Based on the resource files (including layouts and preferences) Do not edit. 35
Run it! 36
Debugging adb Android Debug Bridge moving and syncing files to the emulator running a Linux shell on the device or emulator Dalvik Debug Monitor Server DDMS is GUI + adb. capture screenshots gather thread and stack informaton spoof incoming calls and SMS messages Device or Android Virtual Device JDWP Java Debug Wire Protocol Java IDEs include a JDWP debugger command line debuggers such as jdb. 37
Introduce A Bug package com.example.helloandroid; import android.app.actvity; import android.os.bundle; public class HelloAndroid extends Actvity { /** Called when the actvity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); Object o = null; o.tostring(); setcontentview(r.layout.main); } } 38
Run it! 39
Source Code for Android Examples Sources for many Android applicatons that can be enhanced: htp://code.google.com htp://developer.android.com/resources/brow ser.html?tag=sample 40