android Broadcast 广播的另一种不常用,但是很有用的方式。

本文详细介绍了Android中粘性广播的概念及其实现方式。通过对比普通广播,解释了粘性广播如何使得Intent能够在接收者处于活跃状态时被重新接收到。并通过示例代码展示了发送粘性广播的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



有关在Content.java中的说明:

/** * Perform a {@link #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 {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In * all other ways, this behaves the same as * {@link #sendBroadcast(Intent)}. * * <p>You must hold the {@link android.Manifest.permission#BROADCAST_STICKY} * permission in order to use this API. If you do not hold that * permission, {@link SecurityException} will be thrown. * * @param 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. * * @see #sendBroadcast(Intent) */ public abstract void sendStickyBroadcast(Intent intent);
实例:

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处于活跃的时候,又会接受到它。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值