zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

实例后台状态服务 工作 报告 AndroidApp
2023-06-13 09:15:32 时间

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

从IntentService汇报状态

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

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

复制代码代码如下:


publicfinalclassConstants{
   ...
   //DefinesacustomIntentaction
   publicstaticfinalStringBROADCAST_ACTION=
       "com.example.android.threadsample.BROADCAST";
   ...
   //Definesthekeyforthestatus"extra"inanIntent
   publicstaticfinalStringEXTENDED_DATA_STATUS=
       "com.example.android.threadsample.STATUS";
   ...
}
publicclassRSSPullServiceextendsIntentService{
...
   /*
    *CreatesanewIntentcontainingaUriobject
    *BROADCAST_ACTIONisacustomIntentaction
    */
   IntentlocalIntent=
           newIntent(Constants.BROADCAST_ACTION)
           //PutsthestatusintotheIntent
           .putExtra(Constants.EXTENDED_DATA_STATUS,status);
   //BroadcaststheIntenttoreceiversinthisapp.
   LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
...
}

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

接收来自IntentService的广播

接收方式与普通的Broadcast一样,用一个BroadcastReceiver的子类,实现BroadcastReceiver.onReceive()方法

复制代码代码如下:

//BroadcastreceiverforreceivingstatusupdatesfromtheIntentService
privateclassResponseReceiverextendsBroadcastReceiver
{
   //Preventsinstantiation
   privateDownloadStateReceiver(){
   }
   //CalledwhentheBroadcastReceivergetsanIntentit"sregisteredtoreceive
   @
   publicvoidonReceive(Contextcontext,Intentintent){
...
       /*
        *HandleIntentshere.
        */
...
   }
}

定义好了接收器类以后,定义过滤器,匹配指定的action,categorie,data.
复制代码代码如下:
//Classthatdisplaysphotos
publicclassDisplayActivityextendsFragmentActivity{
   ...
   publicvoidonCreate(BundlestateBundle){
       ...
       super.onCreate(stateBundle);
       ...
       //Thefilter"sactionisBROADCAST_ACTION
       IntentFiltermStatusIntentFilter=newIntentFilter(
               Constants.BROADCAST_ACTION);
 
       //AddsadatafilterfortheHTTPscheme
       mStatusIntentFilter.addDataScheme("http");
       ...

注册方式稍有不同,用LocalBroadcastManager.registerReceiver()。
复制代码代码如下:
 //InstantiatesanewDownloadStateReceiver
       DownloadStateReceivermDownloadStateReceiver=
               newDownloadStateReceiver();
       //RegisterstheDownloadStateReceiveranditsintentfilters
       LocalBroadcastManager.getInstance(this).registerReceiver(
               mDownloadStateReceiver,
               mStatusIntentFilter);
       ...
单个BroadcastReceiver可以处理多种类型的广播,这个特性允许你根据不同的action运行不同的代码,而无需为每个action定义一个BroadcastReceiver。
复制代码代码如下:
 /*
        *Instantiatesanewactionfilter.
        *Nodatafilterisneeded.
        */
       statusIntentFilter=newIntentFilter(Constants.ACTION_ZOOM_IMAGE);
       ...
       //Registersthereceiverwiththenewfilter
       LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
               mDownloadStateReceiver,
               mIntentFilter);
发送广播并不会启动或恢复Activity.BroadcastReceiver让Activity能够接收处理数据,包括应用在后台的时候,但不会强制app回到前台。如果你要在app在后台,对用户不可见时,通知用户一个事件发生,用Notification。绝对不要启动一个Activity来响应广播。