sendStickyBroadcast和sendStickyOrderedBroadcast发出的广播会一直滞留(等待),以便有人注册这则广播消息后能尽快的收到这条广播。其他功能与sendBroadcast相同。但是使用sendStickyBroadcast  发送广播需要获得BROADCAST_STICKY permission,如果没有这个permission则会抛出异常。

例子:

  1. package com.android.test;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. public class StickyBroadcastTest extends Activity {
  10. private Button mSendBroadcast;
  11. private Button mSendStickyBroadcast;
  12. private Button mNextActivity;
  13. private Context mContext;
  14. private int mStickyBrcCount;
  15. /** Called when the activity is first created. */
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. mContext = getApplicationContext();
  21. mSendBroadcast = (Button)findViewById(R.id.broadcast);
  22. mSendStickyBroadcast = (Button)findViewById(R.id.stickybroadcast);
  23. mNextActivity = (Button)findViewById(R.id.next_activity);
  24. mSendBroadcast.setOnClickListener(new OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. Intent intent = new Intent("com.android.action.broadcast");
  28. mContext.sendBroadcast(intent);
  29. }
  30. });
  31. mSendStickyBroadcast.setOnClickListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. mStickyBrcCount++;
  35. Intent intent = new Intent("com.android.action.sticky.broadcast");
  36. intent.putExtra("sent_count", mStickyBrcCount);
  37. mContext.sendStickyBroadcast(intent);
  38. }
  39. });
  40. mNextActivity.setOnClickListener(new OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. Intent intent = new Intent(StickyBroadcastTest.this, MyReceiverActivity.class);
  44. startActivity(intent);
  45. }
  46. });
  47. }
  48. @Override
  49. protected void onResume() {
  50. // TODO Auto-generated method stub
  51. super.onResume();
  52. mStickyBrcCount = 0;
  53. }
  54. }
  55. //MyReceiverActivity
  56. package com.android.test;
  57. import android.app.Activity;
  58. import android.content.BroadcastReceiver;
  59. import android.content.Context;
  60. import android.content.Intent;
  61. import android.content.IntentFilter;
  62. import android.os.Bundle;
  63. import android.util.Log;
  64. public class MyReceiverActivity extends Activity {
  65. private IntentFilter mIntentFilter;
  66. private final static String TAG = "MyReceiverActivity";
  67. /** Called when the activity is first created. */
  68. @Override
  69. public void onCreate(Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71. setContentView(R.layout.broadcast_receiver);
  72. mIntentFilter = new IntentFilter();
  73. mIntentFilter.addAction("com.android.action.broadcast");
  74. mIntentFilter.addAction("com.android.action.sticky.broadcast");
  75. }
  76. private BroadcastReceiver  mReceiver = new BroadcastReceiver () {
  77. @Override
  78. public void onReceive(Context context, Intent intent) {
  79. final String action = intent.getAction();
  80. int count = intent.getIntExtra("sent_count", -1);
  81. Log.d(TAG, "action = " + action + "and count = " + count);
  82. //context.removeStickyBroadcast(intent);
  83. }
  84. };
  85. @Override
  86. protected void onPause() {
  87. // TODO Auto-generated method stub
  88. super.onPause();
  89. unregisterReceiver(mReceiver);
  90. }
  91. @Override
  92. protected void onResume() {
  93. // TODO Auto-generated method stub
  94. super.onResume();
  95. registerReceiver(mReceiver, mIntentFilter);
  96. }
  97. }

运行结果如图:

首先点击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 ;

sendStickyBroadcast和sendStickyOrderedBroadcast的更多相关文章

  1. 1.2 Broadcast

    在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件. 一个Broad ...

  2. BroadcastReceiver介绍

    参考资料 : 基础总结篇之五:BroadcastReceiver应用详解 BroadcastReceiver用于接收广播信息,可以通过sendBroadcast等方法进行发送.sendBroadcas ...

  3. Android四大基本组件介绍与生命周期

    Android四大基本组件介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器 ...

  4. Android - 广播接收者 - BroadcastReceiver

    BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...

  5. 17.(转) Android之四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...

  6. BroadCast Receive 生命周期

    BroadCastReceiver 简介 BroadCastReceiver 源码位于: framework/base/core/java/android.content.BroadcastRecei ...

  7. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  8. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  9. Android安全问题 抢先接收广播 - 内因篇之广播发送流程

    导读:本文说明系统发送广播的部分流程,如何利用Intent查找到对应接收器.我们依然只关注接收器的排序问题 这篇文章主要是针对我前两篇文章 android安全问题(四) 抢先开机启动 - 结果篇 an ...

随机推荐

  1. Newtonsoft.Json.JsonWriter

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4754769.html]  JsonWriter使用: 前台 $.post("Ajax ...

  2. Android 常用UI控件之TabHost(4)实现当Tab栏有多个tab时,可以左右滑动

    <!-- <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_wi ...

  3. 【POJ】1692 Crossed Matchings

    经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...

  4. 下拉列表联动显示(Car表) 三级联动

    .Models namespace 下拉列表联动显示_Car表_.Models { public class ProductorBF { private MyDBDataContext _contex ...

  5. 【JavaScript 开发规范】

    Javascript 最佳实践http://sofish.de/1171http://sofish.de/1181 总是使用 ‘var’ √ 特性检测而非浏览器检测 √ 使用方括号记法 √ 使用&qu ...

  6. 【转】 Xcode基本操作 -- 不错

    原文网址:http://blog.csdn.net/phunxm/article/details/17044337 1.Xcode IDE概览 说明:从左到右,依次是“导航窗格(Navigator)- ...

  7. JAVA与.NET的相互调用——通过Web服务实现相互调用

    JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...

  8. POJ -- 3233 求“等比矩阵”前n(n <=10^9)项和

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S =  ...

  9. bootstrap -- 一个标签中,同时有 col-xs , col-sm , col-md , col-lg

    .col-xs- 超小屏幕 手机 (<768px) .col-sm- 小屏幕 平板 (≥768px) .col-md- 中等屏幕 桌面显示器 (≥992px) .col-lg- 大屏幕 大桌面显 ...

  10. aggregate 和 treeAggregate 的对比

    1.定义 [aggregate] /** * Aggregate the elements of each partition, and then the results for all the pa ...