要知道区别首先需要看一下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则会抛出异常。
这个解释看了后似懂非懂的,于是就写了个例子试了下,下面把代码贴出了,希望能还大家一起讨论
- 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无输出,这是当然的了~~~因为没有广播发出自然就不会有人响应了。
按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方法可以帮你,处理完了后就将它删除吧。
本文深入解析Android开发中sendStickyBroadcast的功能和使用方法,通过实例演示了如何利用这一特性实现广播消息的持久化,以及在不同场景下的应用。详细介绍了sendStickyBroadcast与普通广播的区别,包括权限需求、持久化机制以及如何清除持久化广播。
1591

被折叠的 条评论
为什么被折叠?



