UA Mobile 2012 (English)

284 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
284
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

UA Mobile 2012 (English)

  1. 1. Serving your business logically Dmitry Malykhanov DataArtThe Android robot is reproduced or modified from work created and shared by Google and used according to terms described in theCreative Commons 3.0 Attribution License.
  2. 2. First of all why we need servicesHow is it done service variantsThen start executionExpedited delivery receiving your data
  3. 3. First of allwhy exactly we need services
  4. 4. Alternatives"Just" Code: Service:● Thread (pool) ○ return to UI thread● AsyncTask ○ serialized ○ short lifespan● Application ? ○ garbage on the heap ○ no IPC
  5. 5. Alternatives"Just" Code: Service:● Thread (pool) ○ return to UI thread● AsyncTask ○ serialized ○ short lifespan● Application ? ○ garbage on the heap ○ no IPC
  6. 6. Alternatives"Just" Code: Service:● Thread (pool) ○ return to UI thread● AsyncTask ○ serialized ○ short lifespan● Application ? ○ garbage on the heap ○ no IPC
  7. 7. Alternatives"Just" Code: Service:● Thread (pool) ● Thread/Pool/Etc. ○ return to UI thread ○ flexible control● AsyncTask ● Wake Locks ○ serialized ○ guaranteed CPU ○ short lifespan ● Lifetime● Application ○ on its own ○ garbage on the heap ○ no IPC
  8. 8. Alternatives"Just" Code: Service:● Thread (pool) ● Thread/Pool/Etc. ○ return to UI thread ○ flexible control● AsyncTask ● Wake Locks ○ serialized ○ guaranteed CPU ○ short lifespan ● Lifetime● Application ○ on its own ○ garbage on the heap ○ no IPC
  9. 9. Alternatives"Just" Code: Service:● Thread (pool) ● Thread/Pool/Etc. ○ return to UI thread ○ flexible control● AsyncTask ● Wake Locks ○ serialized ○ guaranteed CPU ○ short lifespan ● Lifetime● Application ○ on its own ○ garbage on the heap ○ no IPC
  10. 10. Whats the price?● IPC● Resources● Security
  11. 11. Whats the price? ● IPC ● Resources ● SECURITYAndroidManifest.xml: <service ... android:exported="false" />
  12. 12. How it is doneservice kinds and variants
  13. 13. Simple serviceIntentService:● single thead● requested enqueued● clear lifecyclehttp://goo.gl/TvtbV
  14. 14. Cloning simple servicesTwo/three tasks at the same time:<service android:name=".FirstIntentService" /><service android:name=".SecondIntentService" /><service android:name=".ThirdIntentService" />
  15. 15. Complex servicesExecutorService:● ThreadPoolExecutor● Scheduled ThreadPoolExecutorhttp://goo.gl/mwV90
  16. 16. Lifecycle: issueIntentService ThreadPoolService ?
  17. 17. Lifecycle: solution startId -> ConcurrentLinkedQueue requests: 1, 2, 3, 4, 5Thread 1 3 4 4 4 idleThread 2 2 2 5 idle idleQueue 1,2,3,4,5 2,3,4,5 3,4,5 4, 5 5 STOP
  18. 18. Thenstart the proper execution
  19. 19. What and how?switch (cmdId) { case CMD_POLL: poll(); How to expand? break; case CMD_UPLOAD: upload(); break; if ("poll".equals(cmd)) { poll(); } else if ("upload".equals(cmd)) { upload(); }
  20. 20. IndependenceGoF: Command Patterninterface Command { void execute(Context);} 4http://goo.gl/hwFY9
  21. 21. Unique IDspublic static final int CMD_POLL = 10;public static final int CMD_UPLOAD = 20;... Command.class.hashCode() System.identityHashCode()
  22. 22. Commands deliveryswitch (commandId) { case CMD_POLL: return new PollCommand().execute(); case CMD_UPLOAD: return new UploadCommand().execute();... abstract class Command implements Parcelable { public abstract void execute(...); }
  23. 23. Out of band executionSerialize/prioritize commands, e.g.: Command.isSerial()frameworks/base/core/java/android/os/AsyncTask.java:private static class SerialExecutor implements Executor {...
  24. 24. Expedited deliveryreceiving your data
  25. 25. Wrapping the data ● Entity Bean● android.os.Bundle
  26. 26. Wrapping the data ● Entity Bean YES: type-safe ○ NO: CPU cycles ○ NO: IPC ○● android.os.Bundle ○ YES: no type conversion ○ NO: Parcelable only
  27. 27. Wrapping the data ● Entity Bean YES: type-safe ○ NO: CPU cycles ○ NO: IPC ○● android.os.Bundle ○ YES: no type conversion ○ NO: Parcelable only
  28. 28. Universal containerpublic class EntityBean implements Parcelable{...http://goo.gl/QSFPe
  29. 29. Serializable? No way!Serializable - the best of all? Parcelable ^ Parcelable S p 5..10 times e e FASTER! d ^ Serializable
  30. 30. One way ticketDelivery transport: ● broadcastIssues: ● IPC ● wakelock ● receivers lifecycle LocalBroadcastManagerhttp://goo.gl/2h80u
  31. 31. Batch themDelivery transport: ● database (provider)Issues: ● delays ● notification "storm"http://goo.gl/ZPoei
  32. 32. Messenger bagDelivery transport: ● singletonIssue: ● single process ● memory consumption ● tight couplinghttp://goo.gl/yfHPn http://goo.gl/5AR9h
  33. 33. Courier deliveryimport android.os.ResultReceiver; private ResultReceiver receiver = new ResultReceiver(handler) { ... }... new PollCommand(receiver) .start(...);http://goo.gl/rwhw0
  34. 34. Zombie-problem Receiver Sender Activity Service Receiver Sender onDestroy Activity Service Receiver Activity Receiver Sender Sender onCreateActivity Service
  35. 35. Eternal lifeApplication Receiver Sender Service ActivityApplication Receiver Sender Service Activity Activity
  36. 36. Eternal life: problemsApplication Receiver Sender ? Service Activity Garbage CollectedApplication Receiver Sender Service Activity
  37. 37. Life without problemsApplication Buffer Receiver Sender Service Activity
  38. 38. Smooth: UI Threadprivate ResultReceiver receiver = new ResultReceiver(new Handler()) { ... } UI Thread!
  39. 39. Smooth: Backgroundprivate Handler handler = new Handler(handlerThread);private ResultReceiver receiver = new ResultReceiver(handler) { ... onReceive(...) { ... runOnUiThread(...) UI } }
  40. 40. Smooth: altogetherprivate ResultReceiver receiverUi = new ResultReceiver(uiThrdHandler) { ... }private ResultReceiver receiverBg = new ResultReceiver(bgThrdHandler) { ... }
  41. 41. Best of both worlds join all in one
  42. 42. Universal all-in-one service GC AIDL Binder Queue get(id) submit(id, command) Receiver Command CommandService
  43. 43. Serving your business logically Dmitry Malykhanov Questions?
  44. 44. Serving your business logicallyThe Android robot is reproduced or modified from work created and shared by Google and used according to terms described in theCreative Commons 3.0 Attribution License.

×