开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜