Running in a Background Service 之Creating a Background Service

本文详细介绍了Android中的IntentService组件,解释了其无法直接与UI交互的原因,并提供了如何通过继承IntentService来处理后台任务的具体实例。此外还说明了IntentService的任务处理机制及在AndroidManifest.xml中的声明方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IntentService 不能和UI交互,如果想将结果呈现在UI上,必须将结果发给Activity,让其帮忙显示
其次IntentService 只能顺序的处理Request,如果上一个Request没有处理完,则发送一个Request的时候必须等待.
IntentService 中的操作不能被中断.


一般通过实现IntentService的子类来新建一个IntentService,
public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}


所有的service 必须在manifest文件中声明
 <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        ...
        <!--
            Because android:exported is set to "false",
            the service is only available to this app.
        -->
        <service
            android:name=".RSSPullService"
            android:exported="false"/>
        ...
    <application/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值