sendStickyBroadcast 的理解和使用

要知道区别首先需要看一下Android Developers Reference, 它可是我们最好的老师了,sendBroadcast 大家应该都会用了我就不赘述了,下面来看看sendStickyBroadcast

google官方的解释是:

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 ofregisterReceiver(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.

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

 

这个解释看了后似懂非懂的,于是就写了个例子试了下,下面把代码贴出了,希望能还大家一起讨论

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

运行结果如图:

首先点击next Activity从代码中可以看到receiver已经注册,但Log无输出,这是当然的了~~~因为没有广播发出自然就不会有人响应了。

按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方法可以帮你,处理完了后就将它删除吧。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值