`
寻梦者
  • 浏览: 623417 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

sendBroadcast和sendStickyBroadcast的区别

阅读更多

我们平时最经常使用的是sendBroadcast,就是把一个Intent广播出去。今天我在看wifi的时候,还发现了sendStickyBroadcast。官方文档是这样写的: 


public abstract void sendStickyBroadcast (Intent intent) 

Since: API Level 1 
Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). 
You must hold the BROADCAST_STICKY permission in order to use this API. If you do not hold that permission, SecurityException will be thrown. 
Parameters 

intent The Intent to broadcast; all receivers matching this Intent will receive the broadcast, and the Intent will be held to be re-broadcast to future receivers. 

光从字面的意思是很难理解的。只有你写例子才会明白的。
Java代码

  1. package com.android.testbroadcast;

  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 MainActivity extends Activity {
  10.         Button btnSendi;
  11.         Button btnSends;
  12.         Button btnStart;
  13.         Context mContext;
  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         btnSendi=(Button) findViewById(R.id.sendi);
  20.         btnSends=(Button) findViewById(R.id.sends);
  21.         btnStart=(Button) findViewById(R.id.start);
  22.         mContext=getApplicationContext();
  23.         btnSendi.setOnClickListener(new OnClickListener(){

  24.                         @Override
  25.                         public void onClick(View v) {
  26.                                 // TODO Auto-generated method stub
  27.                                 Intent intent = new Intent();
  28.                             intent.setAction("com.android.my.action");
  29.                             intent.setFlags(1);
  30.                             mContext.sendBroadcast(intent);
  31.                         }
  32.                 
  33.         });
  34.         
  35.         btnStart.setOnClickListener(new OnClickListener(){

  36.                         @Override
  37.                         public void onClick(View v) {
  38.                                 // TODO Auto-generated method stub
  39.                                 Intent intent = new Intent(MainActivity.this,ReceiverActivity.class);
  40.                            
  41.                             startActivity(intent);
  42.                         }
  43.                 
  44.         });
  45.         
  46.         btnSends.setOnClickListener(new OnClickListener(){

  47.                         @Override
  48.                         public void onClick(View v) {
  49.                                 // TODO Auto-generated method stub
  50.                                 Intent intent = new Intent();
  51.                             intent.setAction("com.android.my.action.sticky");
  52.                             intent.setFlags(2);
  53.                             mContext.sendStickyBroadcast(intent);
  54.                         }
  55.                 
  56.         });
  57.     }
  58. }
复制代码





Java代码
  1. package com.android.testbroadcast;

  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;

  12. public class ReceiverActivity extends Activity {
  13.          private IntentFilter mIntentFilter;
  14.         
  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.         mIntentFilter = new IntentFilter();
  21.         mIntentFilter.addAction("com.android.my.action");
  22.         mIntentFilter.addAction("com.android.my.action.sticky");

  23.                         
  24.     }
  25.     private BroadcastReceiver mReceiver = new BroadcastReceiver() {

  26.         @Override
  27.         public void onReceive(Context context, Intent intent) {
  28.             final String action = intent.getAction();
  29.             System.out.println("action"+action);
  30.             
  31.         }
  32.     };
  33.     
  34.     @Override
  35.     protected void onResume() {
  36.             // TODO Auto-generated method stub
  37.             super.onResume();
  38.             registerReceiver(mReceiver, mIntentFilter);
  39.     }
  40.     
  41.     @Override
  42.     protected void onPause() {
  43.             // TODO Auto-generated method stub
  44.             super.onPause();
  45.             unregisterReceiver(mReceiver);
  46.     }
  47.     
  48.     
  49. }
复制代码
在MainActivity里面会有sendBroadcast和sendStickyBroacat.在ReceverActivity里面通过BroadcastReceiver来接收这两个消息,在ReceiverActivity里是通过代码来注册Recevier而不是在Manifest里面注册的。所以通过sendBroadcast中发出的intent在ReceverActivity不处于onResume状态是无法接受到的,即使后面再次使其处于该状态也无法接受到。而sendStickyBroadcast发出的Intent当ReceverActivity重新处于onResume状态之后就能重新接受到其Intent.这就是the Intent will be held to be re-broadcast to future receivers这句话的表现。就是说sendStickyBroadcast发出的最后一个Intent会被保留,下次当Recevier处于活跃的时候,又会接受到它。
分享到:
评论

相关推荐

    最经典的sendBroadcast与sendStickyBroadcast的区别分析与讲解

    最经典的关于sendBroadcast与sendStickyBroadcast的区别分析与讲解

    android broadcast实例

    android broadcast实例,包括sendBroadcast、 sendOrderedBroadcast、 sendStickyBroadcast三种实现方法

    Android应用程序发送广播(sendBroadcast)的过程分析.doc

    Android应用程序发送广播(sendBroadcast)的过程分析

    Android Intent和Intent Filter详解

     将intent对象传给 Context.sendBroadcast(), Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等广播方法,则它们被传给 broadcast receiver.  在上述三种情况下, android系统会自己找到合适的...

    Android开发指南

    • 传递给任意广播方法(例如Context.sendBroadcast(),Context.sendOrderedBroadcast(), 或者Context.sendStickyBroadcast())的意图对象被传递给所有感兴趣的广播接收者。许多种广播产生于系统代码。 在每个例子里...

    Android 静默安装和静默卸载(SilentInstaller)

    详情见:Android 静默安装和静默卸载(系统层) http://blog.csdn.net/zhouyuanjing/article/details/78507606 安装: Intent intent = new Intent("android.intent.action.SILENT_PACKAGE_INSTALL"); intent....

    Arduino-meshquitto.zip

    Arduino-meshquitto.zip,一个简单的Arduino项目,旨在在ESP8266的网状网络和远程MQTT代理之间提供网关。,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的欢迎,Arduino通过开源...

    android中广播接收

    android中广播接收者,有序广播和无序广播 sendOrderBroadcast sendBroadcast

    实现用户自定义程序向其他应用程序发送广播

    实现用户自定义程序向其他应用程序发送广播,通过sendBroadcast方法实现

    Android实验六.doc

    【实验要求】 1、 练习使用静态方法和动态方法注册广播接收器 2、 练习发送广播消息的方法 3、 完成实验报告 二、实验内容 1、 新建 Android 应用程序项目 BroadcastTest; 2、 业务逻辑代码与界面布局文件如下: 1)...

    usb-permission-issuer.apk

    sendBroadcast(intent); 或者 Intent intent = new Intent(); intent.setAction("ACTION_USB_PERMISSION_ISSUER"); intent.putExtra("packageName", "你的app-package"); intent.putExtra("vendorId", 1569);/...

    com.lampa.startapp:Phonegap插件,用于检查或启动Android设备中的其他应用程序

    最新版本6.1.6 添加支持Java 1.6 添加支持Java 1.7 添加完整的支持activityForResult,sendBroadcast和RegisterReceiver。 添加额外类型。安装安装: cordova plugin add com.lampa.startapp 安装: cordova plugin...

    Android中BroadcastReceiver(异步接收广播Intent)的使用

    用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于...sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的...

    Android入门:广播发送者与广播接收者详细介绍

    广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action=”com.xiazdong

    Android Intent传递数据大小限制详解

    在sendBroadcast,startActivity时,我们会用到Intent。 Intent可以携带一些数据,比如基本类型数据int、Boolean,或是String,或是序列化对象,Parcelable与Serializable。 Intent传递数据时,如果数据太大,可能...

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

    本节讲运行在后台服务里的工作请求,如何向...下一步,调用 LocalBroadcastManager.sendBroadcast()发送Intent,应用中所有注册了接收该广播的接收器都能收到。LocalBroadcastManager.getInstance()获取LocalBroadcastM

    babylon:使用Retrofit2.0,Butterknife,OrmLite和Picasso构建的示例应用程序

    通过Retrofit回调(将从端点提供数据),将包含联系人头像的sendbroadcast发送给Activity,后者将处理该意图。 游标加载程序将管理列表中名字和姓氏的显示,并且头像将填充图像视图(在这种情况下,我们不将图像...

    Android 创建快捷方式图标.rar

    Android 创建快捷方式图标,这个挺简单,有兴趣的Android初学者可参考研究. ... sendBroadcast(addIntent);//发送广播  }  else if(v == myButton2){//按下了退出按钮   System.exit(0);//退出系统  }

Global site tag (gtag.js) - Google Analytics