Architecture your android_application

5,401 views
5,312 views

Published on

DroidCon 2010

Published in: Technology, Education
0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,401
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
144
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide

Architecture your android_application

  1. 1. Architecture your Android Application Mark Brady
  2. 2. Structure <ul><li>20 Minutes talk
  3. 3. 10 Minutes of Q&A
  4. 4. Source Code: http://github.com/zedray/Android-Framework-Prototype/
  5. 5. Market App Framework Prototype [BETA] </li></ul>
  6. 6. About Me <ul><li>Mark Brady
  7. 7. Android developer at Vodafone
  8. 8. Background in web design, back-end development and Java ME
  9. 9. Android Blog: http://blog.zedray.com/category/android/ </li></ul>“ Quick Dial / Call Log Widget”
  10. 10. Overview <ul><li>Component life-cycles </li><ul><li>Activity, Service, Application... </li></ul><li>Communicating between components </li><ul><li>RPC, Handlers, Binding... </li></ul><li>Managing different life-cycles </li><ul><li>Framework , Example code... </li></ul></ul>
  11. 11. <ul>Component life-cycles </ul><ul><li>Android provides various life-cycle components.
  12. 12. Lack of guidance on how to use them. </li></ul>Service Application Activity
  13. 13. Activity #1 <ul><li>Chain of Activities = 1x Task </li></ul>1 2 3 4 Home Screen
  14. 14. Activity #2 <ul><li>Activities are destroyed, but persisted as Bundles </li></ul>3 4 Bundle Bundle
  15. 15. Activity #3 <ul><li>Use the Bundle to reinstate an Activity on the Tasks back-stack. </li></ul>Bundle Bundle 2 3 4
  16. 16. Activity #4 <ul><li>On Screen onResume() - onPause()
  17. 17. Running onStart() - onStop()
  18. 18. Finishing onStop() and onDestroy() may not be called if a process is killed (i.e. due to low memory) </li></ul>
  19. 19. Activity #5 <ul><li>Do your “stop work” in onPause() Release those resources.
  20. 20. Do long running work in AsyncTask Separate non-UI thread will avoid ANR error. </li></ul>
  21. 21. Activity #6 <ul><li>Difficult to maintain execution state in an application made up of multiple activities.
  22. 22. Code from multiple activities can be running in the background. </li></ul>No good definition for when an application has finished .
  23. 23. <ul>Service #1 </ul><ul><li>Execute in the background, regardless of which Activity is on screen. </li></ul>Time 1 2 3 4 3
  24. 24. <ul>Service #2 </ul><ul><li>Pause or block threads to reduce power consumption (e.g. block on long poll networking). </li></ul>Long running services invite “Service Killer” applications. Time
  25. 25. <ul>Service #3 </ul><ul><li>Trigger execution from UI, Broadcast Receivers and Alarms. </li></ul>Time UI Broadcast Receiver
  26. 26. <ul>Service #4 </ul><ul><li>Consider using Notifications to show the user that work is being done in the background. </li></ul>Think about “User Goals” during your design.
  27. 27. <ul>Application </ul><ul><li>Longest running component, used for maintaining global state. </li></ul>Old execution model, i.e. not very Android . Application 1 2 3 4 3
  28. 28. <ul>Sleep and low memory </ul>1 2 3 2
  29. 29. Communicating between components <ul><li>Remote procedure calls (i.e. AIDL)
  30. 30. Singletons
  31. 31. Handlers
  32. 32. Service binding </li></ul>
  33. 33. Remote procedure calls (i.e. AIDL) <ul><li>Do not use, as by default all your components run in the same process.
  34. 34. If you think your application is an exception: “Drag me to Hell - Optimizations for AIDL” Ronan Schwarz, Tic Mobile, 5.30pm. </li></ul>(Android Interface Definition Language)
  35. 35. Java Singletons Flip side of running in on process/JVM is that objects in other components can be called as singletons: public class SingletonObject { private static SingletonObject ref; private SingletonObject() { // Do nothing. } public static SingletonObject getSingletonObject() { if (ref == null) { ref = new SingletonObject(); } return ref; } } Calling your singleton SingletonObject obj = SingletonObject.getSingletonObject();
  36. 36. Handlers <ul><li>Handler sends (or posts) messages to another components UI Thread. </li></ul>private final Handler mHandler = new Handler() { @Override public void handleMessage(final Message message) { processMessage(message); } }; Post message from another component: mHandler.sendMessage(message);
  37. 37. Service binding #1 <ul><li>Problem with passing Singletons is that they give no clue as to whether the originating component still exists.
  38. 38. Calling Serivce.onBind() will create a Service that will remain running for as long as the ServiceConnection is established, or until a corresponding Service.onUnbind() is called. </li></ul>Service
  39. 39. Service binding #2 <ul><li>Service can return a custom binder (non-AIDL example): </li></ul>public class MyService extends Service { @Override public final IBinder onBind(final Intent intent) { return new MyBinder(); } public class MyBinder extends Binder { public final Handler getHandler() { return mHandler; } } } Service
  40. 40. Service binding #3 Bind to this Service: mContext.bindService(new Intent(mContext, MyService.class), mServiceConnection, Context.BIND_AUTO_CREATE); Unbind from this Service: mContext.unbindService(mServiceConnection); Monitor Service Connection: private final ServiceConnection mServiceConnection = new ServiceConnection() { public void onServiceConnected(final ComponentName className, final IBinder service) { mHandler = ((MyService.MyBinder) service).getHandler(); } public void onServiceDisconnected(final ComponentName className) { mHandler = null; } }; Service
  41. 41. Managing different life-cycles <ul><li>Framework
  42. 42. Source code http://github.com/zedray/Android-Framework-Prototype/
  43. 43. Market App Framework Prototype [BETA] </li></ul>
  44. 44. Framework #1
  45. 45. Framework #2
  46. 46. Framework #3
  47. 47. Framework #4 <ul><li>Queue heavy design Your application should be no heaver.
  48. 48. State Handled in a single place, so implement database synchronization here.
  49. 49. Worker Thread Call stopSelf() as soon as all user goals are complete. </li></ul>
  50. 50. Source Code – BaseActivity #1 public class MyActivity extends BaseActivity { @Override public final void onCreate(final Bundle savedInstanceState) { // Database I/O. getCache() .getSomeStateValue(); getDb() .setSomeStateValue(&quot;Foo&quot;, &quot;Bar&quot;); } @Override protected final void onResume() { // Update the UI on resume. updateUi(); super.onResume(); } ...
  51. 51. Source Code – BaseActivity #2 ... public final void doSomeTask() { // Post something to Service queue. getServiceQueue().postToService (Type.DO_SOME_TASK, null); } @Override public final void post (final Type type, final Bundle bundle) { // Implement an incoming UI message, depending on type. switch (type) { case SHOW_SOME_UI: showSomeUi(); break; case UPDATE_UI: updateUi(); break; default: /** Let the BaseActivity handle other message types. */ super.post(type, bundle); break; } } }
  52. 52. Source Code – WorkerThread #1 public class WorkerThread extends Thread { public final void run() { while ( mWorkQueue .size() > 0) { synchronized ( mWorkQueue ) { // Remove a task from the queue. Message message = mWorkQueue .remove(0); ... } // Do the task, depending on type. switch (type) { case DO_SOME_TASK: doSomeTask(bundle); break; } } // No work to do, so kill the service. mMyService .stopSelf(); } ...
  53. 53. Source Code – WorkerThread #2 ... private void doSomeTask(final Bundle bundle) { mCache .setState(&quot;Loading&quot;); mUiQueue . postToUi (Type.UPDATE_UI, null, true); // Do some setup work. ... mCache .setState(&quot;Running&quot;); mUiQueue . postToUi (Type.UPDATE_UI, null, true); // Do some real work. ... mCache .setState(&quot;Finished&quot;); mUiQueue.postToUi (Type.UPDATE_UI, null, true); if (someCondition) { // Show some custom UI (do not suppress). mUiQueue.postToUi (Type.SHOW_SOME_UI, null, false ); } } }
  54. 54. Architecture your Android Application <ul><li>Mark Brady http://blog.zedray.com/category/android/
  55. 55. Framework Source http://github.com/zedray/Android-Framework-Prototype/
  56. 56. Market App Framework Prototype [BETA] </li></ul>

×