- 浏览: 1068270 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
我们平时最经常使用的是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.
光从字面的意思是很难理解的。只有你写例子才会明白的。
在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处于活跃的时候,又会接受到它。
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.
光从字面的意思是很难理解的。只有你写例子才会明白的。
package com.android.testbroadcast; 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 MainActivity extends Activity { Button btnSendi; Button btnSends; Button btnStart; Context mContext; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSendi=(Button) findViewById(R.id.sendi); btnSends=(Button) findViewById(R.id.sends); btnStart=(Button) findViewById(R.id.start); mContext=getApplicationContext(); btnSendi.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.android.my.action"); intent.setFlags(1); mContext.sendBroadcast(intent); } }); btnStart.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,ReceiverActivity.class); startActivity(intent); } }); btnSends.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.android.my.action.sticky"); intent.setFlags(2); mContext.sendStickyBroadcast(intent); } }); } }
package com.android.testbroadcast; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ReceiverActivity extends Activity { private IntentFilter mIntentFilter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mIntentFilter = new IntentFilter(); mIntentFilter.addAction("com.android.my.action"); mIntentFilter.addAction("com.android.my.action.sticky"); } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); System.out.println("action"+action); } }; @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); registerReceiver(mReceiver, mIntentFilter); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); unregisterReceiver(mReceiver); } }
在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处于活跃的时候,又会接受到它。
- TestBroadcast.rar (45.4 KB)
- 下载次数: 81
发表评论
-
ACTIVITY的LAUNCH MODE详解 SINGLETASK正解
2012-05-30 08:58 1104转自:http://www.cnblogs.com/xiaoQ ... -
android的一些开源项目
2011-12-06 14:08 1187转自:http://www.iteye.com/problem ... -
修改StatusBar
2011-10-10 10:27 1252转自:http://iserveandroid.blogspo ... -
单独一个应用程序换语言
2011-09-29 15:16 1443转自http://blog.csdn.net/sodino/a ... -
通过代码设置live wall paper
2011-09-02 09:17 25141. The code is: private Wall ... -
Android camera 默认显示黑白的问题
2011-06-08 16:18 2541转自:http://tassardge.blog. ... -
关于Activity切换动画——overridePendingTransition
2011-05-25 14:33 5766Activity的切换动画指的是从一个activity跳转到另 ... -
Settings.System.getInt获取Setting里面的一些设置
2011-04-28 10:29 7031好久没有更新博客了,其实这期间我都在研究android如何换皮 ... -
Service 启动Activity
2011-03-11 11:06 25176我想我们一般在Service里想启动Activity一定会这样 ... -
Android Windows
2011-03-11 09:45 2051转自:http://elsila.blog.163.com/b ... -
IntentService
2011-03-11 09:23 3444看android的源码可以发现很多很多有趣有用的代码,今天在看 ... -
AsyncQueryHandler
2011-03-02 15:10 6796在QuickContactBadge里面我 ... -
QuickContactBadge如何实现
2011-03-02 11:52 4915从前一篇,我们知道了如何使用了QuikcContactBadg ... -
如何使用QuickContactBadge
2011-03-02 09:34 4064参考文章:http://mobile.tutsplus.com ... -
QuickSearcheBox---SearchManager获取search列表
2011-02-25 14:05 1855在android 2.2之后加入了SearchManager, ... -
QuickSearcheBox---SearchWidgetConfigActivity
2011-02-24 10:48 1642再把QuickSearchBox放到桌面前,会先触发它的Con ... -
RemoteView总结
2011-02-23 11:15 2340我最初认识RemoteView是在AppWidget里面的,但 ... -
Google Search Widget, Google Search ap, Globe Search ap
2011-02-23 10:39 1290http://blog.csdn.net/Judy889/ar ... -
调用android system Search UI须注意的问题
2011-02-23 10:38 1543http://blog.csdn.net/Judy889/ar ...
相关推荐
SendBroadcast 和 SendStickyBroadcast 是 Android 广播机制中两个广泛使用的方法,但是它们之间存在着一些关键的区别。本文将对这两种广播机制进行深入分析和讲解,帮助开发者更好地理解和使用这两种广播机制。 ...
发送广播消息时,将信息和过滤信息放入Intent对象,然后通过sendBroadcast或sendStickyBroadcast方法进行发送。接收到Intent的对象将与注册的BroadcastReceiver进行匹配,匹配成功后调用onReceive()方法处理消息。 ...
本实例将深入探讨如何在Android中使用`sendBroadcast`, `sendOrderedBroadcast`以及`sendStickyBroadcast`三种方法来发送广播。 一、sendBroadcast `sendBroadcast(Intent intent)`是最常见的方式,用于发送一个非...
2. 使用`sendBroadcast()`、`sendOrderedBroadcast()`或`sendStickyBroadcast()`方法将广播发送出去。这三者区别在于处理顺序和是否保留广播状态。 接收广播则需要定义一个BroadcastReceiver: 1. 创建一个继承自`...
发送广播可以通过Intent对象实现,调用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。sendBroadcast()是最简单的发送方式,所有匹配的BroadcastReceiver都会收到广播;sendOrderedBroadcast...
BroadcastReceiver(广播接收器)允许应用程序在不运行的情况下接收和响应系统或自定义事件,这在多进程环境下的通信中显得尤为重要。 在Android中,Broadcast分为系统广播和自定义广播两种。系统广播是由系统发送...
sendStickyBroadcast()会将广播保留在系统中,直到有新的广播覆盖或者被清除。 为了提高广播事件处理的灵活性,我们还可以使用IntentFilter来指定BroadcastReceiver关心的广播类型。IntentFilter可以设置ACTION...
`sendStickyBroadcast()`会将广播保留在系统中,直到有新的广播覆盖它。 ```java Intent intent = new Intent("com.example.ACTION"); sendBroadcast(intent); ``` 在"AndroidBroadcastDemo"项目中,你可能会看到...
3. 发送广播消息可通过BroadcastReceiver和Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。 4. Android的优点如开放源码、丰富的硬件支持、强大的开发工具,缺点可能包括碎片化问题...
sendStickyBroadcast()会将广播保存下来,直到有新的广播覆盖它,这对于需要持久化数据的场景非常有用。 在Android系统中,有很多内置的广播事件,如网络状态改变、电池电量变化等。开发者也可以自定义广播,通过...
发送广播主要通过`sendBroadcast()`、`sendOrderedBroadcast()`和`sendStickyBroadcast()`方法实现。其中: - `sendBroadcast(Intent intent)`用于发送普通广播,所有注册了相应Intent的接收者都会收到广播。 - `...
sendStickyBroadcast()会将广播Intent保留在系统中,直到有新的广播替换它。 三、BroadcastReceiver BroadcastReceiver是Android中用于接收广播的组件。有两种注册方式: 1. 静态注册:在AndroidManifest.xml中声明...
2. 发送广播:使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。 ```java context.sendBroadcast(intent); // 或 context.sendOrderedBroadcast(intent, null); // 或 context....
4. **发送广播**:在BroadcastSender的适当位置,使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法来发送广播。sendBroadcast()用于发送普通广播,sendOrderedBroadcast()用于发送...
- **广播事件**: `sendBroadcast()`, `sendOrderedBroadcast()`和`sendStickyBroadcast()`用于发送广播Intent,BroadcastReceiver监听并处理相关事件。 理解并熟练运用Intent和Intent过滤器对于构建可扩展、灵活的...
- 使用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法发送Intent广播。其中sendOrderedBroadcast()保证广播接收者的执行顺序,sendStickyBroadcast()会让最后发送的广播信息一直存在,直到有...
- 对于Broadcast Receiver,我们可以通过`Context.sendBroadcast()`、`Context.sendOrderedBroadcast()`或`Context.sendStickyBroadcast()`发送Intent,广播会被所有感兴趣且注册过的Broadcast Receiver接收。...
2. 使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法发送广播。不同方法的区别在于广播的分发顺序和是否保留。 接收广播的步骤: 1. 创建BroadcastReceiver类,重写onReceive()...
2. **发送广播**: 调用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法,根据需求选择发送有序或无序广播,或粘性广播。 **四、广播接收者的实现** 1. **定义BroadcastReceiver**: 创建...