Android Service系列(十六)创建后台Service

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, anIntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask

翻译:IntentService很棒,比AsyncTask棒。

 

 

An IntentService has a few limitations:

  • It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
  • Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
  • An operation running on an IntentService can't be interrupted.

翻译:IntentService也有不足的地方

(1)不能直接操作UI

  (2)   只能串行

(3)不能被打断

 

However, in most cases an IntentService is the preferred way to perform simple background operations.

然而,IntentService大部分情况下还是很有用

This lesson shows you how to create your own subclass of IntentService. The lesson also shows you how to create the required callback method onHandleIntent(). Finally, the lesson describes shows you how to define the IntentService in your manifest file.

这堂课想你展示如何create 你自己的IntentService. 这堂课也展示如何创建onHandleIntent。

最终,这堂课展示如何在manifest中定义IntentService

 

Handle incoming intents

处理到来的intents

To create an IntentService component for your app, define a class that extends IntentService, and within it, define a method that overrides onHandleIntent(). For example:

翻译:定义一个类继承IntentService ,定义一个方法override onHandleIntent ,举例:

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

 

Notice that the other callbacks of a regular Service component, such as onStartCommand() are automatically invoked by IntentService. In an IntentService, you should avoid overriding these callbacks.

To learn more about creating an IntentService, see Extending the IntentService class.

翻译:像startCommand方法什么的,都不用管了

参考:Extending the IntentService class.

 

Define the intent service in the manifest

清单文件定义service

An IntentService also needs an entry in your application manifest. Provide this entry as a <service> element that's a child of the <application> element:

翻译:要在清单文件定义

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

The attribute android:name specifies the class name of the IntentService.

翻译:name确定IntentService的class名字

 

Notice that the <service> element doesn't contain an intent filter. The Activity that sends work requests to the service uses an explicit Intent, so no filter is needed. This also means that only components in the same app or other applications with the same user ID can access the service.

翻译:注意到没有intent filter ,也就是说都是显示Intent来调用。 

 

Now that you have the basic IntentService class, you can send work requests to it with Intent objects. The procedure for constructing these objects and sending them to your IntentService is described in the next lesson.

翻译:有了IntentService class, 你可以使用Intent objects  来send work request 给他 让他干活了。

下文详叙。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值