Getting Started with Android Development Rohit Ghatol About Me Rohit Ghatol 1. Architect @QuickOffice 2. Project Mgr @Synerzip 3. Certified Scrum Master LinkedIn Profile 4. Author “Beginning PhoneGap” @Apress 5. Founder TechNext Pune (Pune Developer Community) Topics • • • • • Understanding Android Android Building Blocks Putting Building Blocks Together Latest things in Android World Reference Material 3 Understanding Android 4 What is Android? • Software stack for mobile devices that includes • an operating system • middleware • key applications • SDK to develop application 5 Android Architecture 6 Android OS Names 7 OS Version Nickname API Level Date 1.1 __ 2 9th February 2009 1.5 Cupcake 3 30 April 2009 1.6 Donut 4 5 September 2009 2.1 Eclair 7 26 October 2009 2.2 Froyo 8 20 May 2010 2.3 Gingerbread 9 6th December 2010 3.0 Honeycomb 11 22nd February 2011 4.X Icecream Sandwich 14 19th October 2011 http://en.wikipedia.org/wiki/Android_version_history 8 Key OS Capabilities 9 Android OS Capabilities • Phone and OS features – – – – 3G/4G,GPS, Accelerometer, Compass, Camera, SQLite, Wifi, Bluetooth, etc Near Field Communication Cloud to Device Messaging (C2DM) Direct Wifi Android OS Capabilities • Notable Features of Android – – – All Applications are equal Reuse of Data Reuse of Functionality All Applications are Equal • • • • • Replace Home Application Replace Contacts, Dialer Applications Replace SMS, Email Applications Replace Settings Application OEM Customizations (e.g HTC Sense) 12 Reuse of Data Default Contact Manager New Contact Manager Replaces What happens to data feed into the default Contact Manager? Reuse of Data Default Contact Manager New Contact Manager Replaces Content Provider “Replaces” means by default the new app is launched, but old app still exists Reuse of Functionality My Coupons New Requirement Share with Friends using 1. SMS 2. Email Time to learn SMS API and Email API and code them into my application! More code! Hee hee Reuse of Functionality My Coupons SMS Intention: Want to send Email Mail Here are two applications who can do it for you? Reuse of Functionality My Coupons SMS Mail Reuse of Functionality My Coupons SMS Mail Android Environment Setup http://developer.android.com/sdk/in stalling.html 19 Eclipse Android SDK Android ….. ADT SDK Manager AVD Manager Manages Android 2.2 Google API 2.2 ………………. Emulator Emulator Android 3.2 Android 4.x 20 Android Application Android Manifest Dex File MyApp.apk Signed by Self Signed Private Key Resources Identity of Android Application Identity Part Example Package Name com.sparklytix.android.app.twitter versionCode 1 (numeric value 2,3,4,..101,102) Private Key 22 Android Build Cycle .java .class javac .dex .apk dx apt • AndroidManifest.xml • resources 23 How Applications behave? 24 Uid 1 Uid 2 Uid 3 data data com.xyz.email shared_prefs Dalvik VM Dalvik VM Dalvik VM files UID 1 databases com.abc.skype Process Process Process ...... UID 2 com.koko.sukudo Linux Kernel ...... 25 3 UID Android Building Blocks 26 Activity Service Broadcast Receiver Content Resolver Content Provider Intents Building Blocks Alarm Manager Notification Manager …… Other Components Read more - http://developer.android.com/guide/topics/fundamentals.html First Android Application 28 29 30 31 32 33 34 Interacting with Buttons 35 Interacting with Button 36 Interacting with Buttons 37 38 Screen Navigation 39 40 41 42 43 44 45 startActivityForResult() 46 47 48 49 50 51 Intents 52 Understanding Intent Intents Explicit Need • Class Name Implicit Need • ACTION • CATEGORY • DATA Program Launcher <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample“ android:versionCode="1“ android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloWorld" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest> Program launcher shows all the activities which have MAIN Action and LAUNCHER category Conflicts with implicit Intents So what happens two activities have the exact same intent filter and an intent is fired. Simple you choose one application, and you have an option to tell to system that application as the default application hence forth Intent API Reference Intent to launch an Activity • Context.startActivity(intent) • Context.startActivityForResult(intent) Intent to launch an Service • Context.startService(intent) Intent to send a broadcast • Context.sendBroadCast(intent) Activity 57 Activity Life Cycle Activity Life Cycle (made easy) onCreate onStart onResume Complete LifeCycle Visible LifeCycle Foreground LifeCycle onPause onStop onDestroy What to do in what method? onCreate onStart onResume onPause onStop onDestroy Services 61 Calling Service Activity startService(intent) Service void onStartCommand (Intent intent,…){ } Fire & Forget Calling Service Activity Service void foo(){ } int bar(){ } 1. 2. 3. 4. RPC Style bindService(intent) ….. service.foo() int result=service.bar() 64 Broadcast Receivers 65 Broadcast Receivers Network Change Battery Low App 1 App 2 Android OS Roaming Boot Your App Custom Event 1 Custom Event 2 Interested in any of these Events. Broadcast Receivers • • • • • No Life Cycle Methods Only Call back method 10 second limit before ANR Need not register with Android Manifest Can be registered at Runtime Use case Email Application 68 Mail Application – Use Case Building Blocks of Android Phone Boots Mail Sync Activity Service Communication is using Intents Data Store (Email List) Broadcast Receiver Content Provider/ SQL ite Database Data Store (Email List) Mail Sync Phone Boots Database Service Activity Broadcast R Phone Boots Events.. Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Broadcast R Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Broadcast R Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync starts Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Completes Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Mail Sync Phone Boots Stores Data Store (Email List) Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Notifi. M.. Alarm M.. Mail Sync Phone Boots Stores Data Store (Email List) Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Mail Notification Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Mail Notification Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Mail Notification Notifi. M.. Alarm M.. Data Store (Email List) Mail Sync Phone Boots Database Service Activity Broadcast R Phone Boots Mail Sync (5 mins) Events.. Mail Notification Notifi. M.. Alarm M.. Twitter App • All these Building blocks are covered in more detail on 3rd November at 3:40 p.m in “Building Twitter App for Android” 82 Q&A 83 More about Me • Twitter - http://twitter.com/#!/rohitghatol • TechGig - http://www.techgig.com/rohitghatol • LinkedIn http://www.linkedin.com/in/rohitghatol • Presentations www.slideshare.net/rohitsghatol/ • YouTube Tutorials http://www.youtube.com/user/rohitssghatol