您的位置:首页 > 其它

深入理解 IntentService 与实践

2017-03-08 17:02 381 查看
1,理解为何Android中引入IntentService,解决的是什么场景下的需求?

2,IntentService的使用步骤是?

一,理解IntentService:

1,定义:

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.
也就是IntentService是一个基于Service的类;用于处理异步请求。
可以通过StartService(Intent)来启动该IntentService,然后IntentService启动一个后台线程去处理任务。

2,在Android中,有涉及到耗时操作的,我们都是交给Service处理,然后在Service 中起线程去处理。

而IntentService优雅地处理了起一个Service然后开一个线程处理耗时操作的动作。

二,IntentService 使用:
1,直接启动:像启动一个Service一样,启动一个Service:startService()
2, 执行Handler的操作:重写onHandleIntent()
@Override
protected void onHandleIntent(Intent intent) {
// handleUploading(path) ----------
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: