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

作者:junjie 时间:2023-04-26 19:10:34 

本节讲运行在后台服务里的工作请求,如何向发送请求者报告状态。推荐用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来响应广播。

标签:Android,App,工作状态
0
投稿

猜你喜欢

  • SpringBoot2.3新特性优雅停机详解

    2023-11-28 07:59:43
  • Android防止点击过快造成多次响应事件的解决方法

    2023-05-24 16:26:09
  • 图文详解Java中的序列化机制

    2021-06-15 12:11:49
  • flutter BottomAppBar实现不规则底部导航栏

    2023-06-19 23:40:19
  • IDEA类存在但找不到的解决办法

    2021-10-22 07:24:43
  • C#多态的三种实现方式(小结)

    2022-12-29 18:10:57
  • Android使用自定义View实现360手机卫士波浪球进度的效果

    2023-03-21 19:18:34
  • 深入讲解java线程与synchronized关键字

    2023-08-29 13:56:00
  • C#以太网Sockets服务器设计实现

    2023-10-10 04:38:32
  • 详解Java volatile 内存屏障底层原理语义

    2023-05-08 19:25:47
  • C# 从枚举值获取对应的文本描述详解

    2021-06-14 00:47:58
  • springboot实现多模块项目添加一新模块

    2021-09-22 16:43:09
  • 详解idea maven nexus 常见命令配置

    2021-06-07 18:29:03
  • SpringBoot集成支付宝沙箱支付的实现示例

    2023-10-31 19:22:20
  • 解决JAVA非对称加密不同系统加密结果不一致的问题

    2022-02-13 06:43:56
  • spring boot使用sharding jdbc的配置方式

    2022-02-16 00:29:15
  • java实现滑动验证解锁

    2023-06-02 12:16:36
  • Android Touch事件分发过程详解

    2021-08-28 20:11:33
  • C# FileStream实现多线程断点续传

    2022-06-19 06:50:56
  • java制作复制文件工具代码分享

    2022-08-05 05:30:22
  • asp之家 软件编程 m.aspxhome.com