一、接收广播
(1)用法简介
首先继承BroadcastReceiver,需要重写onReceive方法(两个参数:1.context,2.intent),然后通过registerReceiver方法注册,需要传两个参数,一个是BroadcastReceiver的实例,另一个是IntentFilter的实例,IntentFilter的实例用addAction设置Action。(有两种注册方式,静态动态,由于静态越来越不推荐了,这里只说动态注册)
系统常用的广播
(2)代码示例
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv);
MyBroadcastReceiver receiver=new MyBroadcastReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(receiver,filter);//动态注册
}
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent data){
if(data.getAction().equals(Intent.ACTION_SCREEN_ON)){
tv.setText("屏幕被打开");
}
}
}
}
(3)运行结果
接收屏幕被打开的广播
二、发送无序广播
广播分为有序广播和无序广播,无序广播广播一发出几乎广播接收者几乎同时接收到,接到广播的先后是随机的,有序广播是一级一级接收的优先级高的先接收,并且可以拦截
(1)使用介绍
新建Intent,在Intent里设置Action(setAction),然后调用sendBroadcast(intent)就完成了
(2)代码示例
//创建一个按钮,一点击就发送广播,然后被自己的广播接收者接收
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv);
MyBroadcastReceiver receiver=new MyBroadcastReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("com.hello.world");
registerReceiver(receiver,filter);
}
public void click(View v){
Intent intent=new Intent();
intent.putExtra("data","hello,world");
intent.setAction("com.hello.world");
sendBroadcast(intent);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent data){
if(data.getAction().equals("com.hello.world")){
tv.setText(data.getStringExtra("data"));
}
}
}
}
(3)运行结果
发送广播并接收三、发送有序广播
(1)用法介绍
- 只要将发送无序广播的sendBroadcast改为sendOrderedBroadcast就可以了
- 广播接收者在注册时声明优先级就可以了
-可以通过abortBroadcast就可以拦截广播
(2)代码示例
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter1=new IntentFilter();
filter1.addAction("hello");
filter1.setPriority(100);
IntentFilter filter2=new IntentFilter();
filter2.addAction("hello");
MyBroadcastReceiver1 receiver1=new MyBroadcastReceiver1();
MyBroadcastReceiver2 receiver2=new MyBroadcastReceiver2();
registerReceiver(receiver1,filter1);
registerReceiver(receiver2,filter2);
}
public void click(View view){
Intent intent=new Intent("hello");
sendOrderedBroadcast(intent,null);
}
class MyBroadcastReceiver1 extends BroadcastReceiver{
public void onReceive(Context context, Intent intent){
Log.i("test","我优先级高,我可以修改数据,可以拦截广播");
}
}
class MyBroadcastReceiver2 extends BroadcastReceiver{
public void onReceive(Context context,Intent intent){
Log.i("test","我优先级低");
}
}
}
要想拦截广播
abort Broadcast();
运行结果
每次接收的广播先后顺序都一样四、发送和接收本地广播
(1)使用介绍
之前的广播都是系统级的都可以接收到,存在安全问题,所以可以使用本地广播,首先调用LocalBroadcastManager.getInstance(this)获取LocalBroadcastManager对象,然后通过LocalBroadcastManager来registerReceiver和unRegisterReceiver,通过LocalBroadcastManager来sendBroadcast
(2)代码示例
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager manager=LocalBroadcastManager.getInstance(this);
MyBroadcastReceiver receiver=new MyBroadcastReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("hello");
manager.registerReceiver(receiver,filter);
}
public void click(View v){
Intent intent=new Intent("hello");
LocalBroadcastManager manager=LocalBroadcastManager.getInstance(this);
manager.sendBroadcast(intent);
}
class MyBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent){
Log.i("test","我是本地广播接收器");
}
}
}
五、粘性广播
(1)使用介绍
通过sendStickyBroadcast()方法发送广播,该广播发送后会一直滞留,当有匹配的接收者注册时就可以接收这条广播,发送粘性广播需要权限<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
可以通过removeSticky Broadcast()移除,粘性广播只保留最后一条
(2)代码示例
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v){
Intent intent=new Intent("hello");
sendStickyBroadcast(intent);
}
public void click2(View v){
IntentFilter filter=new IntentFilter();
filter.addAction("hello");
MyReceiver receiver=new MyReceiver();
registerReceiver(receiver,filter);
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("test","粘性广播我来接收你");
}
}
}
网友评论