深入理解 IntentService 与实践

    xiaoxiao2021-03-25  140

    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) ---------- }

    转载请注明原文地址: https://ju.6miu.com/read-3812.html

    最新回复(0)