Android-Broadcast广播事件(1)-简介及普通广播调用步骤

前言

BroadcastReceiver即广播接收器,是专门用于接受广播消息以及做出相应处理的组件。其本质就是一个全局监听器,接收程序所发出的Broadcast Intent。

但是它是没有用户界面的,可以启动一个Activity来响应接收到的信息或者用NotificationManager来通知用户。
总体而言,广播机制包含三个要素:
1. 发送广播的Broadcast;
2. 接收广播的BroadcastReceiver;
3. 以及用于前面两者之间传递消息的Intent;

1. 广播事件开发步骤

1. 定义一个广播接收器

只需重写onReceive方法

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        //method
    }
}

2. 注册广播事件

注册广播事件 有两种方式

  • 动态注册,代码中调用BroadcastReceiver的Context的registerReceiver()方法进行注册
//        实例化定义好的BroadcastReceiver
        MyReceiver receiver=new MyReceiver();
//        实例化过滤器,并设置过滤的广播的action,
        IntentFilter filter=new IntentFilter("MY_ACTION");
//        注册
        registerReceiver(receiver,filter);
  • 静态注册
<receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="MY_ACTION"></action>
            </intent-filter>
  </receiver>

3. 发送广播

               Intent intent=new Intent();
//               action需要与注册广播的action一样
               intent.setAction("MY_ACTION");
//               传递的消息
               intent.putExtra("msg","send Broadcast");
//               发送
               sendBroadcast(intent);

4. 设置接受广播的处理函数

 public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //method
        String msg=intent.getStringExtra("msg");
        System.out.println(msg);
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }

2. 例子

  • MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        实例化定义好的BroadcastReceiver
        MyReceiver receiver=new MyReceiver();
//        实例化过滤器,并设置过滤的广播的action,
        IntentFilter filter=new IntentFilter("MY_ACTION");
//        注册
        registerReceiver(receiver,filter);

button=(Button)findViewById(R.id.button);
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent();
//               action需要与注册广播的action一样
               intent.setAction("MY_ACTION");
//               传递的消息
               intent.putExtra("msg","send Broadcast");
//               发送
               sendBroadcast(intent);

           }
       });
    }
}
  • BroadcastReceiver
ublic class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //method
        String msg=intent.getStringExtra("msg");
        System.out.println(intent.getAction());
        System.out.println(msg);
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
}
  • 测试
    发送广播后,logcat显示为:
    测试结果
    在这里我们可以发现,广播接受器与发送的广播是通过intent的action属性来匹配的,当相互action一致,则广播接收器回调onReceive函数执行操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值