Service vs IntentService in the Android platform

后端 未结 11 576
挽巷
挽巷 2020-11-22 03:38

I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?

I also bel

11条回答
  •  有刺的猬
    2020-11-22 04:15

    Adding points to the accepted answer:

    See the usage of IntentService within Android API. eg:

    public class SimpleWakefulService extends IntentService {
        public SimpleWakefulService() {
            super("SimpleWakefulService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {  ...}
    

    To create an IntentService component for your app, define a class that extends IntentService, and within it, define a method that overrides onHandleIntent().

    Also, see the source code of the IntentService, it's constructor and life cycle methods like onStartCommand...

      @Override
        public int More ...onStartCommand(Intent intent, int flags, int startId) {
           onStart(intent, startId);
            return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
        }
    

    Service together an AsyncTask is one of best approaches for many use cases where the payload is not huge. or just create a class extending IntentSerivce. From Android version 4.0 all network operations should be in background process otherwise the application compile/build fails. separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. For more discussion of this topic, see the blog post

    from Android developers guide:

    IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work.

    Design pattern used in IntentService

    : This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

    All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

    The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask.

    An IntentService has a few limitations:

    It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity. Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished. An operation running on an IntentService can't be interrupted. However, in most cases

    IntentService is the preferred way to simple background operations

    **

    Volley Library

    There is the library called volley-library for developing android networking applications The source code is available for the public in GitHub.

    The android official documentation for Best practices for Background jobs: helps better understand on intent service, thread, handler, service. and also Performing Network Operations

提交回复
热议问题