sendStickyBroadcast 的理解和使用
来源:互联网 发布:什么数据比金十数据好 编辑:程序博客网 时间:2024/05/18 01:15
要知道区别首先需要看一下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 assendBroadcast(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方法可以帮你,处理完了后就将它删除吧。
如果有什么不对的地方欢迎大家指教+拍砖。
- sendStickyBroadcast 的理解和使用
- sendStickyBroadcast 的理解和使用
- sendStickyBroadcast 的理解和使用
- sendStickyBroadcast 的理解和使用
- sendStickyBroadcast 的理解和使用
- sendStickyBroadcast 的理解和使用
- sendBroadcast和sendStickyBroadcast的区别
- sendBroadcast和sendStickyBroadcast的区别
- sendBroadcast和sendStickyBroadcast的区别
- sendBroadcast和sendStickyBroadcast的区别
- sendBroadcast和sendStickyBroadcast的区别
- sendBroadcast和sendStickyBroadcast的区别
- sendStickyBroadcast和sendStickyOrderedBroadcast
- Android学习 - sendBroadcast和sendStickyBroadcast
- sendBroadcast与sendStickyBroadcast的区别
- sendBroadcast与sendStickyBroadcast的区别
- 委托的理解和使用
- ThreadLocal的理解和使用
- Linux系统磁盘分区管理命令
- Android: R cannot be resolved to a variable
- 分享11款主流的开源编程工具
- Oracle SQL优化 总结
- flex Module学习(2)
- sendStickyBroadcast 的理解和使用
- linux下的音频采集
- 如此如此,怎能师夷长技以制夷!
- 创建android逐帧动画的两种方式
- 巧设BIOS,让老主板也支持U盘启动!
- Delphi三层主从表设置
- Win2003下移动硬盘无法识别
- 数据库访问的一些问题
- ASP.Net中无刷新分页的实现