Bind to service from new Context for configuration changes or bind from app context?

前端 未结 4 1755
囚心锁ツ
囚心锁ツ 2021-02-02 02:38

I\'m trying to work out if bound service is appropriate for doing background work in my app. The requirements are that various application components can make web requests throu

4条回答
  •  时光说笑
    2021-02-02 02:48

    There is a much easier way to handle this situation called an IntentService which you can read more about here. From the android site:

    "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"

    Rather than binding your service to your activity you can start long-running actions on a background thread by simply using an intent that starts your IntentService

    public class RSSPullService extends IntentService {
    
        @Override
        protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
        }
    }
    

    This is an example taken from the android docs. You would send an intent with the relevant data then handle that data within the service to do what you want. For example, you could just add a priority flag to your intent so your service knows which requests come before others.

    The benefit of an intent service is that it runs on a background thread and is not tied to the lifecycle of the starting activity. That means you configuration changes should not have an effect on the service execution.

    When your service is done you can report work status by using a local broadcast - either sending the results directly back to the activity (via broadcast receiver) or possibly even through onNewIntent() (though getting that to work is a bit more clunky.

    Edit - answer questions in comment

    IntentService is a relatively small class. This makes it easy to modify. The stock code for IntentService calls stopSelf() and dies when it runs out of work to do. This can be easily fixed. Examining the source for the IntentService (see the previous link) you can see that it pretty much works off a queue already, receiving messages in onStart() and then executing them in the order received as described in the comment. Overriding onStart() will allow you to implement a new queue structure to meet your needs. Use the example code there for how to handle the incoming message and get the Intent then just create your own data structure for handling priority. You should be able to start/stop your web requests in the IntentService the same way you would in a Service. Thus by overriding onStart() and onHandleIntent() you should be able to do what you want.

提交回复
热议问题