android application development at jfokus 2011

269
Android Application Development JFokus 2011 Anders Göransson tisdag den 15 februari 2011

Upload: anders-goeransson

Post on 11-May-2015

4.708 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Android Application Development at JFokus 2011

Android Application Development

JFokus 2011

Anders Göransson

tisdag den 15 februari 2011

Page 2: Android Application Development at JFokus 2011

• Android experience

• None / Little

• Moderate / Experienced

About You

tisdag den 15 februari 2011

Page 3: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 4: Android Application Development at JFokus 2011

• 2005 - Google acquires Android Inc.

• 2007 Sep - Mobile patents filed

• 2007 Nov - Open Handset Alliance

• 2008 Sep - Android 1.0

• 2008 Oct - G1 (HTC Dream)

Android History

tisdag den 15 februari 2011

Page 5: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

tisdag den 15 februari 2011

Page 6: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

I. Market share. The future is Android!

tisdag den 15 februari 2011

Page 7: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

I. Market share. The future is Android!

tisdag den 15 februari 2011

Page 8: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

II. Android will be everywhere!

tisdag den 15 februari 2011

Page 9: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

III. It’s open source, it’s free and it’s powerful

tisdag den 15 februari 2011

Page 10: Android Application Development at JFokus 2011

2011 - Why Develop for Android?

IV. It’s Java!

tisdag den 15 februari 2011

Page 11: Android Application Development at JFokus 2011

Android Platform

tisdag den 15 februari 2011

Page 12: Android Application Development at JFokus 2011

• Each process holds Virtual Machine

• Application runs in a sandbox

• Applications run with distinct system identity

• User Id

• Isolated from other applications and platform

• Android contains an RPC mechanism for IPC

Linux ProcessAndroid Platform

tisdag den 15 februari 2011

Page 13: Android Application Development at JFokus 2011

Process A

Linux ProcessDefault behavior

App A

System startsApp A

Android Platform

tisdag den 15 februari 2011

Page 14: Android Application Development at JFokus 2011

Process B

System startsApp B

Process A

Linux ProcessDefault behavior

App A

System startsApp A

App B

Android Platform

tisdag den 15 februari 2011

Page 15: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

Android Platform

tisdag den 15 februari 2011

Page 16: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

System startsApp B

App AApp B

Android Platform

tisdag den 15 februari 2011

Page 17: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

System startsApp B

App AApp B

- Same Process name- Same User Id - Same signing cert

Android Platform

tisdag den 15 februari 2011

Page 18: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

Android Platform

tisdag den 15 februari 2011

Page 19: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

App startscomponents

App A

Android Platform

tisdag den 15 februari 2011

Page 20: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

App startscomponents

App A

Process B

App startscomponent

App A

Android Platform

tisdag den 15 februari 2011

Page 21: Android Application Development at JFokus 2011

Process A

Linux ProcessConfigured behavior

App A

System startsApp A

App startscomponents

App A

Process B

App startscomponent

App A

IPC

Android Platform

tisdag den 15 februari 2011

Page 22: Android Application Development at JFokus 2011

Android and Java

• Apache Harmony class libraries

• Java 5.0

• Dalvik Virtual Machine

• Memory optimizations

• One VM per application

• .class-files -> .dex-files

• 2.2/Froyo+ includes Just In Time-compilation

• Register based (not stack based)

Android Platform

tisdag den 15 februari 2011

Page 23: Android Application Development at JFokus 2011

Building

.java .class .dex

.jar .apk

Android Platform

tisdag den 15 februari 2011

Page 24: Android Application Development at JFokus 2011

RuntimeAndroid and Java

App

apk

.dexResourcesManifest

tisdag den 15 februari 2011

Page 25: Android Application Development at JFokus 2011

Dalvik VM

OptimizedEnvironment

RuntimeAndroid and Java

App

apk

.dexResourcesManifest

tisdag den 15 februari 2011

Page 26: Android Application Development at JFokus 2011

Linux processLinux properties

- Process Id- User Id- Thread model

Dalvik VM

OptimizedEnvironment

RuntimeAndroid and Java

App

apk

.dexResourcesManifest

tisdag den 15 februari 2011

Page 27: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 28: Android Application Development at JFokus 2011

Android Application

Activity

“Manifest”

Application

AndroidManifest.xml

Activity

Service BroadcastReceiver

ContentProvider

Resources

tisdag den 15 februari 2011

Page 29: Android Application Development at JFokus 2011

Android Application

Activity

“Manifest”

Application

AndroidManifest.xml

Activity

Service BroadcastReceiver

ContentProvider

UI

Backgroundtasks

Eventlistener

Data provider

Appdescriptor

Resources

MediaXML

...

tisdag den 15 februari 2011

Page 30: Android Application Development at JFokus 2011

Android Application

Activity

“Manifest”

Application

AndroidManifest.xml

Activity

Service BroadcastReceiver

ContentProvider

UI

Backgroundtasks

Eventlistener

Data provider

Appdescriptor

= Component

Resources

MediaXML

...

tisdag den 15 februari 2011

Page 31: Android Application Development at JFokus 2011

Minimum

Application

AndroidManifest.xmlActivity

Resources

Android Application

Plain UI application

tisdag den 15 februari 2011

Page 32: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

AndroidManifest.xml<manifest>

    <uses-permission />    <permission />     <uses-sdk />    <uses-configuration />      <uses-feature />      <supports-screens />  

    <application>

        <activity>...</activity>

        <service>...</service>

        <receiver>...</receiver>

        <provider>...</provider>

    </application>

</manifest>

Security

Constraints

Components

Android Application

tisdag den 15 februari 2011

Page 33: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

ApplicationAndroid Application

• Base class for global application state

• Subclass to define behavior

• Accessible from all components

• Started before all components

<manifest> <application> <activity> <service> <receiver> <provider> </application></manifest>

Active

Start

Finish

onCreate()

onTerminate()

tisdag den 15 februari 2011

Page 34: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

ApplicationAndroid Application

• Notified when device configuration changes while the application is running

• Device configuration

• Keyboard open/closed (HW + HW/SW)

• User changes global setting

• Locale, Font, etc.

• Orientation change

• MCC/MNC change

tisdag den 15 februari 2011

Page 35: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

ResourcesAndroid Application

• Multimedia files

• Images

• XML -files

• Layouts

• Animations

• Values (strings, dimensions, colors)

• Menu-definitions

• Preferences

tisdag den 15 februari 2011

Page 36: Android Application Development at JFokus 2011

Resource definitionsAndroid Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

/fade_in.xml

Resources

tisdag den 15 februari 2011

Page 37: Android Application Development at JFokus 2011

Android Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

Bitmaps: *.png, *.jpg

Nine-patches: *.9.png

Shapes: Descriptive drawings commands, *.xml

Layers: Multiple drawables compound to one, *.xml

States: Select one drawable in a list based on state, e.g. Button, *.xml

Levels: Select one drawable in a list based on level, e.g. Battery level, *.xml

Resource definitionsResources

tisdag den 15 februari 2011

Page 38: Android Application Development at JFokus 2011

Android Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

/layout_myactivity.xml

Resource definitionsResources

tisdag den 15 februari 2011

Page 39: Android Application Development at JFokus 2011

Android Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

/strings.xml/colors.xml/dimens.xml/arrays.xml

<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">SpotifySearcher</string> <string name="artist">Artist</string> <string name="search">Search</string> <string name="search_result">Search result:</string></resources>

Resource definitionsResources

tisdag den 15 februari 2011

Page 40: Android Application Development at JFokus 2011

Android Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

/preferences.xml/*.xml

Resource definitionsResources

tisdag den 15 februari 2011

Page 41: Android Application Development at JFokus 2011

Android Application

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw Arbitrary files

Resource definitionsResources

tisdag den 15 februari 2011

Page 42: Android Application Development at JFokus 2011

Providing Resourcespublic final class R { public static final class attr { } public static final class color { public static final int dark_grey=0x7f040001; public static final int white=0x7f040000; } public static final class dimen { public static final int margin_vertical_default=0x7f050001; public static final int padding_default=0x7f050000; public static final int text_size_default=0x7f050002; public static final int text_size_large=0x7f050003; } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int button_search=0x7f070001; public static final int edit_search_input=0x7f070000; public static final int text_search_result=0x7f070002; } public static final class layout { public static final int activity_search=0x7f030000; public static final int divider_line=0x7f030001; } public static final class string { public static final int app_name=0x7f060000; public static final int artist=0x7f060001; public static final int search=0x7f060002; public static final int search_result=0x7f060003; }}

Android Application

Pre-compileActivity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

Resources

tisdag den 15 februari 2011

Page 43: Android Application Development at JFokus 2011

Providing Resourcespublic final class R { public static final class attr { } public static final class color { public static final int dark_grey=0x7f040001; public static final int white=0x7f040000; } public static final class dimen { public static final int margin_vertical_default=0x7f050001; public static final int padding_default=0x7f050000; public static final int text_size_default=0x7f050002; public static final int text_size_large=0x7f050003; } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int button_search=0x7f070001; public static final int edit_search_input=0x7f070000; public static final int text_search_result=0x7f070002; } public static final class layout { public static final int activity_search=0x7f030000; public static final int divider_line=0x7f030001; } public static final class string { public static final int app_name=0x7f060000; public static final int artist=0x7f060001; public static final int search=0x7f060002; public static final int search_result=0x7f060003; }}

Android Application

R.java

Pre-compileActivity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

Resources

tisdag den 15 februari 2011

Page 44: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 45: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

UI

• Programmatic and Descriptive UI

• Declare UI in XML description

• Manipulate UI programmatically

• Separate UI presentation from UI behavior

Java object

Java object

Java object

Java object

Java object

Inflate

<Element><Element><Element>

<Element><Element>

tisdag den 15 februari 2011

Page 46: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

UI

• Programmatic and Descriptive UI

• Declare UI in XML description

• Manipulate UI programmatically

• Separate UI presentation from UI behavior

<ViewGroup><View><ViewGroup>

<View><View>

Java object

Java object

Java object

Inflate

ViewGroup

ViewGroup

tisdag den 15 februari 2011

Page 47: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

UI

• Programmatic and Descriptive UI

• Declare UI in XML description

• Manipulate UI programmatically

• Separate UI presentation from UI behavior

<ViewGroup><View><ViewGroup>

<View><View>

Inflate

ViewGroup

ViewGroup

View

View View

tisdag den 15 februari 2011

Page 48: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

LayoutsUI

• Implements ViewGroup

• Common layouts

• LinearLayout

• RelativeLayout

• TableLayout

tisdag den 15 februari 2011

Page 49: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

LayoutsUI

• Implements ViewGroup

• Common layouts

• LinearLayout

• RelativeLayout

• TableLayout

tisdag den 15 februari 2011

Page 50: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

LayoutsUI

• Implements ViewGroup

• Common layouts

• LinearLayout

• RelativeLayout

• TableLayout

tisdag den 15 februari 2011

Page 51: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

LayoutsUI

• Implements ViewGroup

• Common layouts

• LinearLayout

• RelativeLayout

• TableLayout

tisdag den 15 februari 2011

Page 52: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

LayoutsUI

• Implements ViewGroup

• Common layouts

• LinearLayout

• RelativeLayout

• TableLayout

Inflating objects is expensive. Use as few elements as possible.

Use ‘layoutopt’

Consider

tisdag den 15 februari 2011

Page 53: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Layout Example<?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" android:padding="@dimen/padding_default"> <EditText android:id="@+id/edit_search_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/artist" /> <Button android:id="@+id/button_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/margin_vertical_default" android:layout_marginTop="@dimen/margin_vertical_default" android:text="@string/search" /> <include layout="@layout/divider_line" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_vertical_default" android:textSize="@dimen/text_size_default" android:text="@string/search_result"/> <TextView android:id="@+id/text_search_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:textSize="@dimen/text_size_large"/></LinearLayout>

LinearLayout

EditText ButtonTextView

TextView

UI

tisdag den 15 februari 2011

Page 54: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Drawing UI• Top-down traversal

• Measure

• fill_parent, wrap_content, “fix value”

• Layout

• layout_*

LinearLayout

EditText ButtonTextView

TextView

UI

tisdag den 15 februari 2011

Page 55: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Use Resource Separation<?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" android:padding="@dimen/padding_default"> <EditText android:id="@+id/edit_search_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/artist" /> <Button android:id="@+id/button_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/margin_vertical_default" android:layout_marginTop="@dimen/margin_vertical_default" android:text="@string/search" /> <include layout="@layout/divider_line" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_vertical_default" android:textSize="@dimen/text_size_default" android:text="@string/search_result"/> <TextView android:id="@+id/text_search_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:textSize="@dimen/text_size_large"/></LinearLayout>

UI

tisdag den 15 februari 2011

Page 56: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Use Resource Separation<?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" android:padding="@dimen/padding_default"> <EditText android:id="@+id/edit_search_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/artist" /> <Button android:id="@+id/button_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/margin_vertical_default" android:layout_marginTop="@dimen/margin_vertical_default" android:text="@string/search" /> <include layout="@layout/divider_line" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_vertical_default" android:textSize="@dimen/text_size_default" android:text="@string/search_result"/> <TextView android:id="@+id/text_search_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:textSize="@dimen/text_size_large"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><resources> <color name="white">#ffffff</color> <color name="dark_grey">#222222</color></resources>

colors.xml

UI

tisdag den 15 februari 2011

Page 57: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Use Resource Separation<?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" android:padding="@dimen/padding_default"> <EditText android:id="@+id/edit_search_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/artist" /> <Button android:id="@+id/button_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/margin_vertical_default" android:layout_marginTop="@dimen/margin_vertical_default" android:text="@string/search" /> <include layout="@layout/divider_line" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_vertical_default" android:textSize="@dimen/text_size_default" android:text="@string/search_result"/> <TextView android:id="@+id/text_search_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:textSize="@dimen/text_size_large"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><resources> <color name="white">#ffffff</color> <color name="dark_grey">#222222</color></resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="padding_default">20dp</dimen> <dimen name="margin_vertical_default">20dp</dimen> <dimen name="text_size_default">15sp</dimen> <dimen name="text_size_large">20sp</dimen></resources>

dimens.xml

UI

tisdag den 15 februari 2011

Page 58: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Use Resource Separation<?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" android:padding="@dimen/padding_default"> <EditText android:id="@+id/edit_search_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/artist" /> <Button android:id="@+id/button_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/margin_vertical_default" android:layout_marginTop="@dimen/margin_vertical_default" android:text="@string/search" /> <include layout="@layout/divider_line" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_vertical_default" android:textSize="@dimen/text_size_default" android:text="@string/search_result"/> <TextView android:id="@+id/text_search_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/dark_grey" android:textSize="@dimen/text_size_large"/></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><resources> <color name="white">#ffffff</color> <color name="dark_grey">#222222</color></resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="padding_default">20dp</dimen> <dimen name="margin_vertical_default">20dp</dimen> <dimen name="text_size_default">15sp</dimen> <dimen name="text_size_large">20sp</dimen></resources>

dimens.xml

<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">SpotifySearcher</string> <string name="artist">Artist</string> <string name="search">Search</string> <string name="search_result">Search result:</string></resources>

strings.xml

UI

tisdag den 15 februari 2011

Page 59: Android Application Development at JFokus 2011

• Application component

• Subclassed by application

• User interface

• Typically one full size screen

• Inflates UI layouts

• Key events

• Activity initialization

• onCreate()

Activity

Activity

UI

tisdag den 15 februari 2011

Page 60: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Accessing Resourcespublic class ActivitySearch extends Activity { private EditText editSearch; private TextView textSearchResult; private Button buttonSearch; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); editSearch = (EditText) findViewById(R.id.edit_search_input); buttonSearch = (Button) findViewById(R.id.button_search); textSearchResult = (TextView) findViewById(R.id.text_search_result); }

Activity

1. Inflate layout2. Access by Id

UI

tisdag den 15 februari 2011

Page 61: Android Application Development at JFokus 2011

Activity

AndroidManifest.xml

Accessing Resourcespublic final class R { public static final class attr { } public static final class color { public static final int dark_grey=0x7f040001; public static final int white=0x7f040000; } public static final class dimen { public static final int margin_vertical_default=0x7f050001; public static final int padding_default=0x7f050000; public static final int text_size_default=0x7f050002; public static final int text_size_large=0x7f050003; } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int button_search=0x7f070001; public static final int edit_search_input=0x7f070000; public static final int text_search_result=0x7f070002; } public static final class layout { public static final int activity_search=0x7f030000; public static final int divider_line=0x7f030001; } public static final class string { public static final int app_name=0x7f060000; public static final int artist=0x7f060001; public static final int search=0x7f060002; public static final int search_result=0x7f060003; }}

R.javapublic class ActivitySearch extends Activity { private EditText editSearch; private TextView textSearchResult; private Button buttonSearch; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); editSearch = (EditText) findViewById(R.id.edit_search_input); buttonSearch = (Button) findViewById(R.id.button_search); textSearchResult = (TextView) findViewById(R.id.text_search_result); }

Activity

1. Inflate layout2. Access by Id

UI

tisdag den 15 februari 2011

Page 62: Android Application Development at JFokus 2011

UI

• Commonly used

• Needs to be efficient

• Not consume too much memory

• Fast scrolling

• Android-way of handling lists efficiently

Lists

tisdag den 15 februari 2011

Page 63: Android Application Development at JFokus 2011

UI

The problem

Data 1

Data 2

...

UI Element

ListData source

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Lists

tisdag den 15 februari 2011

Page 64: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data source

UI Lists

tisdag den 15 februari 2011

Page 65: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data source

<interface>Adapter

UI Lists

tisdag den 15 februari 2011

Page 66: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data source

<interface>Adapter...

UI Lists

tisdag den 15 februari 2011

Page 67: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data source

<interface>Adapter...

list_item_layout.xml

UI Lists

tisdag den 15 februari 2011

Page 68: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data source

<interface>Adapter... ...

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

UI Lists

tisdag den 15 februari 2011

Page 69: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data sourceAdapterView

<interface>Adapter... ...

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

UI Lists

tisdag den 15 februari 2011

Page 70: Android Application Development at JFokus 2011

API

Data 1

Data 2

...

Problem RequirementArbitrary data source Abstract mechanismArbitrary data type Look and feel of each elementDynamic data Update list after insert, edit, removeLots of data requires a lot of memory Effective caching

Data sourceAdapterView

<interface>Adapter... ...

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_item_layout.xml

list_layout.xml

UI Lists

tisdag den 15 februari 2011

Page 71: Android Application Development at JFokus 2011

Adapter<interface>

Adapter

SimpleAdapter ArrayAdapter

SimpleCursorAdapter

BaseAdapter

CursorAdapter

UI Lists

tisdag den 15 februari 2011

Page 72: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 73: Android Application Development at JFokus 2011

User Experience

• Critical for application success

• UX

• UI design

• Employ a good designer

• Deliver expected behavior

• Master the Activity lifecycle

• Responsive

• Master the thread handling

tisdag den 15 februari 2011

Page 74: Android Application Development at JFokus 2011

LifecycleUser Experience Activity

Active

Paused

Stopped

States

Foreground

Partly obscured

Completely obscured

tisdag den 15 februari 2011

Page 75: Android Application Development at JFokus 2011

LifecycleUser Experience Activity

Active

Paused

Stopped

States

Foreground

Partly obscured

Completely obscured Activity stack

Active

tisdag den 15 februari 2011

Page 76: Android Application Development at JFokus 2011

LifecycleActivity

Active

Paused

Stopped

States

Foreground

Partly obscured

Completely obscured Activity stack

Stopped

Active

User Experience

tisdag den 15 februari 2011

Page 77: Android Application Development at JFokus 2011

LifecycleActivity

Active

Paused

Stopped

States

Foreground

Partly obscured

Completely obscured Activity stack

Stopped

Paused

Active

User Experience

tisdag den 15 februari 2011

Page 78: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivityUser Experience

tisdag den 15 februari 2011

Page 79: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 80: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume() • System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 81: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 82: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 83: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 84: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 85: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()

• System callbacks

• Override in subclass

User Experience

tisdag den 15 februari 2011

Page 86: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()Entire lifetime

- Long lived objects- Create UI

User Experience

tisdag den 15 februari 2011

Page 87: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()Visible lifetime

- Visible but possibly not in foreground

User Experience

tisdag den 15 februari 2011

Page 88: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

LifecycleActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()Foreground lifetime

- User interaction- Frequent cycle

- Example: Device screen sleep mode- Lightweight code

User Experience

tisdag den 15 februari 2011

Page 89: Android Application Development at JFokus 2011

Activity transitionActivity

onPause()

onStop()

Active

Activity 1

Stopped

Activity stack

I Active

User Experience

Save state in onPause()

Tip

tisdag den 15 februari 2011

Page 90: Android Application Development at JFokus 2011

Activity transitionActivity

onPause()

onStop()

Active

Activity 1

Stopped

Activity 1I

Start

onCreate()

onStart()

onResume()

Active

Activity stack

Stopped

Active

I

II

User Experience

Save state in onPause()

Tip

tisdag den 15 februari 2011

Page 91: Android Application Development at JFokus 2011

Activity transitionActivity

onPause()

onStop()

Active

Activity 1

Stopped

Activity 1I

Start

onCreate()

onStart()

onResume()

Active

Activity stack

Stopped

Active

I

II

Execution path

User Experience

Save state in onPause()

Tip

tisdag den 15 februari 2011

Page 92: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

Lifecycle ExtrasActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()onPostCreate()

onPostResume()

User Experience

tisdag den 15 februari 2011

Page 93: Android Application Development at JFokus 2011

Active

Start

Finish

Paused

Stopped

Lifecycle ExtrasActivity

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()onPostCreate()

onPostResume()

System methods. Normally not used by

application.

User Experience

tisdag den 15 februari 2011

Page 94: Android Application Development at JFokus 2011

Lifecycle changesActivityUser Experience

• The User navigates away from the Activity

• To another Activity

• onStop(), onDestroy() if finish() is called

• BACK-button (Donʼt override unless well motivated)

• onDestroy()

• HOME-button

• onStop()

• Other App in front, e.g. incoming call, onStop()

• Screensaver

• onPause()

tisdag den 15 februari 2011

Page 95: Android Application Development at JFokus 2011

TaskActivityUser Experience

tisdag den 15 februari 2011

Page 96: Android Application Development at JFokus 2011

• System can shut down a process at any time due to low available resources

• The processes are ranked according to importance

• Foreground process

• Visible process

• Service process

• Background process

• Empty process

System CleanupUser Experience

tisdag den 15 februari 2011

Page 97: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

User Experience

tisdag den 15 februari 2011

Page 98: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

- Visible Activity (onPause)- Hosts Service bound to by visible Activity

Visible

User Experience

tisdag den 15 februari 2011

Page 99: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

- Visible Activity (onPause)- Hosts Service bound to by visible Activity

Visible

- Explicitly started Service (startService)

Service

User Experience

tisdag den 15 februari 2011

Page 100: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

- Visible Activity (onPause)- Hosts Service bound to by visible Activity

Visible

- Explicitly started Service (startService)

Service

- Background Activity (onStop)

Background

User Experience

tisdag den 15 februari 2011

Page 101: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

- Visible Activity (onPause)- Hosts Service bound to by visible Activity

Visible

- Explicitly started Service (startService)

Service

- Background Activity (onStop)

Background

- No active components

Empty

User Experience

tisdag den 15 februari 2011

Page 102: Android Application Development at JFokus 2011

Process HierarchySystem Cleanup

- Activity in the foreground (onResume called)- Service executing platform callback (onCreate, onStartCommand, onDestroy)- BroadcastReceiver executing platform callbackForeground

- Visible Activity (onPause)- Hosts Service bound to by visible Activity

Visible

- Explicitly started Service (startService)

Service

- Background Activity (onStop)

Background

- No active components

Empty

Long running tasks in Activity or BroadcastReceiver are best handled in a

Service instead of local Thread. The process is then ranked higher and the

risk of interruption is decreased.

Tip!

User Experience

tisdag den 15 februari 2011

Page 103: Android Application Development at JFokus 2011

Background Process Problem

MyActivity

Some text

Foreground

System CleanupUser Experience

tisdag den 15 februari 2011

Page 104: Android Application Development at JFokus 2011

Background Process Problem

MyActivity

Some text

Foreground

MyActivityOtherActivity

Background

OtherActivitystarts

System CleanupUser Experience

tisdag den 15 februari 2011

Page 105: Android Application Development at JFokus 2011

Background Process Problem

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivityOtherActivity

Background

OtherActivitystarts

System CleanupUser Experience

tisdag den 15 februari 2011

Page 106: Android Application Development at JFokus 2011

Background Process Problem

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivity

Foreground

User presses “Back”

MyActivityOtherActivity

Background

OtherActivitystarts

System CleanupUser Experience

tisdag den 15 februari 2011

Page 107: Android Application Development at JFokus 2011

Background Process Solution

• Activity.onSaveInstanceState(Bundle)

• Called when Activity is destroyed by the system

• Not a lifecycle method

• Data that should be retrieved is set in a Bundle

• put*-methods

System CleanupUser Experience

tisdag den 15 februari 2011

Page 108: Android Application Development at JFokus 2011

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivityOtherActivity

Background

OtherActivitystarts

System CleanupUser Experience

Background Process Solution

tisdag den 15 februari 2011

Page 109: Android Application Development at JFokus 2011

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivityOtherActivity

Background

OtherActivitystarts

@Overrideprotected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(outState); bundle.putString(key, “Some Text”);}

System CleanupUser Experience

Background Process Solution

tisdag den 15 februari 2011

Page 110: Android Application Development at JFokus 2011

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivity

Some text

Foreground

User presses “Back”

MyActivityOtherActivity

Background

OtherActivitystarts

@Overrideprotected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(outState); bundle.putString(key, “Some Text”);}

System CleanupUser Experience

Background Process Solution

tisdag den 15 februari 2011

Page 111: Android Application Development at JFokus 2011

MyActivity

Some text

Foreground

OtherActivity

Killed

System shuts downMyActivity

MyActivity

Some text

Foreground

User presses “Back”

MyActivityOtherActivity

Background

OtherActivitystarts

@Overrideprotected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(outState); bundle.putString(key, “Some Text”);}

@Overrideprotected void onCreate(Bundle bundle) { super.onCreate(bundle); String str = bundle.getString(key); ...}

System CleanupUser Experience

Background Process Solution

tisdag den 15 februari 2011

Page 112: Android Application Development at JFokus 2011

• Each application runs in one Linux process by default

• Single-threaded model - Main / UI thread

• All components and system calls run in the UI thread.

• Blocking the UI thread blocks all components

• UI toolkit not thread-safe

• All UI operations must be made on the UI thread

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 113: Android Application Development at JFokus 2011

Application Not RespondingMaster the thread modelUser Experience

tisdag den 15 februari 2011

Page 114: Android Application Development at JFokus 2011

Process A

Thread ModelMaster the thread modelUser Experience

tisdag den 15 februari 2011

Page 115: Android Application Development at JFokus 2011

Process A

Thread Model

App A

System startsApp A

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 116: Android Application Development at JFokus 2011

Process A

Thread Model

App A

System startsApp A

App startscomponents

App A

UI thread

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 117: Android Application Development at JFokus 2011

Process A

Thread Model

App A

System startsApp A

App startscomponents

App A

UI thread

App startsworker threads

App A

Worker threads

UI thread

...

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 118: Android Application Development at JFokus 2011

Process A

Thread Model

App A

System startsApp A

App startscomponents

App A

UI thread

App startsworker threads

App A

Worker threads

UI thread

...

- Blocks the UI thread- Use only lightweight tasks- All UI manipulations !

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 119: Android Application Development at JFokus 2011

Process A

Thread Model

App A

System startsApp A

App startscomponents

App A

UI thread

App startsworker threads

- Time consuming tasks- No UI manipulations !

App A

Worker threads

UI thread

...

- Blocks the UI thread- Use only lightweight tasks- All UI manipulations !

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 120: Android Application Development at JFokus 2011

Thread Handler API

1. Activity.runOnUiThread(Runnable)

2. View.post(Runnable)

3. Handler public class MyActivity extends Activity {

private TextView textView;

    private void startLongRunningOperation() {

new Thread(new Runnable() {    public void run() {      results = doSomethingExpensive();      runOnUiThread(new Runnable() {        public void run() {          textView.setText(results);        }      });    }  }).start();    }

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 121: Android Application Development at JFokus 2011

Thread Handler API

1. Activity.runOnUiThread(Runnable)

2. View.post(Runnable)

3. Handler private TextView textView;

    private void startLongRunningOperation() {

new Thread(new Runnable() {    public void run() {      results = doSomethingExpensive();      textView.post(new Runnable() {        public void run() {          textView.setText(results);        }      });    }  }).start();    }

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 122: Android Application Development at JFokus 2011

Thread Handler API

1. Activity.runOnUiThread(Runnable)

2. View.post(Runnable)

3. Handler

public class MyActivity extends Activity {

private TextView textView;

    // Need handler for callbacks to the UI thread    final Handler handler = new Handler();

    // Create runnable for posting    final Runnable updateResults = new Runnable() {        public void run() {            // UPDATE UI WITH “results” textView.setText(results);        }    };

    private void startLongRunningOperation() {

        //Fire off a thread to do some work that we shouldn't do //directly in the UI thread        Thread t = new Thread() {            public void run() {                results = doSomethingExpensive();                handler.post(updateResults);            }        };        t.start();    }

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 123: Android Application Development at JFokus 2011

Thread Handler API

1. Activity.runOnUiThread(Runnable)

- Possible to update many UI objects

2. View.post(Runnable)

- Updates one UI object

3. Handler

- Generic mechanism not only UI manipulation

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 124: Android Application Development at JFokus 2011

AsyncTask

• Handles thread management

• Solves common use case

• Code separation

• Subclass

• Create on the UI thread

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 125: Android Application Development at JFokus 2011

StrictMode

• Development API defined added in 2.3/Gingerbread

• Define thread policy for whatʼs allowed

• Detect: Network access, disk reads/writes

• Act: Log, show dialog, crash, dropbox

public void onCreate() {         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()                 .detectDiskReads()                 .detectDiskWrites()                 .detectNetwork()                 .penaltyLog()                 .build());     } }

Master the thread modelUser Experience

tisdag den 15 februari 2011

Page 126: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 127: Android Application Development at JFokus 2011

Intent

• Android message system

• Intent is a declare of need

• “What you want to do?”

• “I want to lookup a contact record”

• “I want to launch this web site”

• “Show the order confirmation screen”

• Activate components

• Activity, Service, BroadcastReceiver

tisdag den 15 februari 2011

Page 128: Android Application Development at JFokus 2011

TypesIntent

Explicit Intent

App A

ComponentA ComponentB

ComponentDComponentC

Intent

ComponentX

tisdag den 15 februari 2011

Page 129: Android Application Development at JFokus 2011

TypesIntent

Explicit Intent

App A

ComponentA ComponentB

ComponentDComponentC

Intent

ComponentX

Implicit Intent

App A

App B App C

Intent

“Aspects of need”

App C

tisdag den 15 februari 2011

Page 130: Android Application Development at JFokus 2011

TypesIntent

Explicit Intent

App A

ComponentA ComponentB

ComponentDComponentC

Intent

ComponentX

Implicit Intent

App A

App B App C

Intent

“Aspects of need”

App C

?

tisdag den 15 februari 2011

Page 131: Android Application Development at JFokus 2011

Types• Explicit intent

• Directed to a specific component in the application

• Compile-time binding

Intent

• Implicit intent

• Directed to any component that can handle the intent

• Run-time binding

tisdag den 15 februari 2011

Page 132: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

tisdag den 15 februari 2011

Page 133: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

• Package name + Class name• Explicit intent• Application internal

tisdag den 15 februari 2011

Page 134: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

• String: Names the action to perform•ACTION_EDIT, ACTION_VIEW, etc.•System defined•Intent class•Application•User defined •Package name + Action name

tisdag den 15 februari 2011

Page 135: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

• URI and/or MIME_TYPE• Specifies the content to act upon

tisdag den 15 februari 2011

Page 136: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

• Kind of component that can handle the intent• CATEGORY_BROWSABLE, etc.

tisdag den 15 februari 2011

Page 137: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags

• Key-Value pairs with extra information

tisdag den 15 februari 2011

Page 138: Android Application Development at JFokus 2011

DefinitionIntent

• Component name

• Action

• Data

• Category

• Extras

• Flags• Typically: Instruct how an Activity should be launched

tisdag den 15 februari 2011

Page 140: Android Application Development at JFokus 2011

IntentFilterIntent

<activity android:name="NotesList" android:label="@string/title_notes_list">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <action android:name="android.intent.action.EDIT" />                <action android:name="android.intent.action.PICK" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />            </intent-filter>        </activity>                <activity android:name="NoteEditor"                  android:theme="@android:style/Theme.Light"                  android:label="@string/title_note" >            <intent-filter android:label="@string/resolve_edit">                <action android:name="android.intent.action.VIEW" />                <action android:name="android.intent.action.EDIT" />                <action android:name="com.android.notepad.action.EDIT_NOTE" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.INSERT" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />            </intent-filter>        </activity>

tisdag den 15 februari 2011

Page 141: Android Application Development at JFokus 2011

Intent OverviewIntent

tisdag den 15 februari 2011

Page 142: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

tisdag den 15 februari 2011

Page 143: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

IntentFilter

1. Install

tisdag den 15 februari 2011

Page 144: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

App A

Component

Intent

(Component)ActionData

CategoryExtrasFlags

IntentFilter

1. Install

tisdag den 15 februari 2011

Page 145: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

App A

Component

Intent

(Component)ActionData

CategoryExtrasFlags

IntentFilter

1. Install

Intent

ActionData

Category

2. Send Intent

tisdag den 15 februari 2011

Page 146: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

App A

Component

Intent

(Component)ActionData

CategoryExtrasFlags

IntentFilter

1. Install

Intent

ActionData

Category

2. Send Intent

3. Intent resolution

tisdag den 15 februari 2011

Page 147: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

App A

Component

Intent

(Component)ActionData

CategoryExtrasFlags

IntentFilter

1. Install

Intent

ActionData

Category

2. Send Intent

3. Intent resolution

4. Start component

tisdag den 15 februari 2011

Page 148: Android Application Development at JFokus 2011

Intent OverviewIntent

App App App

ActivityAComponent

AndroidManifest

IntentFilter

App A

Component

Intent

(Component)ActionData

CategoryExtrasFlags

IntentFilter

1. Install

Intent

ActionData

Category

2. Send Intent

3. Intent resolution

4. Start component

5. Hand over Intent Intent

ActionData

Extras

tisdag den 15 februari 2011

Page 149: Android Application Development at JFokus 2011

Intent ResolutionIntent

• ACTION test (Filter: 1..*)

• Pass: 1 IntentFilter-action matches Intent-action

• CATEGORY test (Filter: 1..*)

• Pass: All Intent-categories matches IntentFilter-categories

• DATA test (Filter: 0..*)

• Uri + MIME-type

• Pass: Intent-data is subset of IntentFilter-data

tisdag den 15 februari 2011

Page 150: Android Application Development at JFokus 2011

Best PracticesIntent

• Only reusable Activities shall specify IntentFilter

• Check if intent can be handled

• Pre-check

• PackageManager.queryIntentActivities()

• Post-check

• Catch exception from startActivity()

tisdag den 15 februari 2011

Page 151: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 152: Android Application Development at JFokus 2011

Content Provider - Data Storage

• File system

• Internal storage & SD Card

• SharedPreferences

• Key/Value pairs

• SQLite DB

tisdag den 15 februari 2011

Page 153: Android Application Development at JFokus 2011

SQLite

• Relational database

• Self-contained

• Transactional

• No need for separate server process

• Stored as a file

• /data/data/com.yourdomain.yourAppPackage/databases

ContentProvider

tisdag den 15 februari 2011

Page 154: Android Application Development at JFokus 2011

• SQLiteDataBase

• SQLiteOpenHelper

• ContentValues

• Cursor

• Container for a query result

Android SQLite API

Cursor cur = db.query("tbl_countries", null, null, null, null, null, null);

cur.moveToFirst();while (cur.isAfterLast() == false) {

//Do somethingcur.moveToNext();

}cur.close();

_id C1 C2

ContentProvider

tisdag den 15 februari 2011

Page 155: Android Application Development at JFokus 2011

ContentProvider

• Generic data handling mechanism for any content type

• Store application data

• Retrieve application data

• Share data between applications

ContentProvider

tisdag den 15 februari 2011

Page 156: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A

DB A

Store/Retrieve bookmarks

tisdag den 15 februari 2011

Page 157: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A

DB A

Store/Retrieve bookmarks

Browser B

DB B

Store/Retrieve bookmarks

tisdag den 15 februari 2011

Page 158: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A

DB A

Store/Retrieve bookmarks

Browser B

DB B

Store/Retrieve bookmarks

Browser C

DB C

Store/Retrieve bookmarks

tisdag den 15 februari 2011

Page 159: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

Use native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 160: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

Use native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 161: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

BookmarkContentProvider Use native content providers

when working with system defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 162: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

BookmarkContentProvider

Store/Retrieve bookmarks

Use native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 163: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

BookmarkContentProvider

Store/Retrieve bookmarks

?

Use native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 164: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

BookmarkContentProvider

Store/Retrieve bookmarks

?

Browser C

DB C

Use native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 165: Android Application Development at JFokus 2011

Motivation - Bookmark exampleContentProvider

Browser A Browser B

BookmarkContentProvider

Store/Retrieve bookmarks

?

Browser C

DB CxUse native content providers when working with system

defined content!

Be a good Android citizen

tisdag den 15 februari 2011

Page 166: Android Application Development at JFokus 2011

Native Content• android.provider.*

• Bookmarks

• CallLog

• Contacts

• Dictionary

• Media

• Audio, Images, Video

• Settings

• Device domain model

ContentProvider

tisdag den 15 februari 2011

Page 167: Android Application Development at JFokus 2011

FundamentalsContentProvider

• Content Provider is based on DB concept, but abstracts storage mechanism

• Database storage the normal use case

• ContentProviders exposes data outside the application

• ContentProviders expose a full CRUD interface

• Create, Retrieve, Update, Delete

tisdag den 15 februari 2011

Page 168: Android Application Development at JFokus 2011

FundamentalsContentProvider

• Data model = Database table

_ID

All ContentProviders,Unique,

(cmp Primary Key)

tisdag den 15 februari 2011

Page 169: Android Application Development at JFokus 2011

FundamentalsContentProvider

_ID Name Phone Location Type

1 Steve 123456 Office Mobile

ContentProvider

1. Uri2. Column names3. Field types

• External interface

• Uri, Column name, Field type, Mime type

tisdag den 15 februari 2011

Page 170: Android Application Development at JFokus 2011

FundamentalsContentProvider

• Native ContentProviders expose URI and Column names as constants, e.g.:

• android.provider.Settings.System.CONTENT_URI

• android.provider.ContactsContract.Contacts.CONTENT_URI

• Native ContentProviders may require permissions, e.g.:

• android.permission.READ_CONTACTS

• android.permission.WRITE_SETTINGS

tisdag den 15 februari 2011

Page 172: Android Application Development at JFokus 2011

Manipulating Content ContentProvider

• ContentValues used for insert and update

• key/value pairs

• InsertUri uri = insert(Uri uri, ContentValues values)

• Updateint rows = update(Uri uri, ContentValues values, String where, String[] selectionArgs)

• Deleteint rows = delete(Uri url, String where, String[] selectionArgs)

tisdag den 15 februari 2011

Page 173: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

tisdag den 15 februari 2011

Page 174: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

Client App

tisdag den 15 februari 2011

Page 175: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

Client App

ContentResolver

query(Uri, {Name, Phone}, “Bill”)

tisdag den 15 februari 2011

Page 176: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

Client App

ContentResolver

query(Uri, {Name, Phone}, “Bill”) Cursor

tisdag den 15 februari 2011

Page 177: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

Client App

ContentResolver

query(Uri, {Name, Phone}, “Bill”) Cursor

“Access result set”

tisdag den 15 februari 2011

Page 178: Android Application Development at JFokus 2011

CursorContentProvider

ContentProvider

_ID Name Phone

1 Steve 123456

2 Bill 654321

UriColumn names

Client App

ContentResolver

query(Uri, {Name, Phone}, “Bill”) Cursor

“Access result set”

Bill 654321

Result set from query

tisdag den 15 februari 2011

Page 181: Android Application Development at JFokus 2011

Expose Data - Step 1ContentProvider

• Decide on storage mechanism

• File system

• Network

• Database

tisdag den 15 februari 2011

Page 182: Android Application Development at JFokus 2011

Expose Data - Step 2ContentProvider

• Extend the ContentProvider classonCreate() - Initialize the provider on application start

______________________________________________________________________________

query(Uri, String[], String, String[], String) - Return Cursor-object

insert(Uri, ContentValues) - Insert data

update(Uri, ContentValues, String, String[]) - Update data

delete(Uri, String, String[]) - Delete data

______________________________________________________________________________

getType(Uri) - Return the data MIME type

tisdag den 15 februari 2011

Page 183: Android Application Development at JFokus 2011

Expose Data - Step 2ContentProvider

• Extend the ContentProvider classonCreate() - Initialize the provider on application start

______________________________________________________________________________

query(Uri, String[], String, String[], String) - Return Cursor-object

insert(Uri, ContentValues) - Insert data

update(Uri, ContentValues, String, String[]) - Update data

delete(Uri, String, String[]) - Delete data

______________________________________________________________________________

getType(Uri) - Return the data MIME type

UI Thread

tisdag den 15 februari 2011

Page 184: Android Application Development at JFokus 2011

Expose Data - Step 2ContentProvider

• Extend the ContentProvider classonCreate() - Initialize the provider on application start

______________________________________________________________________________

query(Uri, String[], String, String[], String) - Return Cursor-object

insert(Uri, ContentValues) - Insert data

update(Uri, ContentValues, String, String[]) - Update data

delete(Uri, String, String[]) - Delete data

______________________________________________________________________________

getType(Uri) - Return the data MIME type

UI Thread

Threadsafe

tisdag den 15 februari 2011

Page 185: Android Application Development at JFokus 2011

Expose Data - Step 3ContentProvider

• Declare the ContentProvider

<manifest>

    <application>

        <provider>...</provider>

    </application>

</manifest>

<manifest>

    <application>

        <provider>...</provider>

    </application>

</manifest>

<manifest>

    <application>

        <provider android:name="com.jayway.provider.MyProvider"          android:authorities="com.jayway.provider.myprovider"

...          </provider>

    </application>

</manifest>

tisdag den 15 februari 2011

Page 186: Android Application Development at JFokus 2011

Expose Data - Step 4ContentProvider

• Create ContentProvider definition according to Android standard

• CONTENT_URI

• Column names

• Mime types

• vnd.android.cursor.item/vnd.yourcompanyname.contenttype

• vnd.android.cursor.dir/vnd.yourcompanyname.contenttype

tisdag den 15 februari 2011

Page 187: Android Application Development at JFokus 2011

Expose Data - ExampleContentProvider

_ID PICTURE_URI Name Rarity _DATA

2. content://se.jayway.animals/animals

1. Storage: DB2. CONTENT_URI3. ContentProvider Definition4. ContentProvider5. UriMatcher

3.Animal

1.

4. AnimalProvider

5.UriMatcher

tisdag den 15 februari 2011

Page 188: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 189: Android Application Development at JFokus 2011

• Receive broadcasted events

• System and Apps

• Subclass

• onReceive()

• Registration

• Static

• Dynamic

• Context.registerReceiver()

BroadcastReceiver

tisdag den 15 februari 2011

Page 190: Android Application Development at JFokus 2011

• Receive broadcasted events

• System and Apps

• Subclass

• onReceive()

• Registration

• Static

• Dynamic

• Context.registerReceiver()

BroadcastReceiver

UI Thread

tisdag den 15 februari 2011

Page 192: Android Application Development at JFokus 2011

Lifecycle

Start

Finish

onReceive()

Active

Incoming event

BroadcastReceiver

tisdag den 15 februari 2011

Page 193: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

tisdag den 15 februari 2011

Page 194: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

tisdag den 15 februari 2011

Page 195: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

tisdag den 15 februari 2011

Page 196: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

Context.sendBroadcast()

tisdag den 15 februari 2011

Page 197: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

Context.sendBroadcast()

tisdag den 15 februari 2011

Page 198: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

Match!

Match!Context.sendBroadcast()

tisdag den 15 februari 2011

Page 199: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

?Context.sendBroadcast()

tisdag den 15 februari 2011

Page 200: Android Application Development at JFokus 2011

Normal BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="some.ACTION" /> </intent-filter></receiver>

IntentFilter

IntentFilter

?Context.sendBroadcast()

Undefined

tisdag den 15 februari 2011

Page 201: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

tisdag den 15 februari 2011

Page 202: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

tisdag den 15 februari 2011

Page 203: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

tisdag den 15 februari 2011

Page 204: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 205: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 206: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

Match!

Match!Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 207: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

?Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 208: Android Application Development at JFokus 2011

Ordered BroadcastsBroadcastReceiver

App A

Receiver A

App BReceiver B

Event sender

Application/System

Intent

some.ACTION

<receiver android:name=".MyReceiver"> <intent-filter

android:priority=”...”> <action android:name="some.ACTION" /> </intent-filter>

IntentFilterpriority = 1

IntentFilterpriority = 2

?Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 209: Android Application Development at JFokus 2011

AppAReceiver A

IntentFilterpriority = 10

App BReceiver B

IntentFilterpriority = 5

App CReceiver C

IntentFilterpriority = 0

Ordered BroadcastsBroadcastReceiver

Event senderIntent

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 210: Android Application Development at JFokus 2011

AppAReceiver A

IntentFilterpriority = 10

App BReceiver B

IntentFilterpriority = 5

App CReceiver C

IntentFilterpriority = 0

Ordered BroadcastsBroadcastReceiver

Event senderIntent

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 211: Android Application Development at JFokus 2011

AppAReceiver A

IntentFilterpriority = 10

App BReceiver B

IntentFilterpriority = 5

App CReceiver C

IntentFilterpriority = 0

Ordered BroadcastsBroadcastReceiver

Event senderIntent

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 212: Android Application Development at JFokus 2011

AppAReceiver A

IntentFilterpriority = 10

App BReceiver B

IntentFilterpriority = 5

App CReceiver C

IntentFilterpriority = 0

Ordered BroadcastsBroadcastReceiver

Event senderIntent

Context.sendOrderedBroadcast()

tisdag den 15 februari 2011

Page 213: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 214: Android Application Development at JFokus 2011

• Background operation that isnʼt interacting with the user

• Less likely to be shut down by the system

• Started explicitly or Bound to

• startService

• bindService

• Expose functionality to other apps

Service

tisdag den 15 februari 2011

Page 215: Android Application Development at JFokus 2011

• Must be enabled to be started

• Permissions limit access to service

• android:process=”processName”

• Run service in own service

• “:a” -> Private process

• “”:A -> Global process

AndroidMainifest.xmlService

tisdag den 15 februari 2011

Page 216: Android Application Development at JFokus 2011

LifecycleService

Start

Finish

onCreate()

onStartCommand()

onDestroy()

Active

Start

Finish

onCreate()

onBind()

onDestroy()

Active

onUnbind()

onRebind()

startService()

Client

stopService()

bindService()

unbindService()

tisdag den 15 februari 2011

Page 217: Android Application Development at JFokus 2011

• Explicitly started services (startService())

• onStartCommand defines start mode

• START_STICKY

• START_NOT_STICKY

   

Start ModesService

public int onStartCommand(Intent intent, int flags, int startId) {        return START_STICKY;    }

tisdag den 15 februari 2011

Page 218: Android Application Development at JFokus 2011

• Service that doesnʼt run in the same process as the client application

• Communication between Linux processes through IPC

• Synchronous calls

• Android provides an abstraction mechanism for IPC

• AIDL

Remote ServiceService

tisdag den 15 februari 2011

Page 219: Android Application Development at JFokus 2011

• Service defines the interface in an AIDL-file

• Limited data set

• Primitives, String, List, Map, CharSequence

• Parcelables - Custom classes

• aidl: Compiles aidl-files

• Implement AIDL interface

• Expose the interface

Android IPC AbstractionService Remote Service

tisdag den 15 februari 2011

Page 220: Android Application Development at JFokus 2011

Android IPC AbstractionService Remote Service

MyRemoteInterface.aidlaidl

MyRemoteInterface.java

tisdag den 15 februari 2011

Page 221: Android Application Development at JFokus 2011

Android IPC AbstractionService Remote Service

MyRemoteInterface.aidlaidl

MyRemoteInterface.java

MyRemoteInterface

StubBinder<inherit>

tisdag den 15 februari 2011

Page 222: Android Application Development at JFokus 2011

System Services

• Context.getSystemService()

• adb shell service list

Service

WINDOW_SERVICE

LAYOUT_INFLATER_SERVICEACTIVITY_SERVICEPOWER_SERVICE

ALARM_SERVICENOTIFICATION_SERVICE

KEYGUARD_SERVICELOCATION_SERVICEVIBRATOR_SERVICE

CONNECTIVITY_SERVICEWIFI_SERVICE

tisdag den 15 februari 2011

Page 223: Android Application Development at JFokus 2011

System Services

• Context.getSystemService()

• adb shell service list

Service

WINDOW_SERVICE

LAYOUT_INFLATER_SERVICEACTIVITY_SERVICEPOWER_SERVICE

ALARM_SERVICENOTIFICATION_SERVICE

KEYGUARD_SERVICELOCATION_SERVICEVIBRATOR_SERVICE

CONNECTIVITY_SERVICEWIFI_SERVICE

The system services may be closely related to the current context. Only use

it within the current context.

Watch out!

tisdag den 15 februari 2011

Page 224: Android Application Development at JFokus 2011

NotificationManager

• Notify the user that something has happened

• Put message in status bar

• Flash backlight

• Flash LEDʼs

• Play soundUse notifications when informing the

user of events in the background. Don’t show Activity or Dialog.

User experience!

Service System Services

tisdag den 15 februari 2011

Page 225: Android Application Development at JFokus 2011

NotificationManager

String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.notification_icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

private static final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification);

System service

Create Notification

Create Intent to send

Let the platform have the notification

Service System Services

tisdag den 15 februari 2011

Page 226: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 227: Android Application Development at JFokus 2011

Application Design

Activity

“Manifest”

Application

Activity

Service BroadcastReceiver

ContentProvider

Resources

tisdag den 15 februari 2011

Page 228: Android Application Development at JFokus 2011

Application

A“ManifeApplication

Application design

• Global state

• Reference to domain model data

• Accessible from all components

tisdag den 15 februari 2011

Page 229: Android Application Development at JFokus 2011

ActivityApplication design

• Easily gets fat

• Move functionality out of the Activity

Activity

BaseActivity

ActivityActivityDialogBuilder

ModelPOJO

Router

tisdag den 15 februari 2011

Page 230: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

tisdag den 15 februari 2011

Page 231: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

Ja

Ja Ja

Ja Ja

Vi

ViVi

Vi Vi

tisdag den 15 februari 2011

Page 232: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

Activity

Ja

Ja Ja

Ja Ja

Vi

ViVi

Vi Vi

tisdag den 15 februari 2011

Page 233: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

ContentProvider

Activity

Ja

Ja Ja

Ja Ja

Vi

ViVi

Vi Vi

tisdag den 15 februari 2011

Page 234: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

ContentProviderCursor

Activity

Ja

Ja Ja

Ja Ja

Vi

ViVi

Vi Vi

tisdag den 15 februari 2011

Page 235: Android Application Development at JFokus 2011

Android MVCApplication design

Controller

View

ModelModelModelModel

ContentProviderCursor

Cursor binds to contentwith ContentObserver

getContext().getContentResolver().notifyChange()ContentObserver.onChange()

Activity

Ja

Ja Ja

Ja Ja

Vi

ViVi

Vi Vi

tisdag den 15 februari 2011

Page 236: Android Application Development at JFokus 2011

Android Network MVCApplication design

Book: Programming Android, O’Reilly

Google IO: http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

tisdag den 15 februari 2011

Page 237: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 238: Android Application Development at JFokus 2011

• Screen size

• Screen pixel density

• Manufacturers

• Sony Ericsson, HTC, Samsung, LG, Motorola, etc.

• Keyboard

• Touch, QWERTY, 12-key, etc."

Android Fragmentation

tisdag den 15 februari 2011

Page 239: Android Application Development at JFokus 2011

• Small

• Normal

• Large

• XLarge (2.2/Froyo)

Screen SizeFragmentation

tisdag den 15 februari 2011

Page 240: Android Application Development at JFokus 2011

• Low (120dpi)

• Medium (160dpi)

• High (240dpi)

• Extra high (320dpi)

Screen Pixel DensityFragmentation

tisdag den 15 februari 2011

Page 241: Android Application Development at JFokus 2011

Alt. Resource FrameworkFragmentation Screen Pixel Density

Activity

AndroidManifest.xml

/res

/anim

/drawable

/layout

/values

/xml

/raw

/drawable_ldpi

/drawable_mdpi

/drawable_hdpi

/drawable_xhdpi

/my_image.png

/my_image.png

/my_image.png

/my_image.png

tisdag den 15 februari 2011

Page 242: Android Application Development at JFokus 2011

RecommendationsFragmentation Screen Pixel Density

• Leave as much as possible to the platform to decide

• wrap_content, match_parent

• Avoid defining absolute pixel values

• “px”

• Use pixel dependent values

• “dp”

• “sp”

tisdag den 15 februari 2011

Page 243: Android Application Development at JFokus 2011

Platform versionsFragmentation

tisdag den 15 februari 2011

Page 244: Android Application Development at JFokus 2011

ScreensFragmentation

tisdag den 15 februari 2011

Page 245: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 246: Android Application Development at JFokus 2011

• Large Android community

• Lotʼs of good, helpful third party tools

External Tools

tisdag den 15 februari 2011

Page 247: Android Application Development at JFokus 2011

• Dependency injection framework for Android

• http://code.google.com/p/roboguice/

RoboguiceExternal Tools

class AndroidWay extends Activity {     TextView name;     ImageView thumbnail;     LocationManager loc;     Drawable icon;     String myName;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name      = (TextView) findViewById(R.id.name);         thumbnail = (ImageView) findViewById(R.id.thumbnail);         loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);         icon      = getResources().getDrawable(R.drawable.icon);         myName    = getString(R.string.app_name);         name.setText( "Hello, " + myName );     } }

class RoboWay extends RoboActivity {     @InjectView(R.id.name)             TextView name;     @InjectView(R.id.thumbnail)        ImageView thumbnail;     @InjectResource(R.drawable.icon)   Drawable icon;     @InjectResource(R.string.app_name) String myName;     @Inject                            LocationManager loc;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name.setText( "Hello, " + myName );     } }

tisdag den 15 februari 2011

Page 248: Android Application Development at JFokus 2011

• Unit test framework

• Runs tests on computer and not on device

• Tests are run in the computer JVM

• Makes mocking frameworks obsolete

• http://pivotal.github.com/robolectric/

RobolectricExternal Tools

tisdag den 15 februari 2011

Page 249: Android Application Development at JFokus 2011

• Black box functional test framework

• Source code access not needed

• Write test scenarios spanning over multiple activities

• Open source

• http://code.google.com/p/robotium/

RobotiumExternal Tools

tisdag den 15 februari 2011

Page 250: Android Application Development at JFokus 2011

• Converts Java objects to their JSON representation

• Converts JSON representation to Java objects

Google GSONExternal Tools

BagOfPrimitives obj = new BagOfPrimitives();Gson gson = new Gson();String json = gson.toJson(obj);

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class)

tisdag den 15 februari 2011

Page 251: Android Application Development at JFokus 2011

• Log4j API

• File logging (Internal/SD Card)

• http://code.google.com/p/microlog4android/

microlog4androidExternal Tools

tisdag den 15 februari 2011

Page 252: Android Application Development at JFokus 2011

• Maven Android Plugin

• m2eclipse

• http://code.google.com/p/maven-android-plugin/

MavenExternal Tools

tisdag den 15 februari 2011

Page 253: Android Application Development at JFokus 2011

• Analyzes memory dumps

• Find memory leaks

• Eclipse plugin

• http://www.eclipse.org/mat/

Memory Analyzer ToolExternal Tools

tisdag den 15 februari 2011

Page 254: Android Application Development at JFokus 2011

AgendaAndroid

introUI

UserExperience

IntentsContentProvider

ApplicationDesign

ExternalTools

AndroidMarket

BroadcastReceiver

Service

AndroidFragmentation

AndroidApplication

tisdag den 15 februari 2011

Page 255: Android Application Development at JFokus 2011

Android Market

tisdag den 15 februari 2011

Page 256: Android Application Development at JFokus 2011

• Restricts what Apps are shown to user in Market

• Filter

• Device type

• Publishing status

• Priced status

• Native platform

• Forward lock

Market FiltersAndroid Market

tisdag den 15 februari 2011

Page 257: Android Application Development at JFokus 2011

• Restricts what Apps are shown to user in Market

• Filter

• Device type

• Publishing status

• Priced status

• Native platform

• Forward lock

Market FiltersAndroid Market

tisdag den 15 februari 2011

Page 258: Android Application Development at JFokus 2011

Device TypeAndroid Market Market Filters

<supports-screens>

<uses-configuration>

<uses-feature>

<uses-library>

<uses-sdk>

AndroidManifest.xml

tisdag den 15 februari 2011

Page 259: Android Application Development at JFokus 2011

• One time fee: $25

RegisterAndroid Market

tisdag den 15 februari 2011

Page 260: Android Application Development at JFokus 2011

• One time fee: $25

RegisterAndroid Market

MerchantAccount

tisdag den 15 februari 2011

Page 261: Android Application Development at JFokus 2011

• APK ( < 50 MB)

• Screenshots (2 Required + 6 Optional)

• Application Icon (Required)

• Promotional video (Optional)

• Listing details

• Contact information (One of WWW, Mail, Phone)

UploadAndroid Market

tisdag den 15 februari 2011

Page 262: Android Application Development at JFokus 2011

Argentina*• Australia• Austria• Belgium• Brazil*• Canada• Denmark• Finland• France• Germany• Hong Kong• Ireland• Israel*• Italy• Japan• Mexico*• Netherlands• New Zealand• Norway• Portugal• Russia*• Singapore• Spain• South Korea*• Sweden• Switzerland• Taiwan*• United Kingdom• United States

Paid AppsAndroid Market

• Transaction fee: 30%

• Pricing

• SEK: 7 SEK - 1500 SEK

tisdag den 15 februari 2011

Page 263: Android Application Development at JFokus 2011

AdvertisingAndroid Market

• AdMob

• AdSense content

• Add market-url

• market://<your_app_url>

tisdag den 15 februari 2011

Page 264: Android Application Development at JFokus 2011

AdMob SDKAndroid Market

<!-- The application's publisher ID assigned by AdMob --><meta-data android:value="YOUR_ID_HERE" android:name="ADMOB_PUBLISHER_ID" />

<!-- AdMobActivity definition --><activity android:name="com.admob.android.ads.AdMobActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="orientation|keyboard|keyboardHidden" />

<!-- Track Market installs --><receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">

<intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /></intent-filter>

</receiver>

Adverstising

• admob-sdk-android.jar

tisdag den 15 februari 2011

Page 265: Android Application Development at JFokus 2011

• Sell content in application

• Android Market publisher account

• Google Checkout merchant account

• No Android API

• Android Market handles the transaction

In-App BillingAndroid Market

tisdag den 15 februari 2011

Page 266: Android Application Development at JFokus 2011

In-App BillingAndroid Market

tisdag den 15 februari 2011

Page 267: Android Application Development at JFokus 2011

Implementation overviewAndroid Market

• Market-app exposes API

• IMarketBillingService.aidl

• Service

• Send info to Market-app

• BroadcastReceiver

• Billing info from Market-app

• Response codes

• Security-component

• Integrity of transaction

In-app Billing

tisdag den 15 februari 2011

Page 268: Android Application Development at JFokus 2011

• 3.0

• Tablet version of Android

• New UI

• Richer widgets

• New application set

• Fragments API

Android - What are we waiting for?

tisdag den 15 februari 2011

Page 269: Android Application Development at JFokus 2011

Thank you for listening!

tisdag den 15 februari 2011