`

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.

光从字面的意思是很难理解的。只有你写例子才会明白的。
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处于活跃的时候,又会接受到它。




分享到:
评论

相关推荐

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

    SendBroadcast 和 SendStickyBroadcast 是 Android 广播机制中两个广泛使用的方法,但是它们之间存在着一些关键的区别。本文将对这两种广播机制进行深入分析和讲解,帮助开发者更好地理解和使用这两种广播机制。 ...

    基于Android的中国象棋局域网博弈平台的设计与实现.pdf

    发送广播消息时,将信息和过滤信息放入Intent对象,然后通过sendBroadcast或sendStickyBroadcast方法进行发送。接收到Intent的对象将与注册的BroadcastReceiver进行匹配,匹配成功后调用onReceive()方法处理消息。 ...

    android broadcast实例

    本实例将深入探讨如何在Android中使用`sendBroadcast`, `sendOrderedBroadcast`以及`sendStickyBroadcast`三种方法来发送广播。 一、sendBroadcast `sendBroadcast(Intent intent)`是最常见的方式,用于发送一个非...

    android的广播机制演示

    2. 使用`sendBroadcast()`、`sendOrderedBroadcast()`或`sendStickyBroadcast()`方法将广播发送出去。这三者区别在于处理顺序和是否保留广播状态。 接收广播则需要定义一个BroadcastReceiver: 1. 创建一个继承自`...

    Android广播

    发送广播可以通过Intent对象实现,调用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。sendBroadcast()是最简单的发送方式,所有匹配的BroadcastReceiver都会收到广播;sendOrderedBroadcast...

    2011.09.07(3)——— android 跨进程通信之Broadcast

    BroadcastReceiver(广播接收器)允许应用程序在不运行的情况下接收和响应系统或自定义事件,这在多进程环境下的通信中显得尤为重要。 在Android中,Broadcast分为系统广播和自定义广播两种。系统广播是由系统发送...

    Android应用源码之20.广播事件处理(1).zip

    sendStickyBroadcast()会将广播保留在系统中,直到有新的广播覆盖或者被清除。 为了提高广播事件处理的灵活性,我们还可以使用IntentFilter来指定BroadcastReceiver关心的广播类型。IntentFilter可以设置ACTION...

    Android广播实现代码

    `sendStickyBroadcast()`会将广播保留在系统中,直到有新的广播覆盖它。 ```java Intent intent = new Intent("com.example.ACTION"); sendBroadcast(intent); ``` 在"AndroidBroadcastDemo"项目中,你可能会看到...

    Android程序设计期末试题A.pdf

    3. 发送广播消息可通过BroadcastReceiver和Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。 4. Android的优点如开放源码、丰富的硬件支持、强大的开发工具,缺点可能包括碎片化问题...

    BroadcastReceiver的小案例

    sendStickyBroadcast()会将广播保存下来,直到有新的广播覆盖它,这对于需要持久化数据的场景非常有用。 在Android系统中,有很多内置的广播事件,如网络状态改变、电池电量变化等。开发者也可以自定义广播,通过...

    广播的使用

    发送广播主要通过`sendBroadcast()`、`sendOrderedBroadcast()`和`sendStickyBroadcast()`方法实现。其中: - `sendBroadcast(Intent intent)`用于发送普通广播,所有注册了相应Intent的接收者都会收到广播。 - `...

    android IntentBroadcast

    sendStickyBroadcast()会将广播Intent保留在系统中,直到有新的广播替换它。 三、BroadcastReceiver BroadcastReceiver是Android中用于接收广播的组件。有两种注册方式: 1. 静态注册:在AndroidManifest.xml中声明...

    android 广播实例,可以收发

    2. 发送广播:使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法。 ```java context.sendBroadcast(intent); // 或 context.sendOrderedBroadcast(intent, null); // 或 context....

    安卓简单广播发送器(课程设计)

    4. **发送广播**:在BroadcastSender的适当位置,使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法来发送广播。sendBroadcast()用于发送普通广播,sendOrderedBroadcast()用于发送...

    【Android开发API】应用的组成部分-意图和意图过滤器.pdf

    - **广播事件**: `sendBroadcast()`, `sendOrderedBroadcast()`和`sendStickyBroadcast()`用于发送广播Intent,BroadcastReceiver监听并处理相关事件。 理解并熟练运用Intent和Intent过滤器对于构建可扩展、灵活的...

    Android移动应用开发中BroadcastReceiver单元主要内容.pdf

    - 使用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法发送Intent广播。其中sendOrderedBroadcast()保证广播接收者的执行顺序,sendStickyBroadcast()会让最后发送的广播信息一直存在,直到有...

    android 基本知识

    - 对于Broadcast Receiver,我们可以通过`Context.sendBroadcast()`、`Context.sendOrderedBroadcast()`或`Context.sendStickyBroadcast()`发送Intent,广播会被所有感兴趣且注册过的Broadcast Receiver接收。...

    跨进程通信 广播

    2. 使用Context的sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法发送广播。不同方法的区别在于广播的分发顺序和是否保留。 接收广播的步骤: 1. 创建BroadcastReceiver类,重写onReceive()...

    Android广播接收和发送

    2. **发送广播**: 调用sendBroadcast()、sendOrderedBroadcast()或sendStickyBroadcast()方法,根据需求选择发送有序或无序广播,或粘性广播。 **四、广播接收者的实现** 1. **定义BroadcastReceiver**: 创建...

Global site tag (gtag.js) - Google Analytics