android quick start gtug

81
Android: Quick Start Dmitry Lukashev

Upload: vashisth4u

Post on 21-Apr-2015

320 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Android Quick Start Gtug

Android: Quick Start

Dmitry Lukashev

Page 2: Android Quick Start Gtug

What is the plan?

• History & Architecture overview

• Android building blocks

• Manifest, system services, NDK

• Activities & Fragments

• Layouts

• Debugging tools

• Publishing & money

Page 3: Android Quick Start Gtug

Where to start

• http://d.android.com/ – developers portal

• http://tools.android.com/ – tools project

• http://s.android.com/ – source page

• http://b.android.com/ – bugz

• http://r.android.com/ – source patches reviews

• http://a.android.com/ – ADK

• http://android-developers.blogspot.com/

Page 4: Android Quick Start Gtug

Where to start (cont.)

Page 6: Android Quick Start Gtug

Training 2010

Part I Part II Part III

Android UI

Layouts, ListView,

Menu, Dialog, Widgets, Tips & tricks, etc.

Android in Action

Screen rotation, Memory analyze, AIDL, SAX, Debug,

Wakelock, etc.

Java +Android (basics)

JVM, GC, Threads, SDK, NDK, Activity,

Code style, etc.

http://android.amberfog.com/?p=655

Page 8: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 10: Android Quick Start Gtug

Android History

Sep 2008

1.0 Mar 2009

1.1 1.5

May 2009

1.6

Sep 2009

2.0

Oct 2009

2.0.1

Dec 2009

2.1

Jan 2010

2.2

May 2010

Cupcake Donut

Éclair

Froyo

• Jul 2005 – Google acquired Android Inc. (co-founded by Andy Rubin)

• Nov 2007 – Google Android presentation and “early look” release

"Astro" and "Bender"

Soft Keyboard Widgets

Voice Search WVGA

Accounts Multi-touch

Live wallpapers

HTC G1

HTC Magic

Nexus One JIT, C2DM, Flash Tethering, V8

Page 11: Android Quick Start Gtug

Android History (cont.)

2.3

Dec 2010

Gingerbread

UI, NFC, SIP, VP8, gyroscope

Nexus S

3.0

Feb 2011

Honeycomb

Tablets

3.1

ADT – ported to 2.3.4 Resizable widgets

May 2011

3.2

May 2011

Comparability display mode

4.0

Oct 2011

Ice Cream Sandwich

New UI virtual buttons Android Beam Phone + Tablet

Roboto

Jellybean?

Page 12: Android Quick Start Gtug

Android Platform Fragmentation in the World

February 2012

Page 13: Android Quick Start Gtug

Android Platform Fragmentation in Russia

Page 14: Android Quick Start Gtug

Java Reflection

• Originally used to inspect classes, interfaces, fields and methods at runtime, without knowing the names of them at compile time. It can be used for observing and/or modifying program execution at runtime

• Classes: Class, Method, Field, Constructor, etc.

// Without reflection

Foo foo = new Foo();

foo.hello();

// With reflection

Class cls = Class.forName("Foo");

Object foo = cls.newInstance();

Method method = cls.getMethod("hello", null);

method.invoke(foo, null);

Page 15: Android Quick Start Gtug

Android Architecture Overview

Linux Kernel

Display Driver

Keypad Driver

Camera Driver

WiFi Driver

Flash Memory Driver

Audio Drivers

Binder (IPC) Driver

Power Management

Libraries

Surface Manager Media Framework

OpenGL | ES FreeType

SQLite

WebKit

SGL SSL libc

Android Runtime

Core Libraries

Dalvik VM

Application Framework

Activity Manager

Package Manager

Window Manager

Telephony Manager

Content Providers

Notification Manager

View System

Location Manager

Resource Manager

Applications

Home Contacts Phone Browser …

ND

K /

JN

I

Page 16: Android Quick Start Gtug

JVM

The pc register (program counter)

JVM Stack

Native methods stacks

Heap

Method Area VM

Thread

Сlass Runtime Constant Pool

class files Class loader subsystem

native method libraries

Execution engine

Native method interface

Page 17: Android Quick Start Gtug

Dalvik VM

• Was written by Dan Bornstein

• Transform class files into DEX

• It is VM… – integrated with Linux

– uses shared memory, mmap

– for OS without swap space

– while powered by a battery

– zygote

• The Dalvik VM is register-based: fewer instructions, code units, instructions

• Verification & optimization at installation time

Give me your huddled bytecodes

yearning to run free. And I lift

the light beside the coder’s door

Page 18: Android Quick Start Gtug

DEX file – shared constant pool

.jar file

.dex file heterogeneous constant pool

other data

.class file

heterogeneous constant pool

other data

.class file

heterogeneous constant pool

other data

.class file

string_ids constant pool

type_ids constant pool

proto_ids constant pool

field_ids constant pool

method_ids constant pool

other data

“Hello World” “Lcom/data/Data”

int String[ ]

String fn() void fn(int)

String.offset Integer.MAX_VALUE

PrintStream.println(…) Collection.size()

Page 19: Android Quick Start Gtug

JIT (since Android 2.2)

• Translates byte code to optimized native code at run time

• Part of the open source

• Trace JIT vs Method JIT

• Trace JIT – Minimizing memory usage critical for mobile devices (100K)

– Important to deliver performance boost quickly

– Trace request is built during interpretation

– Compiled traces chained together in translation cache

– Per process translation cache

• Leave open the possibility of supplementing with method-based JIT

Page 20: Android Quick Start Gtug

Garbage collection

Page 21: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 22: Android Quick Start Gtug

Android Application Building Blocks

• Activity – Presents UI – Each Activity is independent screen – Activity: setContentView(View)

• Service – Used for background operations

• BroadcastReceiver – Receive and process broadcast system or user events

• ContentProvider – Share application data with others – Data can be stored in FS, DB or even in memory – Communicate via ContentResolver

• Intent – Used to activate Activities, Services & BroadcastReceivers

Page 23: Android Quick Start Gtug

Android Application

• Controls global Application state

• Extend Application class (android.app.Application)

– onCreate()

– onLowMemory()

– onTerminate()

– getApplicationContext() – to use it in classes, where is no Context

• Point custom Application class in AndroidManifest.xml

Page 24: Android Quick Start Gtug

Android Context

Application Context • Non UI Context

• startActivity(Intent)

• start /stopService(Intent)

• sendBroadcast(Intent)

• register / unregisterReciever()

• Application FS, Preferences, Resources

• getSystemService

Activity Context • Same as for Application, but specific

to current Activity

• startActivityForResult(Intent) / finish()

• bindService(Intent)

• UI: setContentView(View), findViewById()

• User events handling (Keys, Touches, Trackball)

Page 25: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 26: Android Quick Start Gtug

AndroidManifest.xml (1)

<?xml version="1.0" encoding="utf-8"?>

<manifest

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.my.example"

android:versionName="1.0 beta" android:versionCode="2">

<application android:name=".MyApplication"

android:label="..." android:icon="...">

<activity android:name=".MyActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.DEFAULT" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service ... />

<receiver ... />

<provider ... />

</application>

</manifest>

Page 27: Android Quick Start Gtug

AndroidManifest.xml (2)

<activity android:name=".MyActivity"

android:launchMode="singleTask"

android:theme="@style/Theme.MyDialog" />

<service android:name=".MyService" android:process="new"/>

<receiver android:name=".MyReceiver" >

<intent-filter>

<action android:name= "android.intent.action.PACKAGE_REMOVED" />

<category android:name= "android.intent.category.DEFAULT" />

<data android:scheme= "package" />

</intent-filter>

</receiver>

<provider android:name=".MyProvider"

android:authorities="com.my.provider" />

Page 28: Android Quick Start Gtug

AndroidManifest.xml (3)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="..." package="com.my.example"

android:versionName="1.0 beta" android:versionCode="2">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-sdk

android:minSdkVersion="3"

android:targetSdkVersion="4"/>

<supports-screens

android:largeScreens="true"

android:normalScreens="true"

android:smallScreens="true"

android:resizeable="true"

android:anyDensity="true" />

<application ...> ... </application>

</manifest>

Page 29: Android Quick Start Gtug

AndroidManifest.xml (4)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="..." package="com.my.example"

android:versionName="1.0 beta" android:versionCode="2">

<uses-configuration android:reqHardKeyboard="true"

android:reqTouchScreen="true" />

<uses-feature android:name="android.software.live_wallpaper" />

<uses-feature android:name="android.hardware.telephony" />

<uses-feature android:name="android.hardware.telephony.cdma" />

...

</manifest>

Page 30: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 31: Android Quick Start Gtug

Android System Services

Object getApplicationContext().getSystemService(String serviceName)

serviceName value (contsants in Context)

Service Class name Description

WINDOW_SERVICE WindowManager Controls on-screen windows and their parameters

LAYOUT_INFLATER_SERVICE LayoutInflater Inflates layout resources

POWER_SERVICE PowerManager Controls power management

NOTIFICATION_SERVICE NotificationManager Status bar notifications

CONNECTIVITY_SERVICE ConnectivityManager Handling network connections

WIFI_SERVICE WifiManager Handling Wi-Fi network status

TELEPHONY_SERVICE TelephonyManager Handling phone calls states

LOCATION_SERVICE LocationManager Controls location (GPS) updates

SENSOR_SERVICE SensorManager Controls sensor

… … …

Page 32: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 33: Android Quick Start Gtug

Saving State of Android Application

• Shared Preferences are stored in the application private data space and can be shared only inside this Application, but between launches and versions

• Instance of SharedPreferences class should be obtained:

• To read:

• To write:

• SharedPreferences.OnSharedPreferenceChangeListener

SharedPreferences.Editor editor = mPreferences.edit();

editor.putType(String key, T value);

editor.commit();

Activity.getPreferences()

PreferenceManager.getDefaultSharedPreferences(Context ctx)

Context.getSharedPreferences(String name, int mode)

mPreferences.getType(String key, T defValue);

Page 34: Android Quick Start Gtug

Backup Application Data (android.app.backup – 2.2)

• Perform backup arbitrary data to remote “cloud” storage • Easily perform backup of SharedPreferences and files • Restore the data saved to remote storage • Controlled by Android Backup Manager

– Extend class BackupAgent and override onBackup() & onRestore() OR – Extend BackupAgentHelper to backup/restore SharedPreferences and

files from internal storage – Add your agent to AndroidManifest.xml

• BackupManager.dataChanged()/requestRestore() • New bmgr tool for testing

http://developer.android.com/guide/developing/tools/bmgr.html • Full guide with examples:

http://developer.android.com/guide/topics/data/backup.html

<application android:backupAgent=".MyBackupAgent" >

Page 35: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 36: Android Quick Start Gtug

Android NDK (Native Development Kit)

• Provides ability and tools to embed components that make use of native code in Android applications

• Supports only restricted set of native libraries: – libc (C library) headers

– libm (math library) headers

– JNI interface headers

– libz (Zlib compression) headers

– liblog (Android logging) header

– OpenGL ES 1.1 (since 1.6) and OpenGL ES 2.0 (3D graphics libraries, since 2.0) headers

– libjnigraphics (Pixel buffer access) header (since 2.2)

– A Minimal set of headers for C++ support

• NativeActivity (since 2.3) • For Windows Cygwin 1.7 (or higher) is needed

Page 37: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 38: Android Quick Start Gtug

Activities and Tasks (1)

• An Activity is a “molecule” – a discrete chunk of functionality

• A Task is a collection of Activities (own UI history stack, capable of spanning multiple processes) – is what the user experiences as an "application"

• A Process is a standard Linux process (one VM per process)

Page 39: Android Quick Start Gtug

Activities and Tasks (2)

.apk package .apk package

Process

Service

Process

ContentProvider

Service

Process

Activity Activity Task

Activity

Activity

ContentProvider

Task

Page 40: Android Quick Start Gtug

Android Process

• Android process == Linux process

• adb shell ps -t

• By default: 1 process per APK, 1 thread per process

• Remains running until killed by the system

• The component elements — <activity>, <service>, <receiver>, and <provider> — each have a process attribute that can specify a process where that component should run

• Private process – new thread, same VM

USER PID PPID VSIZE RSS WCHAN PC NAME

app_21 182 30 105984 19188 ffffffff afe0da04 S com.android.email

app_21 183 182 105984 19188 c005925c afe0da04 S HeapWorker

app_21 184 182 105984 19188 c0047438 afe0d32c S Signal Catcher

app_21 188 182 105984 19188 c009a694 afe0cba4 S JDWP

app_21 190 182 105984 19188 c019a810 afe0ca7c S Binder Thread #

app_21 191 182 105984 19188 c019a810 afe0ca7c S Binder Thread #

Page 41: Android Quick Start Gtug

Activities and Tasks (3)

Task

Start Activity (e.g. LAUNCHER)

Activity 1 (1)

GMap Activity

Activity 1 (2)

BACK

HOME

FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_RESET_TASK_IF_NEEDED FLAG_ACTIVITY_SINGLE_TOP

Intent flags

taskAffinity launchMode allowTaskReparenting clearTaskOnLaunch alwaysRetainTaskState finishOnTaskLaunch

<activity> attributes

Page 42: Android Quick Start Gtug

New Task

Task

Activity 1

Activity 2 BACK

HOME

taskAffinity=com.gtug.task1

Intent intent = new Intent(TestUI.this, Activity1.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

NEW_TASK

Task

Activity 3 affinity=com.gtug.task2

BACK

HOME

taskAffinity=com.gtug.task2

Page 43: Android Quick Start Gtug

Task reparenting

android:allowTaskReparenting=["true" | "false"]

Task

Activity 1

Activity 2 allowTaskReparenting=true

affinity=com.gtug.task2

Intent w/o NEW_TASK

taskAffinity=com.gtug.task1

No Task with affinity=com.gtug.task2

Task

Activity 1

Intent w/o NEW_TASK

taskAffinity=com.gtug.task1

Task

Activity 2 allowTaskReparenting=true

affinity=com.gtug.task2

taskAffinity=com.gtug.task2

Activity 2

Page 44: Android Quick Start Gtug

View Activity/Task/Process details

• adb shell dumpsys >> dump.txt

Activities in Current Activity Manager State:

* TaskRecord{43ddd1e8 #10 A com.test.xxx} clearOnBackground=false numActivities=1 rootWasReset=false affinity=com.test.xxx intent={flg=0x10000000 cmp=com.test.TestUI/.Activity1} realActivity=com.test.TestUI/.Activity1 lastActiveTime=15088595 (inactive for 5s)

* Hist #4: HistoryRecord{43ddd000 com.test.TestUI/.Activity1} packageName=com.test.TestUI processName=com.gtug.test123123

launchedFromUid=10025 app=ProcessRecord{43d5a1a8 293:com.gtug.test123123/10025} Intent { flg=0x10000000 cmp=com.test.TestUI/.Activity1 } frontOfTask=true task=TaskRecord{43ddd1e8 #10 A com.test.xxx}

taskAffinity=com.test.xxx realActivity=com.test.TestUI/.Activity1 base=/data/app/com.test.TestUI.apk/data/app/com.test.TestUI.apk data=/data/data/com.test.TestUI labelRes=0x7f040001 icon=0x7f020000 theme=0x0 stateNotNeeded=false componentSpecified=true isHomeActivity=false configuration={ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18} launchFailed=false haveState=false icicle=null state=RESUMED stopped=false delayedResume=false finishing=false keysPaused=false inHistory=true persistent=false launchMode=0 fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true

Page 45: Android Quick Start Gtug

Activity launch modes (1)

standard

android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]

singleTop

singleInstance

singleTask

Multiple instances in one Task Single instance in one Task

only one Activity in the Task

Page 46: Android Quick Start Gtug

Activity launch modes (2)

Task

Activity 1

standard

Task

Activity 1

Activity 2

Task

Activity 1

Activity 2

Activity 3

Task

Activity 1

Activity 2

Activity 3

Activity 2

singleTop Task

Activity 1

Activity 2

Task

Activity 1

Activity 2

Activity 1

Task

Activity 1

Activity 2

Activity 1

Activity1 is singleTop

onNewIntent() run Activity1

Page 47: Android Quick Start Gtug

Activity launch modes (3)

singleTask

Task

Activity 1

Activity 2

Activity 3

Activity1 is singleTask Task

Activity 1

run Activity1

singleInstance

Activity1 is singleInstance Task1

Activity 1

no more Activities

in this task

Task2

Activity 2

run Activity2

Page 48: Android Quick Start Gtug

Activity Graph of Life

Start

Stop

onCreate() onStart()

onResume()

onRestart()

onDestroy()

onPause()

onStop()

Kill process

Running

partly visible

foreground

no longer visible

foreground

onPostCreate ()

onPostResume()

Page 49: Android Quick Start Gtug

Fragments

Page 50: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 51: Android Quick Start Gtug

Android User Interface

• UI is build using View and ViewGroup

• View is base for widgets

• ViewGroup is base for layouts

• setContentView() – attach the view hierarchy tree to the screen for rendering

• Drawing is two pass process: – measuring pass

– layout pass

ViewGroup

ViewGroup View View

View View View

Page 52: Android Quick Start Gtug

Layouts

• Declare layouts in two ways: – Declare UI elements in XML

– Instantiate layout elements at runtime

• Attributes

• ID – android:id="@+id/my_button“

– android:id=“@id/empty“

– android:icon="@*android:drawable/ic_menu_clear_playlist“

• setContentView(R.layout.main_layout); findViewById(R.id.my_button);

Page 53: Android Quick Start Gtug

Layout Parameters

• layout_something – layout parameter

• Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams

• layout_width and layout_height are required for all view groups

LinearLayout

RelativeLayout View View LinearLayout. LayoutParams

LinearLayout. LayoutParams

View RelativeLayout. LayoutParams

View RelativeLayout. LayoutParams

View RelativeLayout. LayoutParams

LinearLayout. LayoutParams

View v = findViewById(R.id.ImageView01);

LinearLayout.LayoutParams lp =

(LinearLayout.LayoutParams)v.getLayoutParams();

lp.weight = 2f;

v.setLayoutParams(lp);

Page 54: Android Quick Start Gtug

Padding, Margins, wrap_content, fill_parent

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:background="#FFFFFF"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/TextView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="mytext"

android:layout_marginLeft="20px"

android:paddingTop="10px"

android:textColor="#FF000000" />

</LinearLayout>

20px

10px

fill_parent

wrap_content

Page 55: Android Quick Start Gtug

Common Layout Objects

• FrameLayout

• LinearLayout

• TableLayout

• RelativeLayout

• GridLayout (API 14)

• AbsoluteLayout – deprecated (use Frame or Relative)

Page 56: Android Quick Start Gtug

FrameLayout

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<Button

android:text="@+id/Button01"

android:id="@+id/Button01"

android:layout_width="200px"

android:layout_height="200px" />

<Button

android:text="@+id/Button02"

android:id="@+id/Button02"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</FrameLayout>

Page 57: Android Quick Start Gtug

LinearLayout

• Orientation: vertical, horizontal

• Weight attribute – “importance” value of a view. Default weight is zero.

0 0 weight = 1

wrap_content for all views

0 1 1

0 1 1

0 1 2

wrap_content for all views

wrap_content, 0dp, 0dp

wrap_content, 0dp, 0dp

0 1 2

wrap_content for all views

Page 58: Android Quick Start Gtug

TableLayout

• TableLayout positions its children into rows and columns

• stretchColumns – zero-based index of the columns to stretch

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text=“gtug1" android:padding=“5dp" /> <TextView android:text=“gtug2" android:gravity="right" android:padding=“5dp" /> </TableRow> <TableRow> … </TableRow> </TableLayout>

Page 59: Android Quick Start Gtug

RelativeLayout (1)

• RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID)

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="?android:attr/listPreferredItemHeight"

android:padding="6dp">

<ImageView … />

<LinearLayout

android:orientation="vertical" …>

<TextView … />

<TextView … />

</LinearLayout>

</LinearLayout>

way 1

<RelativeLayout ... >

<ImageView

android:id="@+id/icon“ …

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true" … />

<TextView

android:id="@+id/secondLine" ….

android:layout_toRightOf="@id/icon"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true" …/>

<TextView

android:layout_toRightOf="@id/icon"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_above="@id/secondLine"

android:layout_alignWithParentIfMissing="true' … />

</RelativeLayout>

better way

Page 60: Android Quick Start Gtug

RelativeLayout (2)

LinearLayout RelativeLayout

Page 61: Android Quick Start Gtug

GridLayout (API 14)

Page 62: Android Quick Start Gtug

GridLayout (cont.)

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:alignmentMode="alignBounds"

android:columnCount="4"

android:columnOrderPreserved="false"

android:useDefaultMargins="true" >

<TextView

android:layout_columnSpan="4"

android:layout_gravity="center_horizontal"

android:text="Email setup"

android:textSize="32dip" />

<TextView

android:layout_columnSpan="4"

android:layout_gravity="left"

android:text="You can configure email in just a few steps:"

android:textSize="16dip" />

<TextView

android:layout_gravity="right"

android:text="Email address:" />

<EditText

android:ems="10" />

<TextView

android:layout_column="0"

android:layout_gravity="right"

android:text="Password:" />

<EditText

android:ems="8" />

<Space

android:layout_column="0"

android:layout_columnSpan="3"

android:layout_gravity="fill"

android:layout_row="4" />

<Button

android:layout_column="3"

android:layout_row="5"

android:text="Next" />

</GridLayout>

Page 63: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 64: Android Quick Start Gtug

Dealing with UI-thread (1)

• Main application thread

• Responsible for drawing and received UI callbacks

• Long operations on UI thread cause ANR

• State of UI elements should be changed only on UI thread CalledFromWrongThreadException: Only the original thread that created a

view hierarchy can touch its views

• ListView adapters should be filled on UI thread java.lang.IllegalStateException: The content of the adapter has changed

but ListView did not receive a notification. Make sure the content of your

adapter is not modified from a background thread, but only from the UI

thread

Page 65: Android Quick Start Gtug

Dealing with UI-thread (2)

• Activity.runOnUiThread(Runnable)

• View.post(Runnable)

• View.postDelayed(Runnable, long)

• Handler

• AsyncTask

– UI: onPreExecute()

– WR: doInBackground()

– WR: publishProgress() – UI: onProgressUpdate()

– UI: onPostExecute()

• android.app.IntentService

Page 66: Android Quick Start Gtug

Devices and Displays

Page 67: Android Quick Start Gtug

Supporting Multiple Screens

• Screen size: small, normal, large, xlarge

• Screen density: low, medium, high, extra high

• Orientation: landscape, portrait

• Resolution

• Density-independent pixel (dp) px = dp * (dpi / 160)

xlarge screens are at least 960dp x 720dp large screens are at least 640dp x 480dp normal screens are at least 470dp x 320dp small screens are at least 426dp x 320dp

Page 68: Android Quick Start Gtug

Qualifiers

Page 69: Android Quick Start Gtug

Since 3.2

Screen configuration Qualifier values Description

smallestWidth

sw<N>dp sw600dp sw720dp

You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least <N> dps of width available for it UI.

Available screen width

w<N>dp w720dp w1024dp

Specifies a minimum available width in dp units at which the resources should be used—defined by the <N> value.

Available screen height

h<N>dp h720dp h1024dp

Specifies a minimum screen height in dp units at which the resources should be used—defined by the <N> value.

Page 70: Android Quick Start Gtug

Various screen configurations available from emulator

Page 71: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 72: Android Quick Start Gtug

Debugging tools

• Android Debug Bridge (ADB)

• Dalvik Debug Monitor Server (DDMS)

• Traceview

• logcat

• ADT plug-in

• DevTools

• SpareParts

• AXMLPrinter2

Page 73: Android Quick Start Gtug

ADB

• Install/Uninstall apps, port forwarding, scripting, files management

• Shell

• adb shell ls /system/bin

• adb kill-server

$ adb -s emulator-5554 shell # sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db SQLite version 3.3.12 Enter ".help" for instructions .... enter commands, then quit... sqlite> .exit

Page 74: Android Quick Start Gtug

Monkey

• Stress-test your application: generate pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events

• You can write your own script and execute thru telnet

• You can even take screenshots thru command line

$ adb shell monkey -p your.package.name -v 500

press DPAD_CENTER

sleep 4000

tap 290 40

sleep 1000

tap 290 40

sleep 500

Monkey Demo

Page 75: Android Quick Start Gtug

logcat

The priority is one of the following character values, ordered from lowest to highest priority: V — Verbose (lowest priority) D — Debug I — Info W — Warning E — Error F — Fatal S — Silent (highest priority, on which nothing is ever printed)

adb logcat -b radio

Page 76: Android Quick Start Gtug

DDMS

• Threads info

• VM Heap info

• Allocation Tracker

• System info

• Emulator control

• Screen capture

• File Explorer

Demo

Page 77: Android Quick Start Gtug

Performance Analysis: Traceview

• In code: – Debug.startMethodTracing(“path”) // at /sdcard

– Debug.stopMethodTracing()

• Using adb: – adb shell am profile com.gtug.project start /sdcard/filename

– adb shell am profile com.gtug.project stop

• Impact performance! <!– AndroidManifest.xml //-->

<application

android:debuggable="true" ... />

Page 78: Android Quick Start Gtug

Android

• Architecture Overview & History

• Building Blocks

• Manifest

• System Services

• Saving Application State

• NDK

• Activities & Fragments

• Layouts

• UI thread

• Debugging tools

• Application publishing & Money

Page 79: Android Quick Start Gtug

Publishing and $$$?

• Idea

• Design

• https://market.android.com/

• http://www.admob.com/

Page 80: Android Quick Start Gtug

Contacts

Dmitry Lukashev

http://ru.linkedin.com/in/dmitrylukashev

[email protected]

Blog - http://android.amberfog.com/

Page 81: Android Quick Start Gtug

Thank You!

Questions?