architecture your android_application

Download Architecture your android_application

If you can't read please download the document

Upload: mark-brady

Post on 12-Jun-2015

4.916 views

Category:

Technology


0 download

DESCRIPTION

DroidCon 2010

TRANSCRIPT

  • 1. Architecture your Android Application Mark Brady

2. Structure

  • 20 Minutes talk

3. 10 Minutes of Q&A 4. Source Code: http://github.com/zedray/Android-Framework-Prototype/ 5. Market App Framework Prototype [BETA] 6. About Me

  • Mark Brady

7. Android developer at Vodafone 8. Background in web design, back-end development and Java ME 9. Android Blog: http://blog.zedray.com/category/android/ Quick Dial / Call Log Widget 10. Overview

  • Component life-cycles
  • Activity, Service, Application...

Communicating between components

  • RPC, Handlers, Binding...

Managing different life-cycles

  • Framework , Example code...

11.

  • Component life-cycles
  • Android provides various life-cycle components.

12. Lack of guidance on how to use them. Service Application Activity 13. Activity #1

  • Chain of Activities = 1x Task

1 2 3 4 Home Screen 14. Activity #2

  • Activities are destroyed, but persisted as Bundles

3 4 Bundle Bundle 15. Activity #3

  • Use the Bundle to reinstate an Activity on the Tasks back-stack.

Bundle Bundle 2 3 4 16. Activity #4

  • On Screen onResume() - onPause()

17. Running onStart() - onStop() 18. Finishing onStop() and onDestroy() may not be called if a process is killed (i.e. due to low memory) 19. Activity #5

  • Do your stop work in onPause() Release those resources.

20. Do long running work in AsyncTask Separate non-UI thread will avoid ANR error. 21. Activity #6

  • Difficult to maintain execution state in an application made up of multiple activities.

22. Code from multiple activities can be running in the background. No good definition for when an applicationhas finished . 23.

  • Service #1
  • Execute in the background, regardless of which Activity is on screen.

Time 1 2 3 4 3 24.

  • Service #2
  • Pause or block threads to reduce power consumption (e.g. block on long poll networking).

Long running services invite Service Killer applications. Time 25.

  • Service #3
  • Trigger execution from UI, Broadcast Receivers and Alarms.

Time UI Broadcast Receiver 26.

  • Service #4
  • Consider using Notifications to show the user that work is being done in the background.

Think about User Goals during your design. 27.

  • Application
  • Longest running component, used for maintaining global state.

Old execution model, i.e. not very Android . Application 1 2 3 4 3 28.

  • Sleep and low memory

1 2 3 2 29. Communicating between components

  • Remote procedure calls (i.e. AIDL)

30. Singletons 31. Handlers 32. Service binding 33. Remote procedure calls (i.e. AIDL)

  • Do not use, asby defaultall your components run in the same process.

34. If you think your application is an exception: Drag me to Hell - Optimizations for AIDL Ronan Schwarz, Tic Mobile, 5.30pm. (Android Interface Definition Language) 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. Handlers

  • Handler sends (or posts) messages to another components UI Thread.

private final Handler mHandler = new Handler() { @Override public void handleMessage(final Message message) { processMessage(message); } }; Post message from another component: mHandler.sendMessage(message); 37. Service binding #1

  • Problem with passing Singletons is that they give no clue as to whether the originating component still exists.

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. Service 39. Service binding #2

  • Service can return a custom binder (non-AIDL example):

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. 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. Managing different life-cycles

  • Framework

42. Source code http://github.com/zedray/Android-Framework-Prototype/ 43. Market App Framework Prototype [BETA] 44. Framework #1 45. Framework #2 46. Framework #3 47. Framework #4

  • Queue heavy design Your application should be no heaver.

48. State Handled in a single place, so implement database synchronization here. 49. Worker Thread Call stopSelf() as soon as all user goals are complete. 50. Source Code BaseActivity #1 public class MyActivity extendsBaseActivity{ @Override public final void onCreate(final Bundle savedInstanceState) { // Database I/O. getCache() .getSomeStateValue(); getDb() .setSomeStateValue("Foo", "Bar"); } @Override protected final void onResume() { // Update the UI on resume. updateUi(); super.onResume(); } ... 51. Source Code BaseActivity #2 ... public final void doSomeTask() { // Post something to Service queue. getServiceQueue().postToService (Type.DO_SOME_TASK, null); } @Override public final voidpost (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. Source Code WorkerThread #1 public classWorkerThreadextends 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. Source Code WorkerThread #2 ... private void doSomeTask(final Bundle bundle) { mCache .setState("Loading"); mUiQueue . postToUi (Type.UPDATE_UI, null, true); // Do some setup work. ... mCache .setState("Running"); mUiQueue . postToUi (Type.UPDATE_UI, null, true); // Do some real work. ... mCache .setState("Finished"); 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. Architecture your Android Application

  • Mark Brady http://blog.zedray.com/category/android/

55. Framework Source http://github.com/zedray/Android-Framework-Prototype/ 56. Market App Framework Prototype [BETA]