【实验目的】
- 了解使用Intent进行组件通信的原理;
- 了解Intent过滤器的原理和匹配机制;
- 掌握发送和接收广播的方法
【实验内容】
任务1、普通广播;
任务2、系统广播;
任务3、有序广播;
【实验要求】
1、练习使用静态方法和动态方法注册广播接收器
2、练习发送广播消息的方法;
【实验设计】
src/main/java/com/example/broadcastdemo/:包含所有的Java类文件。
MainActivity.java:应用的主活动,负责发送普通和有序广播。
NormalBroadcastReceiver.java:接收普通广播的接收器。
OrderedBroadcastReceiver.java:接收有序广播的接收器。
src/main/res/:包含资源文件。
layout/:包含布局文件。
activity_main.xml:主活动的布局。
activity_second.xml:第二个活动的布局。
mipmap/:包含应用图标资源。
values/:包含字符串和其他资源值。
AndroidManifest.xml:定义应用的配置,包括活动、接收器等组件。
关键组件:
活动(Activity):
MainActivity:定义了发送广播的按钮和逻辑。
SecondActivity:未在代码中定义,但布局文件存在。
广播接收器(BroadcastReceiver):
NormalBroadcastReceiver:接收自定义的普通广播。
OrderedBroadcastReceiver:接收自定义的有序广播。
布局文件(XML):
activity_main.xml和activity_second.xml:定义了用户界面,包含发送广播的按钮。
【实验结果】
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastdemo">
<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/AppTheme">
<activity android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="@string/app_name" />
<!-- 普通广播接收器 -->
<receiver android:name=".NormalBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.broadcast.normal" />
</intent-filter>
</receiver>
<!-- 系统广播接收器 -->
<receiver android:name=".SystemBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
<!-- 有序广播接收器 -->
<receiver android:name=".OrderedBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.example.broadcast.ordered" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
package com.example.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private NormalBroadcastReceiver normalBroadcastReceiver;
private SystemBroadcastReceiver systemBroadcastReceiver;
private OrderedBroadcastReceiver orderedBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化广播接收器
normalBroadcastReceiver = new NormalBroadcastReceiver();
systemBroadcastReceiver = new SystemBroadcastReceiver();
orderedBroadcastReceiver = new OrderedBroadcastReceiver();
// 设置按钮监听器
Button sendNormalBtn = findViewById(R.id.send_normal_btn);
sendNormalBtn.setOnClickListener(view -> sendNormalBroadcast());
Button sendOrderedBtn = findViewById(R.id.send_ordered_btn);
sendOrderedBtn.setOnClickListener(view -> sendOrderedBroadcast());
}
@Override
protected void onResume() {
super.onResume();
// 动态注册广播接收器
IntentFilter normalFilter = new IntentFilter("com.example.broadcast.normal");
registerReceiver(normalBroadcastReceiver, normalFilter);
IntentFilter systemFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(systemBroadcastReceiver, systemFilter);
IntentFilter orderedFilter = new IntentFilter("com.example.broadcast.ordered");
registerReceiver(orderedBroadcastReceiver, orderedFilter);
}
@Override
protected void onPause() {
super.onPause();
// 动态注销广播接收器
unregisterReceiver(normalBroadcastReceiver);
unregisterReceiver(systemBroadcastReceiver);
unregisterReceiver(orderedBroadcastReceiver);
}
// 发送普通广播
private void sendNormalBroadcast() {
Intent intent = new Intent("com.example.broadcast.normal");
sendBroadcast(intent);
}
// 发送有序广播
private void sendOrderedBroadcast() {
Intent intent = new Intent("com.example.broadcast.ordered");
sendOrderedBroadcast(intent, null); // null表示没有权限限制
}
}
NormalBroadcastReceiver.java
package com.example.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class NormalBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收到普通广播后的处理逻辑
Log.d("Broadcast", "Received normal broadcast");
}
}
OrderedBroadcastReceiver.java
package com.example.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class OrderedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收到有序广播后的处理逻辑
Log.d("Broadcast", "Received ordered broadcast");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/send_normal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Normal Broadcast"
app:layout_constraintBottom_toTopOf="@+id/send_ordered_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/send_ordered_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Ordered Broadcast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/send_normal_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".SecondActivity">
<Button
android:id="@+id/send_normal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Normal Broadcast"
app:layout_constraintBottom_toTopOf="@+id/send_ordered_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/send_ordered_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Ordered Broadcast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/send_normal_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
【实验分析或心得】
第一部分:Android组件和生命周期
Activity:
MainActivity展示了如何动态注册和注销广播接收器。
活动的生命周期方法(如onCreate、onResume、onPause)用于管理资源和状态。
BroadcastReceiver:
NormalBroadcastReceiver和OrderedBroadcastReceiver展示了如何接收和处理广播。
广播接收器可以是动态注册的,也可以在AndroidManifest.xml中静态声明。
Intent和IntentFilter:
使用Intent发送广播,IntentFilter用于过滤和接收特定的广播。
第二部分:广播机制和应用配置
广播机制:
普通广播(Normal Broadcast):无序发送,所有接收器几乎同时接收。
有序广播(Ordered Broadcast):按优先级顺序发送,可以被拦截和修改。
应用配置:
AndroidManifest.xml定义了应用的组件和权限。
每个组件(如活动、接收器)都需要在清单文件中声明。
资源管理:
布局文件(XML)定义了用户界面,使用ConstraintLayout进行布局设计。