软件编程
位置:首页>> 软件编程>> Android编程>> Android四大组件之广播BroadcastReceiver详解

Android四大组件之广播BroadcastReceiver详解

作者:tea_year  发布时间:2023-03-29 23:41:14 

标签:Android,四大组件,广播,BroadcastReceiver

定义

BroadcastReceiver,“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播。在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这条广播就能实现开机启动服务的功能;当网络状态改变时系统会产生一条广播,接收到这条广播就能及时地做出提示和保存数据等操作;当电池电量改变时,系统会产生一条广播,接收到这条广播就能在电量低时告知用户及时保存进度等等。Android中的广播机制设计的非常出色,很多事情原本需要开发者亲自操作的,现在只需等待广播告知自己就可以了,大大减少了开发的工作量和开发周期。而作为应用开发者,就需要数练掌握Android系统提供的一个开发利器,那就是BroadcastReceiver。
在我们详细分析创建BroadcastReceiver的两种注册方式前,我们先罗列本次分析的大纲:
(1)对静态和动态两种注册方式进行概念阐述以及演示实现步骤
(2)简述两种BroadcastReceiver的类型(为后续注册方式的对比做准备)
(3)在默认广播类型下设置优先级和无优先级情况下两种注册方式的比较
(4)在有序广播类型下两种注册方式的比较
(5)通过接受打电话的广播,在程序(Activity)运行时和终止运行时,对两种注册方式的比较
(6)总结两种方式的特点
一、静态和动态注册方式
? 构建Intent,使用sendBroadcast方法发出广播定义一个广播 * ,该广播 * 继承BroadcastReceiver,并且覆盖onReceive()方法来响应事件注册该广播 * ,我们可以在代码中注册(动态注册),也可以AndroidManifest.xml配置文件中注册(静态注册)。

案例解析:

1.主界面设计


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:orientation="vertical"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

<Button
       android:id="@+id/btnSend"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:insetTop="16dp"
       android:text="发松" />
</LinearLayout>

如图:

Android四大组件之广播BroadcastReceiver详解

2.后台代码设计


package com.aaa.btdemo02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
   //定义对象;村长:一样权威,光辉的存在,拿着大喇叭,讲话;
   Button btnSend;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main); //取值
       btnSend=(Button) findViewById(R.id.btnSend);
       //这对这个按钮做监听事件;发送信息,大喇叭...
       btnSend.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Intent intent=new Intent();
               //设置intent的动作;后面字符串是自定义的
               intent.setAction("android.intent.action.receiverdata");
               intent.putExtra("msg","羊村各位村民开会了");
               MainActivity.this.sendBroadcast(intent);
           }
       });
   }
}

3.创建自己的广播 * 类


package com.aaa.btdemo02;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
       //接受广播
       if(intent==null)return;
       //intent:接受从主端传递过来的数据,action数据;
       String action=intent.getAction();
       //针对上述做判断;第一个判断是否为空也可以写成action.isEmpty
       if(!TextUtils.isEmpty(action)&&"android.intent.action.receiverdata".equals(action)){
           String msg=intent.getStringExtra("msg");//不习惯可以使用Bundle
           Log.i("喜洋洋-->",msg);
       }
   }
}

4.注册广播


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.aaa.btdemo02">

<application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/Theme.Btdemo02">
       <activity
           android:name=".MainActivity"
           android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       <receiver android:name=".MyReceiver"
           android:exported="true">
           <intent-filter>
               <!-- 自定义的action名 -->
               <action android:name="android.intent.action.receiverdata"/>
           </intent-filter>
       </receiver>
   </application>

</manifest>

5.运行效果

在这里插入图片描述

Android四大组件之广播BroadcastReceiver详解

Android四大组件之广播BroadcastReceiver详解

来源:https://blog.csdn.net/zhangchen124/article/details/121105794

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com