【實驗目的】
- 了解使用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進行布局設計。