Monday, March 11, 2019

Android开发笔记-ch3.4.8 Service/IntentService, Process/Thread


3.4.8 Service/IntentService, Process/Thread

For Service, refer to vogella. A service is a component which runs in the background without direct interaction with the user. As the service has no user interface, it is not bound to the lifecycle of an activity. It is possible to assign services the same priority as foreground activities. In this case it is required to have a visible notification active for the related service. It is frequently used for services which play videos or music. A commonly used pattern for a service implementation is to create and run a new Thread in the service to perform the processing in the background and then to terminate the service once it has finished the processing. Services which run in the process of the application are sometimes called local services. OnCreate() method would be invoked first (only once), then OnStartCommand().
You can also extend the IntentService class for your service implementation. The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates itself automatically. An example for its usage would be downloading certain resources from the internet. The IntentService class offers the onHandleIntent() method which will be asynchronously called by the Android system.
Refer this for bind an app and a service: A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).
For Process/Thread, refer to this. A thread created inside a service has not much difference from a thread created in an Acitivity, other than the service is independent of Activity which starts it, ie. Even the service and its thread will keep running after the Activity is closed. A thread spawn by an Activity's main thread will still response even the Activity is put to background, but not destroyed.
Quoted from here: The opening question, whether to use a service or a background thread is not an either/or question. Whenever you have a longer task to perform you should delegate it to a worker thread. The question should really be: “Who should be managing the worker thread?”. Implementing a service tells the Android system that 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. If neither of these two properties is required by your background task then you should not use a Service but a background thread in another context. Worker threads are not managed by the Android system and you should terminate them when they are no longer needed. This means that a thread that is created in Activity.onStart() should be terminated in Activity.onStop(). Otherwise you will be creating new threads every time that the activity starts, resulting in leaking threads. This can be extremely devastating for performance.
A table from here which might not be all correct:



Service Thread IntentService AsyncTask
When to use ? Task with no UI, but shouldn't be too long. Use threads within service for long tasks. - Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)
- Long task usually with no communication to main thread.
- If communication is required, can use main thread handler or broadcast intents
- When callbacks are needed (Intent triggered tasks). 
- Small task having to communicate with main thread.
- For tasks in parallel use multiple instances OR Executor
 
Trigger Call to method
onStartService()
Thread start() method Intent Call to method execute()
Triggered From (thread) Any thread Any Thread Main Thread (Intent is received on main thread and then worker thread is spawed) Main Thread
Runs On (thread) Main Thread, may create worker thread Its own thread Separate worker thread Worker thread. However, Main thread methods may be invoked in between to publish progress.
Limitations
Drawbacks
May block main thread - Manual thread management

- Code may become difficult to read
- Cannot run tasks in parallel.

- Multiple intents are queued on the same worker thread.
- one instance can only be executed once (hence cannot run in a loop) 
- Must be created and executed from the Main thread

0 Comments:

Post a Comment