IntentService与ResultReceiver的应用

转自 http://lyzhanghai.iteye.com/blog/947504


在google的I/O大会中关于“Writing zippy Android apps”,有讲过用IntentService的问题,但是因为API文档中对IntentService描述不是很详细,所以很少人使用IntentService。

android.app.IntentService
“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. 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.”

有 很多种模式可以运用在RESTful Client(想要了解更多的RESTful Client的模式可以参见I/O大会Developing Android REST client applications,但是苦于没有具体demo可以参见),如果不是特别特别复杂的RESTful Web Service, 我们可以使用ResultReceiver 和 IntentService。
举个例子,你想从web service取一些数据:
1.    调用startService。
2.    service中开始操作处理,并且通过消息告诉activity处理已经开始。
3.    activity处理消息并且显示进度条
4.    service完成处理并且返回给activity需要的数据。
5.    activity处理数据。
6.    service通过消息告诉activity处理完成,并且kill掉自己。
7.    activity取得消息并且结束掉进度条。

public class HomeActivity extends Activity implements ResultReceiver {
    public void onCreate(Bundle savedInstanceState) {
        ...
        final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
        intent.putExtra("receiver", this);
        intent.putExtra("command", "query");
        startService(intent);
    }

    public void onReceiveResult(int resultCode, Bundle resultData) {
        switch (resultCode) {
        case RUNNING:
            //show progress
            break;
        case FINISHED:
            List results = resultData.getParcelableList("results");
            // do something interesting
            // hide progress
            break;
        case ERROR:
            // handle the error;
            break;
    }
}

public class QueryService extends IntentService {
    protected void onHandleIntent(Intent intent) {
        final ResultReceiver receiver = intent.getParcelableExtra("receiver");
        String command = intent.getStringExtra("command");
        Bundle b = new Bundle();
        if(command.equals("query") {
            receiver.send(STATUS_RUNNING, Bundle.EMPTY);
            try {
                // get some data or something          
                b.putParcelableArrayList("results", results);
                receiver.send(STATUS_FINISHED, b)
            } catch(Exception e) {
                b.putString(Intent.EXTRA_TEXT, e.toString());
                receiver.send(STATUS_ERROR, b);
            }    
        }
        this.stopSelf();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值