欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android App后台服务报告工作状态实例

程序员文章站 2023-02-02 12:50:04
本节讲运行在后台服务里的工作请求,如何向发送请求者报告状态。推荐用localbroadcastmanager发送和接收状态,它限制了只有本app才能接收到广播。 从int...

本节讲运行在后台服务里的工作请求,如何向发送请求者报告状态。推荐用localbroadcastmanager发送和接收状态,它限制了只有本app才能接收到广播。

从intentservice汇报状态

从intentservice发送工作请求状态给其他组件,先创建一个包含状态和数据的intent。也可以添加action和uri到intent里。

下一步,调用 localbroadcastmanager.sendbroadcast()发送intent,应用中所有注册了接收该广播的接收器都能收到。localbroadcastmanager.getinstance()获取localbroadcastmanager实例。

复制代码 代码如下:

public final class constants {
    ...
    // defines a custom intent action
    public static final string broadcast_action =
        "com.example.android.threadsample.broadcast";
    ...
    // defines the key for the status "extra" in an intent
    public static final string extended_data_status =
        "com.example.android.threadsample.status";
    ...
}
public class rsspullservice extends intentservice {
...
    /*
     * creates a new intent containing a uri object
     * broadcast_action is a custom intent action
     */
    intent localintent =
            new intent(constants.broadcast_action)
            // puts the status into the intent
            .putextra(constants.extended_data_status, status);
    // broadcasts the intent to receivers in this app.
    localbroadcastmanager.getinstance(this).sendbroadcast(localintent);
...
}

下一步是接收广播并处理。

接收来自intentservice的广播

接收方式与普通的broadcast一样,用一个broadcastreceiver的子类,实现broadcastreceiver.onreceive()方法

复制代码 代码如下:

// broadcast receiver for receiving status updates from the intentservice
private class responsereceiver extends broadcastreceiver
{
    // prevents instantiation
    private downloadstatereceiver() {
    }
    // called when the broadcastreceiver gets an intent it's registered to receive
    @
    public void onreceive(context context, intent intent) {
...
        /*
         * handle intents here.
         */
...
    }
}

定义好了接收器类以后,定义过滤器,匹配指定的action,categorie,data.
复制代码 代码如下:

// class that displays photos
public class displayactivity extends fragmentactivity {
    ...
    public void oncreate(bundle statebundle) {
        ...
        super.oncreate(statebundle);
        ...
        // the filter's action is broadcast_action
        intentfilter mstatusintentfilter = new intentfilter(
                constants.broadcast_action);
 
        // adds a data filter for the http scheme
        mstatusintentfilter.adddatascheme("http");
        ...

注册方式稍有不同,用localbroadcastmanager.registerreceiver()。
复制代码 代码如下:

  // instantiates a new downloadstatereceiver
        downloadstatereceiver mdownloadstatereceiver =
                new downloadstatereceiver();
        // registers the downloadstatereceiver and its intent filters
        localbroadcastmanager.getinstance(this).registerreceiver(
                mdownloadstatereceiver,
                mstatusintentfilter);
        ...

单个broadcastreceiver可以处理多种类型的广播,这个特性允许你根据不同的action运行不同的代码,而无需为每个action定义一个broadcastreceiver。
复制代码 代码如下:

  /*
         * instantiates a new action filter.
         * no data filter is needed.
         */
        statusintentfilter = new intentfilter(constants.action_zoom_image);
        ...
        // registers the receiver with the new filter
        localbroadcastmanager.getinstance(getactivity()).registerreceiver(
                mdownloadstatereceiver,
                mintentfilter);

发送广播并不会启动或恢复activity.broadcastreceiver让activity能够接收处理数据,包括应用在后台的时候,但不会强制app回到前台。如果你要在app在后台,对用户不可见时,通知用户一个事件发生,用notification。绝对不要启动一个activity来响应广播。