• Creating a Background Service ——IntentService


    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, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask
    //intentservice 提供了一个在后台线程运行操作的简洁的结构,他允许在不影响用户界面响应的情况下处理长时间运行的操作。intentservice不会受界面生命周期的影响,所以同样的情况asynctask可能会被结束但是intentservice还可以继续存活

    An IntentService has a few limitations:
    // intent service有一些限制:
    It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
    // 不能和界面交互,为了让结果更新到界面上必须将结果发送给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.
    // 任务请求是顺序执行的。如果一个操作此刻正在intentservice中执行,然后你发送另一个请求,这个请求会等待,知道第一个请求任务结束
    An operation running on an IntentService can't be interrupted.
    // 运行中的操作不能被中断
    However, in most cases an IntentService is the preferred way to perform simple background operations.

    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 an IntentService
    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:

    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.
    //注意 其他的对于通常使用的service组件的其他回调方法,例如onStartCommand会自动被intentservice调用,所以在intentservice中,你应该避免重写这些回调

    Define the IntentService in the Manifest
    An IntentService also needs an entry in your application manifest. Provide this entry as a element that's a child of the 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.

    Notice that the 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.

    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.

    ----------- Do not start just casually, and do not end just casually. -----------
  • 相关阅读:
    VB运算符总结
    动态实现树形菜单
    使用C#开发ActiveX控件
    jquery+ajax加载xml文件到页面
    C#序列化与反序列化
    jquery之ajax——全局事件引用方式以及各个事件(全局/局部)执行顺序
    SerialPort实现对串口COM的操作(有些纠结)
    jquery+ajax+C#实现无刷新操作数据库数据
    超过一百多个Web2.0在线生成器
    asp.net URL Rewriter 问题
  • 原文地址:https://www.cnblogs.com/yexiant/p/5627899.html
Copyright © 2020-2023  润新知