105
Wifi: pUp3EkaP

Android pro tips trilogy

Embed Size (px)

Citation preview

Page 1: Android  pro tips trilogy

Wifi: pUp3EkaP

Page 2: Android  pro tips trilogy

> 2000 members Largest Android Active Community

Page 3: Android  pro tips trilogy

Jonathan Yarkoni

Android Developer & Advocate

Ironsource

Android Academy Staff

Yonatan Levin

Android Google Developer

Expert

Britt Barak

Android LeadFigure8

Yossi Segev

Android DeveloperColu

Shahar Avigezer

Android DeveloperHello Heart

Page 4: Android  pro tips trilogy

Community Mentors

Page 5: Android  pro tips trilogy

What Do We Do?

● Android Fundamentals - NOW

● Android UI / UX - 29/1 !

● Community Hackathon - 9/3 !!!

● Android Performance

● Mentors Program● Active community

Page 6: Android  pro tips trilogy
Page 7: Android  pro tips trilogy
Page 8: Android  pro tips trilogy

+RanNachmany@shed2k

From good to awesome

Page 9: Android  pro tips trilogy

01Install

Page 10: Android  pro tips trilogy

01Install

02Launch

Start!

Page 11: Android  pro tips trilogy

01Install

02Launch

03Look & Feel

Start!

Page 12: Android  pro tips trilogy

01Install

02Launch

03Look & Feel

04Use

Start!

Page 13: Android  pro tips trilogy

Step 1: InstallFirst touch point.

Page 14: Android  pro tips trilogy
Page 15: Android  pro tips trilogy
Page 16: Android  pro tips trilogy
Page 17: Android  pro tips trilogy

Minimize Permissions• Request only core functionality blockers.

Page 18: Android  pro tips trilogy

Minimize Permissions• Request only core functionality blockers.• You don't need permission to launch another App that has

the permission

Page 19: Android  pro tips trilogy

Minimize Permissions• Request only core functionality blockers.• You don't need permission to launch another App that has

the permission

Page 20: Android  pro tips trilogy

Need a contact?

Page 21: Android  pro tips trilogy

Use the force, Luke

Page 22: Android  pro tips trilogy

Asking for contact

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType(Phone.CONTENT_ITEM_TYPE);startActivityForResult(intent, MY_REQUEST_CODE);

Page 23: Android  pro tips trilogy

void onActivityResult(int requestCode, int resultCode, Intent data) {

if (data != null) { Uri uri = data.getData(); if (uri != null) { Cursor c = getContentResolver().query(uri, new String[] {Contacts.DISPLAY_NAME,

Phone.NUMBER}, null,null, null) }

}}

Asking for contact

Page 24: Android  pro tips trilogy

Need a UUID?

Option 1: TelephonyManager.getDeviceIdRequires READ_PHONE_STATE.

Option 2: Settings.Secure.ANDROID_IDReset at wipeFollows the device, not the user.

Page 25: Android  pro tips trilogy

Use the force, Luke

Page 26: Android  pro tips trilogy

Generate UUIDUse backup API

Page 27: Android  pro tips trilogy

Step 2: First Launch“ok, let’s see what this app is all about”

Page 28: Android  pro tips trilogy

My BigBRAND

Page 29: Android  pro tips trilogy

LOADING ...

Page 30: Android  pro tips trilogy

A bunch of data to insert?

Page 31: Android  pro tips trilogy

SQLTransactions speeds things up

Page 32: Android  pro tips trilogy

db.beginTransaction();

try{ for(int i=0; i<selectedIds.length; i++){ values.put(COLUMN_ID,selectedIds[i]); values.put(COLUMN_STARRED,starred); db.insert(TABLE_STARRED,null,values); db.yieldIfContendedSafely(); } db.setTransactionSuccessful();}finally { db.endTransaction();}

Bulk DB update

Page 33: Android  pro tips trilogy

Not fast enough?

Page 34: Android  pro tips trilogy

Server Side● Create a db file using SQLite and

fill it.● Name your primary key columns

“_id”● Create a table:

“android_metadata”● Insert a single row containing the

locale (ex: "en_US")

Import ready to use DB

Page 35: Android  pro tips trilogy

Mobile Side● Grab the zipped DB from assets

or network.● Unzip it to your getDatabaseDir()● Use as usual

Import ready to use DB

SQLite version

Page 36: Android  pro tips trilogy

Step 3: Look & Feel“Where is the share button?”

Page 37: Android  pro tips trilogy
Page 38: Android  pro tips trilogy

HOTMAIL

Page 39: Android  pro tips trilogy

OUTLOOK.COM

Page 40: Android  pro tips trilogy

HOTMAIL OUTLOOK.COM

Page 41: Android  pro tips trilogy

Follow the guidelines

Page 42: Android  pro tips trilogy

LOOK AND FEEL

Page 43: Android  pro tips trilogy

LOOK AND FEEL

Page 44: Android  pro tips trilogy

LOOK AND FEEL

Page 45: Android  pro tips trilogy

LOOK AND FEEL

Page 46: Android  pro tips trilogy

LOOK AND FEEL

Page 47: Android  pro tips trilogy

LOOK AND FEEL

Page 48: Android  pro tips trilogy

Follow the guidelines

Please

Page 49: Android  pro tips trilogy

Step 4: Daily Use“I really like it, but...”

Page 50: Android  pro tips trilogy
Page 51: Android  pro tips trilogy
Page 52: Android  pro tips trilogy

Always up to dateUsers don't like to wait. • Location updated.• Data downloaded from web.

Page 53: Android  pro tips trilogy

Always up to dateUsers don't like to wait. • Location updated.• Data downloaded from web.

Page 54: Android  pro tips trilogy

Find the location fast.• Loop through all providers->getLastKnownLoc.

• If there is one or more location which is recent enough – return the most accurate one

• If not – Return the latest one.• In case of #2 – look for “fastest” provider:

• Coarse Accuracy && low power consumption. • Register for location update.

Page 55: Android  pro tips trilogy

Use passive location• >Gingerbread. • Passive location – Receive location update when other app is using

location provider. • Requires ACCESS_FINE_LOCATION permission.• Location.getProvider().

Page 56: Android  pro tips trilogy

Fused Location• Define your priorities • Let Google play services do the heavy lifting.

Page 57: Android  pro tips trilogy

// Create an instance of GoogleAPIClient.if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build();}

Using fused location

Page 58: Android  pro tips trilogy

@Overridepublic void onConnected(Bundle connectionHint) { //get last known location mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { //do something }

//register for location updates LocationRequest request = LocationRequest.create(); request.setInterval(minTime); request.setPriority(lowPowerMoreImportantThanAccuracy ? LocationRequest.PRIORITY.BALANCED_POWER_ACCURACY : LocationRequest.PRIORITY_HIGH_ACCURACY ); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);}

Using fused location

Page 59: Android  pro tips trilogy

@Overridepublic void onLocationChanged(Location location) { mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); updateUI();}

Using fused location

Page 60: Android  pro tips trilogy

Data updates

Page 61: Android  pro tips trilogy

The challenges:• When to update?• Where the data is coming from?• What to update?• How to update?

Page 62: Android  pro tips trilogy

When to update my App?• Right before the user launches it

• Assuming I have battery.• Assuming I have BW.

Page 63: Android  pro tips trilogy

Where the data is coming from?• My Server• 3rd Party Servers

Page 64: Android  pro tips trilogy

Where the data is coming from?• My Server• 3rd Party Servers

Page 65: Android  pro tips trilogy

What to Update? • What's Hot• Interesting stuff• Other

Page 66: Android  pro tips trilogy

BG Update

What's Hot

BG Update

Interesting Stuff Other

Page 67: Android  pro tips trilogy

BG Update

What's Hot

BG Update

Interesting Stuff Other

FCM Embedded

Page 68: Android  pro tips trilogy

BG Update

What's Hot

BG Update

Interesting Stuff Other

FCM Embedded

FCM + Back-off

Page 69: Android  pro tips trilogy

BG Update

What's Hot

BG Update

Interesting Stuff Other

FCM Embedded

FCM + Back-off

Daily / Bundled

Page 70: Android  pro tips trilogy
Page 71: Android  pro tips trilogy

Being Invisible• I don't need to think how the app works.• I never notice the app's work.• I am never being bothered by the app.

Page 72: Android  pro tips trilogy

Work OfflineQueue and Send transactions.• Use persistence layer.• Tape From Square.

Page 73: Android  pro tips trilogy

Work Semi-Offline• Be resilient to poor networks.

• Prioritize your transactions.• Be able to cancel transaction on the fly, or clear the Queue.• Adjust your app’s behavior and timeouts accordingly.

• Use Volley (or any other cool transport layer)

Page 74: Android  pro tips trilogy

Be efficient – Data usage

Page 75: Android  pro tips trilogy
Page 76: Android  pro tips trilogy

Radio Resource• Frequency is expensive. • Cell tower can not service 100% of its clients 100% of the time. • Frequency is dynamically allocated to clients.

• Cellular cells uses various multiplexing methods (OFDM/OFDMA, FTDMA).

Page 77: Android  pro tips trilogy

UMTS RRC States. (source: 3gpp)

Page 78: Android  pro tips trilogy

Radio State Machine

IDLE

FACH

DCH

Power

Bandwidth

Page 79: Android  pro tips trilogy

Radio State Machine

IDLE

FACH

DCH

Power

~10Sec tail time

~12-75 Sec tail time

Bandwidth

Page 80: Android  pro tips trilogy

Avoid bursty traffic• Transmit data “together”.

• Piggyback if needed.• Pre-fetch data for the next 2-5 minutes.

• Don't ping just to keep TCP connection alive• RRC != TCP Connection.• TCP connection is kept even in IDLE mode

Page 81: Android  pro tips trilogy

#1: Case study: Pandora• Music file streamed as single file.• Analytics data sends ~2KB every 62.5 seconds

Source: AT&T research

Page 82: Android  pro tips trilogy

#1: Case study: Pandora

0.2% of data consumed 46% of energy!

Page 83: Android  pro tips trilogy

Don't be HTTP rookie• Don't download what you already have.• Take care of server headers

• Max-age, expires. • Use conditional GET when cache expires

• Use “last modified” header.• Server return 304, with no body.

Page 85: Android  pro tips trilogy
Page 86: Android  pro tips trilogy

Adaptive App• Optimized for different User Experience.

• User has more than one device. • Be predictable.• Behave as expected

Page 87: Android  pro tips trilogy

Text Input• Specify the Edit Text input to show the right keyboard type.

• use android:inputType attribute• Four classes of keyboards:

• Plain text• Decimal Number• Phone Number• Date or Time

Page 88: Android  pro tips trilogy

Text Input• Plain text types:

• URIs• Email address• People's names• Postal address• Passwords

Page 89: Android  pro tips trilogy
Page 90: Android  pro tips trilogy
Page 91: Android  pro tips trilogy
Page 92: Android  pro tips trilogy
Page 93: Android  pro tips trilogy

mGeofenceList.add(new Geofence.Builder() // Set the request ID of the geofence. This is a string to identify this // geofence. .setRequestId(entry.getKey())

.setCircularRegion( entry.getValue().latitude, entry.getValue().longitude, Constants.GEOFENCE_RADIUS_IN_METERS ) .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());

Geofencing

Page 94: Android  pro tips trilogy

private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build();}

Geofencing

Page 95: Android  pro tips trilogy

private PendingIntent getGeofencePendingIntent() { // Reuse the PendingIntent if we already have it. if (mGeofencePendingIntent != null) { return mGeofencePendingIntent; } Intent intent = new Intent(this, GeofenceTransitionsIntentService.class); // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when // calling addGeofences() and removeGeofences(). return PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT);}

Geofencing

Page 96: Android  pro tips trilogy

LocationServices.GeofencingApi.addGeofences( mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent() ).setResultCallback(this);

Geofencing

Page 97: Android  pro tips trilogy

private void initTextToSpeech() { Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(intent,TTS_DATA_CHECK)}

Geek Magic - Text to speech

Page 98: Android  pro tips trilogy

private void onActivityResult(int request, int result, Intent data) { if (TTS_DATA_CHECK == request && Engine.CHECK_VOICE_DATA_PASS == result) { tts = new TextToSpeech(this, new on InitListener() { public void onInit(int status) { if (TextToSpeech == status) { ttsIsInit = true; } }

});}else { startActivity(new Intent(Engine.ACTION_INSTALL_TTS_DATA));}

}

Geek Magic - Text to speech

Page 99: Android  pro tips trilogy

private void say(Strint text) { if (null != tts && ttsIsInit) { tts.speak(text,TextToSpeech.QUEUE_ADD, null); }}

Geek Magic - Text to speech

Page 100: Android  pro tips trilogy

private void requestVoiceInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.String.voice_input_prompt); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); startActivityForResult(intent,VOICE_RECOGNITION);}

Geek Magic - Speech Recognition

Page 101: Android  pro tips trilogy

private void requestVoiceInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.String.voice_input_prompt); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); startActivityForResult(intent,VOICE_RECOGNITION);}

Geek Magic - Speech Recognition

Page 102: Android  pro tips trilogy

Geek Magic – Speech Recognition• More than one result is returned.• Loop through all results, taking context in mind.

Page 103: Android  pro tips trilogy

<intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:host="mysite.com" android:scheme="http"/></intent-filter>

Intercept Links

Page 104: Android  pro tips trilogy

Follow the guidelines

Be FreshBe InvisibleBe EfficientBe AdaptiveBe Psychic

Make it quick

Start!

minimize permissions

Page 105: Android  pro tips trilogy

Thank You!+RanNachmany@shed2k