Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android Whats running in background


Published on

  • Be the first to comment

Android Whats running in background

  1. 1. Welcome Android: What’s running in Background
  2. 2. Road to perdition Hello world UI Thread (ANR) Worker Thread (CalledFromWrongThreadException) Handler AsyncTask (Component Destroyed)
  3. 3. Better Call Service Service (when no one is looking)
  4. 4. 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.
  5. 5. Golden Rule DO NOT let services live longer than they are needed.
  6. 6. Options GCM BroadcastReceiver LocalBroadcastReceiver WakefulBroadcastReceiver HandlerThreads AsyncTaskLoaders ThreadPoolExecutor ContentProvider IntentService SyncAdapter JobScheduler DownloadManager
  7. 7. Services Normal 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]
  8. 8. onStartCommand START_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.
  9. 9. Start Services • What happens if task is removed from recent? • What happens if force stop? • What happens if exception occur in running process?
  10. 10. Local Bound Services What would happen if try to make connect IPC in LocalBinder?
  11. 11. Sync on Android Well, you probably use it everyday...
  12. 12. Big picture
  13. 13. Big picture
  14. 14. Account manager Single sign-in Secure storage Share auth-token Ref: android-authenticator/Account manager
  15. 15. Content provider Common storage CRUD operations Require permissions ContactsProvider: <uses-permission android:name="android.permission.READ_CONTACTS"> <uses-permission android:name="android.permission.WRITE_CONTACTS">
  16. 16. setIsSyncable (Account account, String authority, int syncable) setSyncAutomatically (Account account, String authority, boolean sync) addPeriodicSync (Account account, String authority, Bundle extras, long pollFrequency) requestSync (Account account, String authority, Bundle extras) How the sync adapter works
  17. 17. Download manager Run in a background Network type check Maintain queue
  18. 18. 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
  19. 19. ❏ 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