Sie sind auf Seite 1von 51

Beginner's Guide to Android

Nick Butcher
9th May 2011

@crafty
hashtags: #iobootcamp #android
Session Feedback

http://www.speakermeter.com/talks/guide-android/
http://goo.gl/XLLgk
Agenda

Android platform
Writing your app
Anatomy of an app
Building your app
Surfacing your Content
Hardware Sensors
Quality
Tools
Publishing your app
Registering for Android Market
Targeting
Many Devices, One Platform
170+ devices in 100+ countries
Shipping 350K+ devices per day
1 year ago was 60K per day!
> 1m new potential users every 3 days!!
Any device with Google services must pass
the Compatibility Test Suite
Contract of device capabilities & APIs
Support many screen sizes/resolutions
Market filters on hardware availability
Many Devices, One Platform

New Platform/SDK
releases
Target latest but support
older
Gracefully degrade
functionality

Source: Date collected during two weeks ending on April 1, 2011


Writing your app
An Android application is actually a
collection of several components, each
defined in AndroidManifest.xml
Anatomy of an App

Activity
Service
Content Provider
Broadcast Receiver
Intents
Manifest
Anatomy of an App
Activity

A single screen with a


user interface
Independent but
work together to form
a cohesive whole
Possible to invoke
from other applications
Extends the Activity
class
Anatomy of an App
Service

Perform long-running operations in the background


Does not provide a user interface
Other components can bind/interact
Extends the Service class
Anatomy of an App
Content provider
Manages a shared set of application data
Consistent interface to retrieve/store data
RESTful model
CRUD operations
Can be backed by different stores
e.g. File System, SQLite DB, Web
Can expose your data to other applications
Can consumer data from other Content Providers
e.g. Contacts or Call Log
Extend the ContentProvider class
Anatomy of an App
Broadcast receiver

Respond to system wide messages


Messages can be initiated by the system or an app
2 type of broadcast:
Normal - delivered async to all receivers
Ordered - delivered in priority order & can be aborted
Can programatically register or statically register via the
manifest
Should be very light, pass any work onto a Service
Extend the BroadcastReceiver class
Anatomy of an App
Intents
Intents are the messages that link app components together
Explicit / implicit
An abstract description of an operation to be performed
An Action to be performed
The Data to operate upon
Extra metadata
Standardise on a common vocabulary of Actions
e.g. 'View', 'Edit', 'Send'
Apps register their ability to handle Actions for a given data type
via IntentFilter
Anatomy of an App
Intents
Publish an 'Intent API'
Specify Action & Extras to invoke your component
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {


if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {


if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {


if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {


if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Anatomy of an App
Intents
Achieve complex tasks by calling other Application's
Intents, e.g. scanning a barcode

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {


if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
Anatomy of an App
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>

<uses-feature ... />


<uses-permission ... /> Declare
<uses-sdk ... />
components of your
<application ...> app
<activity ...>
... Declare required
</activity> features for your app
<service ...>
... State permissions
</service> required by your app
<provider ...>
... State platform versions
</provider> app is compatible with
<receiver ...>
...
</receiver>
</application>

</manifest>
Anatomy of an App
Activity lifecycle

Running in a multitasking
environment
Users switch apps, calls
come in, system runs low on
memory
System invokes callbacks in
your app
The system will kill your app
Be sure to save state!
Anatomy of an App
Activity lifecycle
Activity
created

Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more


Anatomy of an App
Activity lifecycle
Activity
created

onCreate()

onResume()

Activity
running

Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more


Anatomy of an App
Activity lifecycle

onResume()

Activity
running

Call comes
in

onPause()

Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more


Anatomy of an App
Activity lifecycle

onResume()

Activity
running
Return to
app
Call comes
in

onPause()

Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more


Anatomy of an App
Activity lifecycle

onResume()

Activity
running
Return to
app
Activity Call comes
destroyed in

Low onPause()
Memory

Simlpified, see http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for more


Surfacing Your Content

Android offers unique


ways to surface your
content
Surfacing Your Content
Unobtrusive notifications

Event driven
notifications
Optional vibration /
audio / LED alert
Ticker text / pull down
draw with more details
Custom expanded
view
Deep link into your
app with
a PendingIntent
Surfacing Your Content
Unobtrusive notifications

Event driven
notifications
Optional vibration /
audio / LED alert
Ticker text / pull down
draw with more details
Custom expanded
view
Deep link into your
app with
a PendingIntent
Surfacing Your Content
Widgets

Display your app's info


on the homescreen
Updated regularly
App can offer multiple
widgets
Great way to keep
user engaged with
your app
Surfacing Your Content
Cloud to Device Messages

Push messages from servers to app


Send 'tickles'
App doesn't have to be running
App has full control over how it responds
Efficient, event driven updates
Uses existing network channel
Minimal battery impact
Android 2.2+
Building your App
UI
Resource Framework
Supporting Multiple Screens
Hardware Sensors
Background Tasks
Accessibility
Quality
Tools
Building your App
UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout ... >
UI is a hierarchy of
<TextView android:id="@+id/text"
android:text="Hello World!" View & ViewGroup
... /> Defined
<Button android:id="@+id/button" programatically or
android:text="I am a Button" in XML Layout files
... />
Platform supplies
</LinearLayout> common palette of
widgets
Create your own
public void onCreate(Bundle savedInstanceState) { widgets
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Building your App
Resources framework

Resources live in the res folder


Qualifiers provide specific resources for
different device configuration
e.g. layout-land, drawable-hdpi
Resources IDs automatically generated
in R.java
e.g. R.layout.main

See http://developer.android.com/guide/topics/resources/index.html for more


Building your App
Supporting multiple screens
Building your App
Supporting multiple screens
Building your App
Supporting multiple screens

Use RelativeLayout
Size with wrap_content, match_parent, weight
Use dp units
Think web style layout
Provide density specific assets
e.g. drawable-hdpi
Provide size specific layouts
e.g. layout-xlarge
Use 9-patch drawables
Building your App
Hardware Sensors
Many devices have access to a variety of sensors
Accelerometer, Gravity, Gyroscope, Light, Magnetic Field
Use SensorManager.getSensorList() to find out what is available
SensorManager.registerListener() for sensor updates

See http://developer.android.com/reference/android/hardware/Sensor.html for more


Building your App
Background tasks

More than just multitasking


Use background tasks to provide fresh information
Use the AlarmManager to schedule updates
Use 'inexact repeating' to minimise power consumption
Regularly sync content from the web
Provide offline availability
Great for regularly updated content e.g. news feeds
Building your App
Accessibility
Label controls and input widgets with android:contentDescription tag
allows built in accessibility to work
Allow navigation with trackball / d-pad where available
Follow Android UI Best Practices
http://developer.android.com/guide/practices/ui_guidelines/index.html
Back button should always move the user back one logical
step
Try it in accessible mode!
Enable TalkBack in Settings -> Accessibility or download
from Market

See http://developer.android.com/guide/practices/design/accessibility.html for more


Building your App
Quality
Work with the Activity
lifecycle
Offload any long running
operation to a
background thread
Stability, UI
Responsiveness,
Performance
Listen to your users,
read their feedback
regularly
Work with a designer
Tools

1. Download and Install the Android SDK


developer.android.com/sdk/index.html
1. Download and Install Eclipse
http://eclipse.org
2. Install the Android Development Tools (ADT) Eclipse Plugin
http://developer.android.com/sdk/eclipse-adt.html#installing
3. Use SDK & AVD Manager in Eclipse to install the latest
Android packages.

Full guide:
http://developer.android.com/sdk/installing.html
Publishing Your App
Register For

Create a developer profile


Register at http://market.android.com/publish
One time $25 fee
No limit to number of apps you can publish
Developer Distribution Agreement
To sell apps, create a Checkout Merchant Account
Upload Your App

No review process
Publish as many updates as you like
Updates available right away
Supply promotional assets & video
Max APK size 50MB
Accessible on device & on Android Web Market
View crash reports online
View application statistics
Targeting

Only target specific countries


Target specific hardware
e.g. Telephony or OpenGL ES 2.0

// in AndroidManifest.xml

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

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

Android Market will not show app to incompatible device


Recap

Android platform
Writing your app
Anatomy of an app
Building your app
Surfacing your Content
Hardware Sensors
Quality
Tools
Publishing your app
Registering for Android Market
Targeting
Session Feedback

http://www.speakermeter.com/talks/guide-android/
http://goo.gl/XLLgk

Das könnte Ihnen auch gefallen