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来响应广播。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值