开发者

How to not end an Android IntentService when onHandleIntent() finishes?

开发者 https://www.devze.com 2023-04-04 11:32 出处:网络
I start an IntentService.In onHandleInt开发者_如何转开发ent() I make an HTTP request with a Handler that is called when the request completes, like so:

I start an IntentService. In onHandleInt开发者_如何转开发ent() I make an HTTP request with a Handler that is called when the request completes, like so:

public function onHandleIntent(){
    makeRequest("http://somehthing.com", new Handler(){
        doStuff();
    });
}

onHandleIntent() finishes immediately, before the HTTP request returns. The request does return, but the ServiceIntent thread has already been destroyed and doStuff() doesn't ever run.

What is the best way to handle a callback Handler structure like this with a ServiceIntent?


Unfortunately, you don't. IntentService is meant to queue off synchronous blocks of work, complete them in another thread, then finish. You'll have to create your own Service. You can take a look at the source code for IntentService and start from there. Most importantly, you'll want to call stopSelf after the callback is completed instead of after onHandleIntent.

0

精彩评论

暂无评论...
验证码 换一张
取 消