Top Banner
100

Being Epic: Best Practices for Android Development

Sep 08, 2014

Download

Technology

Reto Meier

Turn good ideas into great apps by following some essential Android development best practices. Starting with an overview of the 5 deadly sins and 5 golden rules, you will learn how to build apps that users love and that are good citizens of the mobile environment. You will also learn specific best practices for background apps and location based services.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Being Epic: Best Practices for Android Development
Page 2: Being Epic: Best Practices for Android Development

Revised v4Presenter

Being Epic: Best Practices for Android DevelopmentReto MeierTwitter: retomeier

Page 3: Being Epic: Best Practices for Android Development

?

Page 4: Being Epic: Best Practices for Android Development

?

Page 5: Being Epic: Best Practices for Android Development

?

Page 6: Being Epic: Best Practices for Android Development
Page 7: Being Epic: Best Practices for Android Development
Page 8: Being Epic: Best Practices for Android Development

Android Market

• Buyer support: 32• Seller support: 29• Including Germany, Czech Republic, and Russia!

Page 9: Being Epic: Best Practices for Android Development

More Downloads = More Revenue

• Paid apps• Ad supported• Freemium

Page 10: Being Epic: Best Practices for Android Development

Agenda

• The Five Deadly Sins• The Five Glorious Virtues• Measuring Your Success• Two Practical Examples

Page 11: Being Epic: Best Practices for Android Development

The Five Deadly Sins

Page 12: Being Epic: Best Practices for Android Development

S L O T HBe Fast. Be Responsive.

Page 13: Being Epic: Best Practices for Android Development

The Golden Rules of Performance

• Don't do work that you don't need to do• Don't allocate memory if you can avoid it

Page 14: Being Epic: Best Practices for Android Development

Performance Pointers

developer.android.com/guide/practices/design/performance.html

• Optimize judiciously• Avoid creating objects• Use native methods• Prefer Virtual over Interface• Prefer Static over Virtual• Avoid internal setters and getters• Declare constants final• Avoid float and enums• Use package scope with inner classes

Page 15: Being Epic: Best Practices for Android Development

Responsiveness

• Avoid modal Dialogues and Activitieso Always update the user on progress (ProgressBar and

ProgressDialog)o Render the main view and fill in data as it arrives

Page 16: Being Epic: Best Practices for Android Development
Page 17: Being Epic: Best Practices for Android Development

Responsiveness

• "Application Not Responding"o Respond to user input within 5 secondso Broadcast Receiver must complete in 10 seconds

• Users perceive a lag longer than 100 to 200ms• Use Threads and AsyncTasks within Services

Page 18: Being Epic: Best Practices for Android Development

AsyncTask

protected Void doInBackground(Void... arg0) { // Do time consuming processing postProgress(); return null;}

@Override protected void onPostProgress(Void... arg0) { // Update GUI with progress}

@Overrideprotected void onPostExecute(Void result) { // Update GUI on completion}

Page 19: Being Epic: Best Practices for Android Development

G L U T T O N YUse system resources responsibly

Page 20: Being Epic: Best Practices for Android Development

Gluttony

Don'ts• DON'T over use WakeLocks• DON'T update Widgets too frequently• DON'T update your location unnecessarily• DON'T use Services to try to override users or the system

Dos• DO share data to minimize duplication• DO use Receivers and Alarms not Services and Threads• DO let users manage updates• DO minimize resource contention

Page 21: Being Epic: Best Practices for Android Development

What is a WakeLock?

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Wakelock");

wl.acquire(); // Screen and power stays onwl.release();

• Force the CPU to keep running• Force the screen to stay on (or stay bright)• Drains your battery quickly and efficiently

Page 22: Being Epic: Best Practices for Android Development

Using WakeLocks

• Do you really need to use one?• Use the minimum level possible

o PARTIAL_WAKE_LOCKo SCREEN_DIM_WAKE_LOCKo SCREEN_BRIGHT_WAKE_LOCKo FULL_WAKE_LOCK

• Release as soon as you can• Specify a timeout• Don't use them in Activities

Page 23: Being Epic: Best Practices for Android Development

Window Managed WakeLocks

• No need for permissions• No accidently leaving the screen from the background

getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Page 24: Being Epic: Best Practices for Android Development

H O S T I L I T YDon't fight your users

Page 25: Being Epic: Best Practices for Android Development

Hostility

• UX should be your #1 priority

Page 26: Being Epic: Best Practices for Android Development

Hostility

• User experience should be your top priority• Respect user expectations for navigating your app• Don't hijack the native experience• Respect user preferences

Page 27: Being Epic: Best Practices for Android Development

Respect User Expectations for Navigation Flow

• The back button should always navigate back through previously seen screens

• Always support trackball navigation• Understand your navigation flow when entry point is a

notification or widget• Navigating between application elements should be easy

and intuitive

Page 28: Being Epic: Best Practices for Android Development

Don't Hijack the Native Experience

• Don't hide the status bar• Back button should always navigate through previous screens• Use native icons consistently• Don't override the menu button• Put menu options behind the menu button

Page 29: Being Epic: Best Practices for Android Development

Respect User Preferences

• Use only enabled location-based services• Ask permission before transmitting location data• Only transfer data in the background if user enabled

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

boolean backgroundEnabled = cm.getBackgroundDataSetting();

Page 30: Being Epic: Best Practices for Android Development

A R R O G A N C EDon't fight the system

Page 31: Being Epic: Best Practices for Android Development

Arrogance

• Don't use undocumented APIs• Seriously. Don't use undocumented APIs• Make your app behave consistently with the system• Respect the application lifecycle model• Support both landscape and portrait modes• Don't disable rotation handling

Page 32: Being Epic: Best Practices for Android Development

D I S C R I M I N A T I O NDesign for everyone

Page 33: Being Epic: Best Practices for Android Development
Page 34: Being Epic: Best Practices for Android Development
Page 35: Being Epic: Best Practices for Android Development

Size Discrimination

• Don't make assumptions about screen size or resolution• Use Relative Layouts and device independent pixels• Optimize assets for different screen resolutions

Page 36: Being Epic: Best Practices for Android Development
Page 37: Being Epic: Best Practices for Android Development

Ensuring Future Hardware Happiness

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

<uses-feature android:name="android.hardware.location.network" android:required="false"/>

<uses-feature android:name="android.hardware.location.gps" android:required="false"/>

• Specify uses-feature node for every API you use.• Mark essential features as required.• Mark optional features as not required.• Check for API existence in code.

Page 38: Being Epic: Best Practices for Android Development

Ensuring Future Hardware Happiness

PackageManager pm = getPackageManager();

boolean hasCompass = pm.hasSystemFeature( PackageManager.FEATURE_SENSOR_COMPASS);

if (hasCompass) { // Enable things that require the compass.}

• Specify uses-feature node for every API you use.• Mark essential features as required.• Mark optional features as not required.• Check for API existence in code.

Page 39: Being Epic: Best Practices for Android Development

Agenda

• The Five Deadly Sins• The Five Glorious Virtues• Measuring Your Success• Two Practical Examples

Page 40: Being Epic: Best Practices for Android Development

The Five Glorious Virtues

Page 41: Being Epic: Best Practices for Android Development

B E A U T YHire a designer

Page 42: Being Epic: Best Practices for Android Development
Page 43: Being Epic: Best Practices for Android Development
Page 44: Being Epic: Best Practices for Android Development

Beauty

• Create assets optimized for all screen resolutionso Start with vectors or high-res raster arto Scale down and optimize for supported screen

• Support resolution independence• Use tools to optimize your implementation

o layoutopto hierarchyviewer

Page 45: Being Epic: Best Practices for Android Development

G E N E R O S I T YShare and consume

Page 46: Being Epic: Best Practices for Android Development

Generosity

• Use Intents to leverage other people's apps• Define Intent Filters to share your functionality

Page 47: Being Epic: Best Practices for Android Development

Using Intents to Start Other Apps

String action = "com.hotelapp.ACTION_BOOK";

String hotel = "hotel://name/" + selectedhotelName;Uri data = Uri.parse(hotel);

Intent bookingIntent = new Intent(action, data);startActivityForResult(bookingIntent);

• Works just like your own Activity• Can pass data back and forth between applications• Return to your Activity when closed

Page 48: Being Epic: Best Practices for Android Development

Activity Intent Filters

• Indicate the ability to perform an action on data• Specify an action you can perform• Specify the data you can perform it on

<activity android:name="Booking" android:label="Book"> <intent-filter> <action android:name="com.hotelapp.ACTION_BOOK" /> <data android:scheme="hotel" android:host="name"/> </intent-filter></activity>

Page 49: Being Epic: Best Practices for Android Development

@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(r.layout.main);

Intent intent = getIntent();

String action = intent.getAction(); Uri data = intent.getData();

String hotelName = data.getPath();

// TODO Provide booking functionality

setResult(RESULT_OK, null); finish();}

Activity Intent Filters

Page 50: Being Epic: Best Practices for Android Development

U B I Q U I T YBe more than an icon

Page 51: Being Epic: Best Practices for Android Development

Ubiquity

• Create widgets• Surface search results into the Quick Search Box• Live Folders• Live Wallpapers• Expose Intent Receivers to share your functionality• Fire notifications

Page 52: Being Epic: Best Practices for Android Development

U T I L I T YBe useful. Be interesting.

Page 53: Being Epic: Best Practices for Android Development

Utility & Entertainment

• Create an app that solves a problem• Present information in the most useful way possible• Create games that are ground breaking and compelling

Page 54: Being Epic: Best Practices for Android Development

Agenda

• The Five Deadly Sins• The Five Glorious Virtues• Measuring Your Success• Two Practical Examples

Page 55: Being Epic: Best Practices for Android Development

Using Analytics

Page 56: Being Epic: Best Practices for Android Development

User Feedback

• Haven't received a notification in weeks even though they're turned on. HTC EVO

• Great app! HTC EVO• SD card support anytime soon?• This is useless and really annoying because every time

an earth quake happens it makes Ur phone ring• Slowest app on the market.• Boring• Awesome totally f ing awesome!• Great tool! I love it. Im a geologist so its great to have this

info handy.

Page 57: Being Epic: Best Practices for Android Development

User Feedback

• Users contradict each other• Users don't know framework limitations• Users can't follow instructions• Users provide unhelpful feedback• Users LIE

Page 58: Being Epic: Best Practices for Android Development
Page 59: Being Epic: Best Practices for Android Development

Use Analytics

• User demographics• App usage patterns• Exception and error reporting• Any analytics package is supported

Page 60: Being Epic: Best Practices for Android Development

Google Analytics for Mobile Applications

• Track every Activity viewed• Track settings selected• Track exceptions and error conditions

// Start trackingtracker.start("UA-MY_CODE-XX", this);

// Register a page viewtracker.trackPageView("/map_view");

// Send views to servertracker.dispatch();

Page 61: Being Epic: Best Practices for Android Development

Agenda

• The Five Deadly Sins• The Five Glorious Virtues• Measuring Your Success• Two Practical Examples

Page 62: Being Epic: Best Practices for Android Development

Background Updates

Page 63: Being Epic: Best Practices for Android Development

Don't be "That Guy"

Page 64: Being Epic: Best Practices for Android Development

Don't be "That Guy"

• Let the runtime kill your background Service

Page 65: Being Epic: Best Practices for Android Development

Let the Runtime Kill Your Service

@Overridepublic int onStartCommand(Intent intent, int f, int sId) { handleCommand(intent); return START_NOT_STICKY;}

@Overridepublic void onStart(Intent intent, int sId) { handleCommand(intent);}

• For Services that perform a single action / polling• Reduces resource contention

Page 66: Being Epic: Best Practices for Android Development

Don't be "That Guy"

• Let the runtime kill your background Service• Kill your own Service

Page 67: Being Epic: Best Practices for Android Development

Kill Your Own Service

• Services should only be running when needed• Complete a task, then kill the Service

stopSelf();

Page 68: Being Epic: Best Practices for Android Development

@Overridepublic int onStartCommand(Intent i, int f, int sId) { myTask.execute(); return Service.START_NOT_STICKY;}

AsyncTask<Void, Void, Void> myTask = new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... arg0) { // TODO Execute Task return null; } @Override protected void onPostExecute(Void result) { stopSelf(); }};

Kill Your Own Service

Page 69: Being Epic: Best Practices for Android Development

Don't be "That Guy"

• Let the runtime kill your background Service• Kill your own Service• Use Alarms and Intent Receivers

Page 70: Being Epic: Best Practices for Android Development

Alarms and Intent Receivers

• Schedule updates and polling• Listen for system or application events• No Service. No Activity. No running Application.

Page 71: Being Epic: Best Practices for Android Development

<receiver android:name="MyReceiver"> <intent-filter> <action android:name="REFRESH_THIS" /> </intent-filter></receiver>

public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent i) { Intent ss = new Intent(context, MyService.class); context.startService(ss); }}

Intent Receivers

Page 72: Being Epic: Best Practices for Android Development

String alarm = Context.ALARM_SERVICE;AlarmManager am;am = (AlarmManager)getSystemService(alarm);

Intent intent = new Intent("REFRESH_THIS");PendingIntent op;op = PendingIntent.getBroadcast(this, 0, intent, 0);

int type = AlarmManager.ELAPSED_REALTIME_WAKEUP; long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;long triggerTime = SystemClock.elapsedRealtime() + interval;

am.setRepeating(type, triggerTime, interval, op);

Alarms

Page 73: Being Epic: Best Practices for Android Development

Don't be "That Guy"

• Let the runtime kill your background Service• Kill your own Service• Use Alarms and Intent Receivers• Use inexact Alarms

Page 74: Being Epic: Best Practices for Android Development

Inexact Alarms

• All the Alarm goodness• Now with less battery drain!

int type = AlarmManager.ELAPSED_REALTIME_WAKEUP; long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;long triggerTime = SystemClock.elapsedRealtime() + interval;

am.setInexactRepeating(type, triggerTime, interval, op);

Page 75: Being Epic: Best Practices for Android Development

Don't be "That Guy"

• Let the runtime kill your background Service• Kill your own Service• Use Alarms and Intent Receivers• Use inexact Alarms• Use Cloud to Device Messaging

Page 76: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

• Lets you push instead of poll• Works best for targeted updates• Perfect for updates that need to be instant

Page 77: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

Page 78: Being Epic: Best Practices for Android Development

C2DM: Registering a Device

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));

registrationIntent.putExtra("sender", "[email protected]");

startService(registrationIntent);

Page 79: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

Page 80: Being Epic: Best Practices for Android Development

C2DM: Receiving the Device Registration ID

<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.mypackage.myAppName"/> </intent-filter></receiver>

Page 81: Being Epic: Best Practices for Android Development

C2DM: Receiving the Device Registration ID

public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( "com.google.android.c2dm.intent.REGISTRATION")) { // Handle Registration }}

Page 82: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

Page 83: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

Page 84: Being Epic: Best Practices for Android Development

C2DM: Sending a Message

C2DMessaging.get(getServletContext()). sendWithRetry( targetRegistrationID, userEmailAsCollapseKey, null, null, null, null);

• Use com.google.android.C2DM project to simplify.• Target individuals by mapping user to registration ID.

Page 85: Being Epic: Best Practices for Android Development

Cloud to Device Messaging

Page 86: Being Epic: Best Practices for Android Development

C2DM: Receiving a Message

<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.mypackage.myAppName"/> </intent-filter></receiver>

Page 87: Being Epic: Best Practices for Android Development

C2DM: Receiving a Message

public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( "com.google.android.c2dm.intent.RECEIVE")) { // Handle incoming message }}

Page 88: Being Epic: Best Practices for Android Development

Location Based Services

Page 89: Being Epic: Best Practices for Android Development

String serviceName = Context.LOCATION_SERVICE;lm = LocationManager)getSystemService(serviceName);

LocationListener l = new LocationListener() { public void onLocationChanged(Location location) { // TODO Do stuff when location changes! }

public void onProviderDisabled(String p) {} public void onProviderEnabled(String p) {} public void onStatusChanged(String p, int s, Bundle e) {}};

lm.requestLocationUpdates("gps", 0, 0, l);

Location Based Services

Page 90: Being Epic: Best Practices for Android Development

Location Based Services

• How often do you need updates?• What happens if GPS or Wifi LBS is disabled?• How accurate do you need to be?• What is the impact on your battery life?• What happens if location 'jumps'?

Page 91: Being Epic: Best Practices for Android Development

Restricting Updates

• Specify the minimum update frequency• Specify the minimum update distance

int freq = 5 * 60000; // 5minsint dist = 1000; // 1000m

lm.requestLocationUpdates("gps", freq, dist, l);

Page 92: Being Epic: Best Practices for Android Development

Criteria criteria = new Criteria();criteria.setPowerRequirement(Criteria.POWER_LOW);criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setAltitudeRequired(false);criteria.setBearingRequired(false);criteria.setSpeedRequired(false);criteria.setCostAllowed(false);

String provider = lm.getBestProvider(criteria, true);

lm.requestLocationUpdates(provider, freq, dist, l);

Use Criteria to Select a Location Provider

Page 93: Being Epic: Best Practices for Android Development

Use Criteria to Select a Location Provider

• Specify your requirements and preferenceso Allowable power draino Required accuracyo Need for altitude, bearing, and speedo Can a cost be incurred?

• Find the best provider that meets your criteria• Relax criteria (in order) until a provider is found• Can limit to only active providers• Can use to find all matching providers

Page 94: Being Epic: Best Practices for Android Development

Implement a Back-off Pattern

• Use multiple Location Listenerso Fine and coarseo High and low frequency / distance

• Remove listeners as accuracy improves

Page 95: Being Epic: Best Practices for Android Development

lm.requestLocationUpdates(coarseProvider,0,0, lcourse);lm.requestLocationUpdates(fineProvider, 0, 0, lbounce);

Location Based Services

Page 96: Being Epic: Best Practices for Android Development

private LocationListener lbounce = new LocationListener(){ public void onLocationChanged(Location location) { runLocationUpdate(); if (location.getAccuracy() < 10) { lm.removeUpdates(lbounce); lm.removeUpdates(lcoarse); lm.requestLocationUpdates(provider, freq, dist, l); } }};

private LocationListener lcoarse = new LocationListener(){ public void onLocationChanged(Location location) { runLocationUpdate(); lm.removeUpdates(lcoarse); }};

Location Based Services

Page 97: Being Epic: Best Practices for Android Development

E P I C (N E S S)Be legendary

?

Page 98: Being Epic: Best Practices for Android Development

Epicnessicity

• Don't be satisfied with good• Create unique solutions • Invent new paradigms• Leverage the hardware• Respect your users• Respect the system• Think BIG!

Page 99: Being Epic: Best Practices for Android Development

Questions? Feedback?

• Twitter: retomeier• Stack Overflow tag: android• developer.android.com

Page 100: Being Epic: Best Practices for Android Development