android deep dive

57
Marko Gargenta Android Deep Dive Sprint Dev Con 2010

Upload: marko-gargenta

Post on 15-May-2015

2.959 views

Category:

Technology


0 download

DESCRIPTION

Sprint Developer Conference 2010

TRANSCRIPT

Page 1: Android Deep Dive

Marko  Gargenta  

Android  Deep  Dive  Sprint  Dev  Con  2010  

Page 2: Android Deep Dive

About  Marko  Gargenta  Developed Android Bootcamp for Marakana.

Taught 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

Page 3: Android Deep Dive

Agenda  •  The  Stack  •  Hello  World!  

•  Main  Building  Blocks  

•  Android  User  Interface  •  OperaFng  System  Features  

•  Debugging  and  Tools  •  Summary  

Page 4: Android Deep Dive

ANDROID  STACK  

Page 5: Android Deep Dive

The  Stack  

Page 6: Android Deep Dive

Linux  Kernel  Android runs on Linux.

Linux provides: Hardware abstraction layer Memory management Process management Networking

Users never see Linux sub system

The adb shell command opens Linux shell

Linux Kernel

Libraries

Application Framework

Applications

Home Contacts Phone Browser Other

Activity Manager

Window Manager

ContentProviders

View System

PackageManager

TelephonyManager

Resource Manager

LocationManager

NotiicationManager

Surface Manager

OpenGL

SGL

Media Framework

FreeType

SSL

SQLite

WebKit

libc

Android Runtime

Core Libs

Delvik VM

DisplayDriver

KeypadDriver

CameraDriver

WiFiDriver

FlashDriver

AudioDriver

BinderDriver

PowerMgmt

Page 7: Android Deep Dive

NaFve  Libraries  Pieces borrowed from other open source projects:

Bionic, a super fast and small license-friendly libc library optimized for Android

WebKit library for fast HTML rendering

OpenGL for graphics

Media codecs offer support for major audio/video codecs

SQLite database

Much more…

Linux Kernel

Libraries

Application Framework

Applications

Home Contacts Phone Browser Other

Activity Manager

Window Manager

ContentProviders

View System

PackageManager

TelephonyManager

Resource Manager

LocationManager

NotiicationManager

Surface Manager

OpenGL

SGL

Media Framework

FreeType

SSL

SQLite

WebKit

libc

Android Runtime

Core Libs

Delvik VM

DisplayDriver

KeypadDriver

CameraDriver

WiFiDriver

FlashDriver

AudioDriver

BinderDriver

PowerMgmt

Page 8: Android Deep Dive

Dalvik  

Dalvik VM is Android implementation of Java VM

Dalvik is optimized for mobile devices: •  Battery consumption •  CPU capabilities

Key Dalvik differences: •  Register-based versus stack-based VM •  Dalvik runs .dex files •  More efficient and compact implementation •  Different set of Java libraries than JDK

Page 9: Android Deep Dive

ApplicaFon  Framework  The rich set of system services wrapped in an intuitive Java API.

This ecosystem that developers can easily tap into is what makes writing apps for Android easy.

Location, web, telephony, WiFi, Bluetooth, notifications, media, camera, just to name a few.

Linux Kernel

Libraries

Application Framework

Applications

Home Contacts Phone Browser Other

Activity Manager

Window Manager

ContentProviders

View System

PackageManager

TelephonyManager

Resource Manager

LocationManager

NotiicationManager

Surface Manager

OpenGL

SGL

Media Framework

FreeType

SSL

SQLite

WebKit

libc

Android Runtime

Core Libs

Delvik VM

DisplayDriver

KeypadDriver

CameraDriver

WiFiDriver

FlashDriver

AudioDriver

BinderDriver

PowerMgmt

Page 10: Android Deep Dive

ApplicaFons  

Dalvik Executable + Resources = APK Must be signed (but debug key is okay for development) Many markets with different policies

Page 11: Android Deep Dive

Android  and  Java  Android Java

= Java SE

– AWT/Swing

+ Android API

Page 12: Android Deep Dive

Android  SDK  -­‐  What’s  In  The  Box  

SDK

Tools Docs Platforms

Data Skins Images Samples

Add-ons Google

Page 13: Android Deep Dive

HELLO  WORLD!  

Page 14: Android Deep Dive

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  

AcFvity   Java  class  

Page 15: Android Deep Dive

Anatomy  of  An  App  

Java Code +

XML and Other Resources

+ Manifest File

= Android App

Page 16: Android Deep Dive

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>

Page 17: Android Deep Dive

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>

Page 18: Android Deep Dive

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); } }

Page 19: Android Deep Dive

Running  on  Emulator  

Emulator, not a simulator

Page 20: Android Deep Dive

MAIN  BUILDING  BLOCKS  

Page 21: Android Deep Dive

AcFviFes  Android Application

Main Activity AnotherActivity

AnotherActivity

An Activity represents a screen or a window. Sort of.

Page 22: Android Deep Dive

AcFvity  Lifecycle  Starting

Running

PausedStopped

Destroyed

(1) onSaveInstanceState()(2) onPause()

(3) onResume()(2) onStart()

(1) onRestart() onResume()

(1) onSaveInstanceState()(2) onStop()

<process killed>

onDestroy()or

<process killed>

(1) onCreate()(2) onStart()

(3) onRestoreInstanceState()(4) onResume()

Activities have a well-defined lifecycle. The Android OS manages your activity by changing its state. You fill in the blanks.

Page 23: Android Deep Dive

Intents  Android Application

AnotherActivity

Android Application

Main Activity

Intent

Intent

Main Activity

Intent AnotherActivity

Intents represent events or actions.

They are to Android apps what hyperlinks are to websites. Sort of.

Intents can be implicit or explicit.

Page 24: Android Deep Dive

Services  Services are code that runs in the background. They can be started and stopped. Services doesn’t have UI.

Page 25: Android Deep Dive

Service  Lifecycle  Service also has a lifecycle, but it’s much simpler than activity’s.

An activity typically starts and stops a service to do some work for it in the background, such as play music, check for new tweets, etc.

Services can be bound or unbound.

Page 26: Android Deep Dive

Content  Providers  

Content Provider

Content URI

insert()

update()

delete()

query()

Content Providers share content with applications across application boundaries. Examples of built-in Content Providers are: Contacts, MediaStore, Settings and more.

Page 27: Android Deep Dive

Broadcast  Receivers  

An Intent-based publish-subscribe mechanism. Great for listening system events such as SMS messages.

Page 28: Android Deep Dive

Twitter.com

MyTwitterActivity

UpdaterService

TimelineReceiver

TimelineDB

PrefsXML

UpdatesStatus via

web service

PreferenceActivity

Pull timelineupdates viaweb service

Insertupdates

in DB

Notify of new status

TimelineActivity

Pull timelinefrom DB

Update list

TimelineAdapter

Update ListViewRead/writepreferences

BootReceiver

Start atboot

ReadPrefs

ReadPrefs

Yamba  –  A  Real  World  App  

Page 29: Android Deep Dive

ANDROID  USER  INTERFACE  

Page 30: Android Deep Dive

Two  UI  Approaches  Procedural   DeclaraCve  

You  write  Java  code  Similar  to  Swing  or  AWT  

You  write  XML  code  Similar  to  HTML  of  a  web  page  

You can mix and match both styles. Best practice:

•  Start with XML and declare most of UI •  Switch to Java and implement the UI logic

Page 31: Android Deep Dive

XML-­‐Based  User  Interface  

Use WYSIWYG tools to build powerful XML-based UI. Easily customize it from Java. Separate concerns.

Page 32: Android Deep Dive

Common  View  ProperFes  

Layout  Width   FILL_PARENT,  WRAP_CONTENT or  a  specific  size  in  units.  

Layout  Height   FILL_PARENT,  WRAP_CONTENT or  a  specific  size  in  units.  

Layout  Weight   How  greedy  or  generous  a  widget  is.  Value  is  0  to  1.  

Layout  Gravity   How  to  align  widget  within  parent  

Gravity   How  to  align  content  of  widget  within  itself  

Id   Unique  id  of  this  widget,  usually  @+id/someUniqueId!

Text   Text  for  the  content  of  the  component,  usually  @string/myText!

Page 33: Android Deep Dive

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  oeen  used  by  Google  

sp   Similar  to  dp  but  also  scaled  by  users  font  size  preference  

Page 34: Android Deep Dive

Views  and  Layouts  

Layouts contain widgets and other layouts forming a “composite” pattern.

Page 35: Android Deep Dive

Linear  Layout  

One of the most commonly used layouts. It lays its children next to each other, either horizontally or vertically.

Page 36: Android Deep Dive

RelaFve  Layout  

Children of relative layout are placed in relationship to each other. This layout is efficient.

Page 37: Android Deep Dive

Table  Layout  

Table layout puts its children into table rows and columns. It is similar to an HTML table.

Page 38: Android Deep Dive

Frame  Layout  

Frame layout places its children on top of each other, like a deck of cards. It is useful for widgets such as tabs or as a placeholder for views added programmatically.

Page 39: Android Deep Dive

Common  UI  Components  

Android UI includes many common modern UI widgets, such as Buttons, Tabs, Progress Bars, Date and Time Pickers, etc.

Page 40: Android Deep Dive

SelecFon  Components  

Some UI widgets may be linked to zillion pieces of data. Examples are ListView and Spinners (pull-downs).

Page 41: Android Deep Dive

Adapters  

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.

Data Source

Adapter

Page 42: Android Deep Dive

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.

Page 43: Android Deep Dive

Menus  and  Dialogs  

Page 44: Android Deep Dive

Graphics  &  AnimaFon  

Android has rich support for 2D graphics. You can draw & animate from XML. You can use OpenGL for 3D graphics.

Page 45: Android Deep Dive

MulFmedia  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();

Page 46: Android Deep Dive

OPERATING  SYSTEM  FEATURES    

Page 47: Android Deep Dive

Security  

Android Application

PrefsDBFile

System

Linux Process

Each Android application runs inside its own Linux process.

Additionally, each application has its own sandbox file system with its own set of preferences and its own database.

Other applications cannot access any of its data, unless it is explicitly shared.

Page 48: Android Deep Dive

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.

Page 49: Android Deep Dive

Cloud  to  Device  Push  

Big deal for many pull-based apps. Will make devices use less battery.

Page 50: Android Deep Dive

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.

Page 51: Android Deep Dive

SQLite  Database  Android ships with SQLite3

SQLite is a

•  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.

Page 52: Android Deep Dive

DEBUGGING    ANDROID  APPS  

Page 53: Android Deep Dive

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.

Page 54: Android Deep Dive

Debugger  

Your standard debugger is included in SDK, with all the usual bells & whistles.

Page 55: Android Deep Dive

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.

Page 56: Android Deep Dive

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.

Page 57: Android Deep Dive

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.

Licensed under Creative Commons License (cc-by-nc-nd) – non-commercial. Please Share!

Marko Gargenta, Marakana.com [email protected] +1-415-647-7000