sendStickyBroadcast和sendStickyOrderedBroadcast - 牛仔的移动开发博客 - 博客频道 - CSDN.NET
sendStickyBroadcast和sendStickyOrderedBroadcast发出的广播会一直滞留(等待),以便有人
注册这则广播消息后能尽快的收到这条广播。其他功能与sendBroadcast相同。但是使用sendStickyBroadcast 发送广播需要获得
BROADCAST_STICKY
permission,如果没有这个permission则会抛出异常。例子:
- package com.android.test;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class StickyBroadcastTest extends Activity {
- private Button mSendBroadcast;
- private Button mSendStickyBroadcast;
- private Button mNextActivity;
- private Context mContext;
- private int mStickyBrcCount;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mContext = getApplicationContext();
- mSendBroadcast = (Button)findViewById(R.id.broadcast);
- mSendStickyBroadcast = (Button)findViewById(R.id.stickybroadcast);
- mNextActivity = (Button)findViewById(R.id.next_activity);
- mSendBroadcast.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent("com.android.action.broadcast");
- mContext.sendBroadcast(intent);
- }
- });
- mSendStickyBroadcast.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- mStickyBrcCount++;
- Intent intent = new Intent("com.android.action.sticky.broadcast");
- intent.putExtra("sent_count", mStickyBrcCount);
- mContext.sendStickyBroadcast(intent);
- }
- });
- mNextActivity.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(StickyBroadcastTest.this, MyReceiverActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- mStickyBrcCount = 0;
- }
- }
- //MyReceiverActivity
- package com.android.test;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.util.Log;
- public class MyReceiverActivity extends Activity {
- private IntentFilter mIntentFilter;
- private final static String TAG = "MyReceiverActivity";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.broadcast_receiver);
- mIntentFilter = new IntentFilter();
- mIntentFilter.addAction("com.android.action.broadcast");
- mIntentFilter.addAction("com.android.action.sticky.broadcast");
- }
- private BroadcastReceiver mReceiver = new BroadcastReceiver () {
- @Override
- public void onReceive(Context context, Intent intent) {
- final String action = intent.getAction();
- int count = intent.getIntExtra("sent_count", -1);
- Log.d(TAG, "action = " + action + "and count = " + count);
- //context.removeStickyBroadcast(intent);
- }
- };
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- unregisterReceiver(mReceiver);
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- registerReceiver(mReceiver, mIntentFilter);
- }
- }
运行结果如图:
首先点击next Activity从代码中可以看到receiver已经注册,但Log无输出,这是当然的了~~~因为没有广播发出自然就不会有人响应了。(onPause里unregisterReceiver了)
按back后退到上图
下面分别点击send broadcast 和 send stickybroadcast按钮,随便点击几次,此时对应的receiver并没有注册,所以是不会有人响应这两条广播的。然后点击next activity,当打开新的activity后对应的receiver被注册,此时从日志中就能看出已经收到了send stickybroadcast发出的广播,但没有send broadcast发出的广播。这就是sendStickyBroadcast的特别之处,它将发出的广播保存起来,一旦发现有人注册这条广播,则立即能接收到。
日志打印为: action = com.android.action.sticky.broadcastand count = 4
从上面的日志信息可以看出sendStickyBroadcast只保留最后一条广播,并且一直保留下去,这样即使已经处理了这条广播但当再一次注册这条广播后依然可以收到它。
如果你只想处理一遍,removeStickyBroadcast方法可以帮你,处理完了后就将它删除吧。
sticky形式的intent,接收者可以为空,接收者为空时,通常是获取最后一个保存广播的intent,从而获取intent里的值,不如获取电池的电量:
因为BatteryManager发送的是sticky形式的intent,所以接收者可以为空
Intent batteryStatus = registerReceiver(null, ifilter);//得到电池当前的状态(共有5种,包括unkonwn、charging、discharging、not charging、full)
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
//是否处于充电状态
boolean isCharging = BatteryManager.BATTERY_STATUS_CHARGING ;