当前位置: 首页 > 编程笔记 >

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

都昊乾
2023-03-14
本文向大家介绍Android App后台服务报告工作状态实例,包括了Android App后台服务报告工作状态实例的使用技巧和注意事项,需要的朋友参考一下

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

 类似资料:
  • 7.1 ABP后台服务 - 后台作业和后台工人 7.1.1 简介 ABP提供了后台作业和后台工人,来执行应用程序中的后台线程的某些任务。 7.1.2 后台作业 由于各种各样的原因,你需要后台作业以队列和持久化的方式来排队执行某些任务。 例如: 用户等待执行一个长时任务。例如:某个用户按下了报表按钮生成一个需要长时间等待的报表。你添加这个工作到队列中,当报表生成完毕后,发送报表结果到该用户的邮箱。

  • 问题内容: 我试图理解这一点。通常,每次用户登录系统时,服务器端都会创建一个会话,而用户客户端端则有cookie。当人们谈论无状态服务器端,有状态客户端时,它们是什么意思?服务器端无需使用会话保持跟踪用户?只在客户端使用Cookie来检查用户?意味着如果我更换服务器,用户将不会注意到它,仍然可以继续使用该服务? 如何配置spring-security来做到这一点? 问题答案: 对于真正的无状态服务

  • 我想创建一个小应用程序,在后台记录数据。所以我试着用绑定服务。这很好,但如果我关闭应用程序,服务也会停止。< br >那么,我的问题是:使用即时服务来执行这一操作是不是一个好方法?当应用程序关闭时,我如何保持服务在后台运行(我也想在启动后启动它)?

  • 有人能发布一个如何使用web服务作为数据源定义子报表的工作示例吗? 我正在阅读以下内容: http://community . jasper soft . com/project/we B- service-data-source 在“子报告和其他数据集”一节中,我需要考虑(2)的情况: 您有一个希望循环的主结果集,并且希望对每一行执行一个新的查询(webservice调用) 在子报表配置(或数据

  • 理解Gradle profile报告时遇到了困难,这些报告的执行任务结果是“没有工作”。 Gradle文档只是声明,结果将反映为没有跳过但没有工作的任务。这是否意味着构建任务不起作用,只是调用和/或依赖Java插件中的其他子任务?

  • 示例: state state 是另外一个元数据。 如果你在元数据中设置了state=inactive, 客户端将不能访问这些服务,即使这些服务是"活"着的。 你可以使用临时禁用一些服务,而不是杀掉它们, 这样就实现了服务的降级。 server. 你可以通过 rpcx-ui)来时实现禁用和启用的功能。 ```go server.go func main() { flag.Parse() go cr