Android Whats running in background

28
Welcome Android: What’s running in Background

Transcript of Android Whats running in background

Page 1: Android Whats running in background

Welcome

Android: What’s running in Background

Page 2: Android Whats running in background

Road to perdition Hello world

UI Thread (ANR)

Worker Thread (CalledFromWrongThreadException)

Handler AsyncTask (Component Destroyed)

Page 3: Android Whats running in background
Page 4: Android Whats running in background

Better Call ServiceService (when no one is looking)

Page 5: Android Whats running in background

Services / Threads (war of two worlds)A service is not an alternative to background threads but it provides another scope in which to run your worker threads! • The thread should stay alive, even when the user is not interacting with the application.

• In addition, a Service can be started from another application using an Intent.

Naval Kishore Barthwal
need a diagram activity thread and service timeline
Page 6: Android Whats running in background
Page 7: Android Whats running in background

Golden Rule

DO NOT let services live longer than they are

needed.

Page 8: Android Whats running in background
Page 9: Android Whats running in background
Naval Kishore Barthwal
get service life cycle diagram
Page 10: Android Whats running in background

OptionsGCM

BroadcastReceiver

LocalBroadcastReceiver

WakefulBroadcastReceiver

HandlerThreads

AsyncTaskLoaders

ThreadPoolExecutor

ContentProvider

IntentService

SyncAdapter

JobScheduler

DownloadManager

Page 11: Android Whats running in background

ServicesNormal Service

Intent Service [special case of normal service]

Bound Service [in same process]

Bound Service [in separate process-> Messenger]

Bound Service [in separate application-> AIDL]

Page 12: Android Whats running in background

onStartCommandSTART_STICKY to automatically restart if killed, to keep it

active.

START_REDELIVER_INTENT for auto restart and retry if the service was killed before stopSelf(). This is needed for services receiving inputs.

START_NOT_STICKY to not automatically restarts if killed, but wait for next startService() call. For non-essential services.

START_STICKY_COMPATIBILITY with Android 1.6 and earlier legacy onStart() method.

Page 13: Android Whats running in background

Start Services

• What happens if task is removed from recent?

• What happens if force stop?

• What happens if exception occur in running process?

Page 14: Android Whats running in background

Local Bound Services

What would happen if try to make connect IPC in LocalBinder?

Page 15: Android Whats running in background
Page 16: Android Whats running in background
Page 17: Android Whats running in background
Page 18: Android Whats running in background
Page 19: Android Whats running in background
Page 20: Android Whats running in background

Sync on AndroidWell, you probably use it everyday...

Page 21: Android Whats running in background

Big picture

Page 22: Android Whats running in background

Big picture

Page 23: Android Whats running in background

Account managerSingle sign-in

Secure storage

Share auth-token

Ref: http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/Account manager

Page 24: Android Whats running in background

Content providerCommon storage

CRUD operations

Require permissions

ContactsProvider:

<uses-permission android:name="android.permission.READ_CONTACTS"><uses-permission android:name="android.permission.WRITE_CONTACTS">

Page 26: Android Whats running in background

Download managerRun in a background

Network type check

Maintain queue

Page 27: Android Whats running in background

Job Scheduler❏Introduced in Android 5.0 Lollipop (API 21)

❏When you would use this- Tasks that should be done once the device is connected to a power supply- Tasks that require network access or a Wi-Fi connection.- Task that are not critical or user facing- Tasks that should be running on a regular basis as batch where the timing is not critical

Page 28: Android Whats running in background

❏ Job Scheduler

- Persists the data across device reboots

- Certain criteria can be set

- Service runs when set criteria is available

- Delay can be set

- Use for periodical running task

❏ Alarm Manager

- Broadcast listener to wake up

- Data get lost on device reboot

- Service keeps running until you manually turns off or system do it for you

- Use for scheduling task at specific time