Android for Java Developers at OSCON 2010

1,509 views
1,438 views

Published on

While Android programming is based on Java, there are some important philosophical differences and Android-specific constructs to consider. Android for Java Developers is an action-packed, hands-on presentation that takes you through the anatomy of an Android application. The sample application includes most major Android building blocks (Activities, Intents, Services, Broadcast Receivers, Content Providers) to illustrate the philosophy of Android application development. It assumes basic Java knowledge.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,509
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
145
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android for Java Developers at OSCON 2010

  1. 1. Android  for  Java   Developers   OSCON  2010   Marko  Gargenta   Marakana  
  2. 2. About  Marko  Gargenta   Developed Android Bootcamp for Marakana. Trained over 1,000 developers on Android. Clients include Qualcomm, Sony-Ericsson, Motorola, Texas Instruments, Cisco, Sharp, DoD. Author of upcoming Learning Android by O’Reilly. Spoke at OSCON, ACM, IEEE, SDC. Organizes SFAndroid.org
  3. 3. Agenda   •  The  Stack   •  Android  SDK   •  Hello  World!   •  Main  Building  Blocks   •  Android  User  Interface   •  OperaIng  System  Features   •  Debugging   •  Summary  
  4. 4. ANDROID  STACK  
  5. 5. The  Stack  
  6. 6. Linux  Kernel   Android runs on Linux. Applications Home Contacts Phone Browser Other Linux provides as well as: Hardware abstraction layer Application Framework Memory management Activity Window Content View Process management Manager Manager Providers System Package Telephony Resource Location Notiication Networking Manager Manager Manager Manager Manager Libraries Users never see Linux sub system Surface Media SQLite Android Runtime Manager Framework Core Libs The adb shell command opens OpenGL FreeType WebKit Delvik Linux shell SGL SSL libc VM Display Camera Linux Kernel Flash Binder Driver Driver Driver Driver Keypad WiFi Audio Power Driver Driver Driver Mgmt
  7. 7. NaIve  Libraries   Bionic, a super fast and small Applications license-friendly libc library Home Contacts Phone Browser Other optimized for embedded use Application Framework Surface Manager for composing Activity Window Content View window manager with off-screen Manager Manager Providers System buffering Package Telephony Resource Location Notiication Manager Manager Manager Manager Manager Libraries 2D and 3D graphics hardware Surface Media SQLite Android Runtime support or software simulation Manager Framework Core Libs OpenGL FreeType WebKit Delvik Media codecs offer support for SGL SSL libc VM major audio/video codecs Display Camera Linux Kernel Flash Binder Driver Driver SQLite database Driver Driver Keypad WiFi Audio Power Driver Driver Driver Mgmt WebKit library for fast HTML rendering
  8. 8. Dalvik   Dalvik VM is Google’s implementation of Java VM Optimized for mobile devices Key Dalvik differences: - Register-based versus stack-based VM - Dalvik runs .dex files - More efficient and compact implementation - Different set of Java libraries than SDK
  9. 9. ApplicaIon  Framework   The rich set of system services Applications wrapped in an intuitive Java API. Home Contacts Phone Browser Other This ecosystem that developers Application Framework can easily tap into is what makes Activity Window Content View writing apps for Android easy. Manager Manager Providers System Package Telephony Resource Location Notiication Manager Manager Manager Manager Manager Location, web, telephony, WiFi, Libraries Bluetooth, notifications, media, Surface Manager Media Framework SQLite Android Runtime camera, just to name a few. Core Libs OpenGL FreeType WebKit Delvik VM SGL SSL libc Display Camera Linux Kernel Flash Binder Driver Driver Driver Driver Keypad WiFi Audio Power Driver Driver Driver Mgmt
  10. 10. ApplicaIons   Dalvik Executable + Resources = APK Must be signed (but debug key is okay for development) Many markets with different policies
  11. 11. Android  and  Java   Android Java = Java SE – AWT/Swing + Android API
  12. 12. Android  SDK  -­‐  What’s  in  the  box   SDK Tools Docs Platforms Data Skins Images Samples Add-ons Google
  13. 13. HELLO  WORLD!  
  14. 14. Create  New  Project   Use the Eclipse tool to create a new Android project. Here are some key constructs: Project   Eclipse  construct   Target   minimum  to  run   App  name   whatever   Package   Java  package   AcIvity   Java  class  
  15. 15. The  Manifest  File   <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.marakana" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloAndroid" 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="5" /> </manifest>
  16. 16. The  Layout  Resource   <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="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>
  17. 17. The  Java  File   package com.marakana; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
  18. 18. Running  on  Emulator  
  19. 19. MAIN  BUILDING  BLOCKS  
  20. 20. AcIviIes   Activity is to an Android Application application what a Main Activity Another Another Activity Activity web page is to a website. Sort of.
  21. 21. AcIvity  Lifecycle   Starting Activities have a well- (1) onCreate() (2) onStart() (3) onRestoreInstanceState() defined lifecycle. The (4) onResume() Android OS manages your activity by Running changing its state. (3) onResume() (2) onStart() (1) onSaveInstanceState() (2) onPause() You fill in the blanks. (1) onRestart() onResume() (1) onSaveInstanceState() Stopped (2) onStop() Paused onDestroy() or <process killed> <process killed> Destroyed
  22. 22. Intents   Intents represent Android Application an events or actions. Main Activity Intent Another Activity They are to Intent Android apps what hyperlinks are to Android Application websites. Sort of. Another Main Activity Intent Activity Intents can be implicit or explicit.
  23. 23. Services   Services are code that runs in the background. They can be started and stopped. Services doesn’t have UI.
  24. 24. Service  Lifecycle   Service also has a Starting lifecycle, but it’s (1) onCreate() much simpler than (2) onStart() activity’s. onStart() An activity typically Stopped Running starts and stops a service to do some onStop() work for it in the background, such as onDestroy() or play music, check for <process killed> Destroyed new tweets, etc.
  25. 25. Content  Providers   Content Providers share Content content with applications Provider across application Content URI boundaries. insert() Examples of built-in update() Content Providers are: delete() Contacts, MediaStore, query() Settings and more.
  26. 26. Broadcast  Receivers   An Intent-based publish-subscribe mechanism. Great for listening system events such as SMS messages.
  27. 27. MyTwiVer  –  A  Real  World  App   Update list MyTwitter Preference Timeline Activity Timeline Activity Activity Receiver Read/write Notify of preferences new status Update ListView Read Prefs Prefs XML Timeline Updates Status via Adapter web service Updater Read Prefs Service Pull timeline from DB Insert Pull timeline updates updates via in DB web service Start at Twitter.com Timeline boot DB Boot Receiver
  28. 28. ANDROID  USER  INTERFACE  
  29. 29. Two  UI  Approaches   Procedural   Declara?ve   You  write  Java  code   You  write  XML  code   Similar  to  Swing  or  AWT   Similar  to  HTML  of  a  web  page   You can mix and match both styles. Declarative is preferred: easier and more tools
  30. 30. XML-­‐Based  User  Interface   Use WYSIWYG tools to build powerful XML-based UI. Easily customize it from Java. Separate concerns.
  31. 31. Dips  and  Sps   px  (pixel)   Dots  on  the  screen   in  (inches)   Size  as  measured  by  a  ruler   mm  (millimeters)   Size  as  measured  by  a  ruler   pt  (points)   1/72  of  an  inch   dp  (density-­‐independent  pixel)   Abstract  unit.  On  screen  with  160dpi,   1dp=1px   dip   synonym  for  dp  and  ocen  used  by  Google   sp   Similar  to  dp  but  also  scaled  by  users  font   size  preference  
  32. 32. Views  and  Layouts   ViewGroup ViewGroup View View View View Layouts contain other Views, or other Layouts.
  33. 33. Common  UI  Components   Android UI includes many common modern UI widgets, such as Buttons, Tabs, Progress Bars, Date and Time Pickers, etc.
  34. 34. SelecIon  Components   Some UI widgets may be linked to zillion pieces of data. Examples are ListView and Spinners (pull-downs).
  35. 35. Adapters   Adapter Data Source To make sure they run smoothly, Android uses Adapters to connect them to their data sources. A typical data source is an Array or a Database.
  36. 36. Complex  Components   Certain high-level components are simply available just like Views. Adding a Map or a Video to your application is almost like adding a Button or a piece of text.
  37. 37. Menus  and  Dialogs  
  38. 38. Graphics  &  AnimaIon   Android has rich support for 2D graphics. You can draw & animate from XML. You can use OpenGL for 3D graphics.
  39. 39. MulImedia   AudioPlayer lets you simply specify the audio resource and play it. VideoView is a View that you can drop anywhere in your activity, point to a video file and play it. XML: <VideoView android:id="@+id/video" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center” /> Java: player = (VideoView) findViewById(R.id.video); player.setVideoPath("/sdcard/samplevideo.3gp"); player.start();
  40. 40. OPERATING  SYSTEM  FEATURES    
  41. 41. Security   Each Android application Android Application runs inside its own Linux process. Linux Process Additionally, each application has its own sandbox file File system with its own set of Prefs DB System preferences and its own database. Other applications cannot access any of its data, unless it is explicitly shared.
  42. 42. File  System   The file system has three main mount points. One for system, one for the apps, and one for whatever. Each app has its own sandbox easily accessible to it. No one else can access its data. The sandbox is in /data/data/com.marakana/ SDCard is expected to always be there. It’s a good place for large files, such as movies and music. Everyone can access it.
  43. 43. Cloud  to  Device  Push   Big deal for many pull-based apps. Will make devices use less battery.
  44. 44. Preferences   Your app can support complex preferences quite easily. You define your preferences in an XML file and the corresponding UI and data storage is done for free.
  45. 45. SQLite  Database   Android ships with SQLite3 SQLite is Zero configuration Serverless Single database file Cross-Platform Compact Public Domain Database engine. May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give.
  46. 46. DEBUGGING     ANDROID  APPS  
  47. 47. LogCat   The universal, most versatile way to track what is going on in your app. Can be viewed via command line or Eclipse. Logs can be generated both from SDK Java code, or low-level C code via Bionic libc extension.
  48. 48. Debugger   Your standard debugger is included in SDK, with all the usual bells & whistles.
  49. 49. TraceView   TraceView helps you profile you application and find bottlenecks. It shows execution of various calls through the entire stack. You can zoom into specific calls.
  50. 50. Hierarchy  Viewer   Hierarchy Viewer helps you analyze your User Interface. Base UI tends to be the most “expensive” part of your application, this tool is very useful.
  51. 51. Summary   Android is open and complete system for mobile development. It is based on Java and augmented with XML. Android is being adopted very quickly both by users, carriers, and manufacturers. It takes about 3-5 days of intensive training to learn Android application development for someone who has basic Java (or similar) experience. Marko Gargenta, Marakana.com marko@marakana.com +1-415-647-7000 Licensed under Creative Commons License (cc-by-nc-nd) – non-commercial. Please Share!

×