Android廣播實驗

【實驗目的】

  1. 了解使用Intent進行組件通信的原理;
  2. 了解Intent過濾器的原理和匹配機制;
  3. 掌握發送和接收廣播的方法

【實驗內容】

任務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進行布局設計。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/91172.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/91172.shtml
英文地址,請注明出處:http://en.pswp.cn/web/91172.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

html轉word下載

一、插件使用//轉html為wordnpm i html-docx-js //保存文件到本地npm i file-saver 注&#xff1a;vite 項目使用esm模式會報錯&#xff0c;with方法錯誤&#xff0c;修改如下&#xff1a;//直接安裝修復版本npm i html-docx-fixed二、封裝導出 exportWord.jsimport htmlDocx f…

北方公司面試記錄

避免被開盒&#xff0c;先稱之為“北方公司”&#xff0c;有確定結果后再更名。 先說流程&#xff0c;線下面試&#xff0c;時間非常急&#xff0c;下午兩點鐘面試&#xff0c;中午十二點打電話讓我去&#xff0c;帶兩份紙質簡歷。 和一般的菌工單位一樣&#xff0c;先在傳達室…

linux——ps命令

PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND0 1 1 1 ? -1 Ss 0 0:01 /usr/lib/systemd/systemd1 123 123 123 ? -1 S 0 0:00 /usr/sbin/sshd -D123 456 456 456 pts/0 456 R 10…

C#.NET 依賴注入詳解

一、是什么 在 C#.NET 中&#xff0c;依賴注入&#xff08;Dependency Injection&#xff0c;簡稱 DI&#xff09; 是一種設計模式&#xff0c;用于實現控制反轉&#xff08;Inversion of Control&#xff0c;IoC&#xff09;&#xff0c;以降低代碼耦合、提高可測試性和可維護…

Vue監視數據的原理和set()的使用

在 Vue 中&#xff0c;Vue.set()&#xff08;或 this.$set()&#xff09;是用于解決響應式數據更新檢測的重要方法&#xff0c;其底層與 Vue 的數據監視原理緊密相關。以下從使用場景和實現原理兩方面詳細說明&#xff1a;一、Vue.set () 的使用場景與用法1. 為什么需要 Vue.se…

在 Vue 中,如何在回調函數中正確使用 this?

在 Vue 組件中&#xff0c;this 指向當前組件實例&#xff0c;但在回調函數&#xff08;如定時器、異步請求、事件監聽等&#xff09;中&#xff0c;this 的指向可能會丟失或改變&#xff0c;導致無法正確訪問組件的屬性和方法。以下是在回調函數中正確使用 this 的幾種常見方式…

第4章唯一ID生成器——4.4 基于數據庫的自增主鍵的趨勢遞增的唯一ID

基于數據庫的自增主鍵也可以生成趨勢遞增的唯一 ID&#xff0c;且由于唯一ID不與時間戳關聯&#xff0c;所以不會受到時鐘回撥問題的影響。 4.4.1 分庫分表架構 數據庫一般都支持設置自增主鍵的初始值和自增步長&#xff0c;以MySQL為例&#xff0c;自增主鍵的自增步長由auto_i…

設計模式:Memento 模式詳解

Memento 模式詳解Memento&#xff08;備忘錄&#xff09;模式是一種行為型設計模式&#xff0c;用于在不破壞封裝性的前提下&#xff0c;捕獲并外部化一個對象的內部狀態&#xff0c;以便在之后能夠將該對象恢復到原先保存的狀態。它廣泛應用于需要實現撤銷&#xff08;Undo&am…

數據結構(6)單鏈表算法題(下)

一、環形鏈表Ⅰ 1、題目描述 https://leetcode.cn/problems/linked-list-cycle 2、算法分析 思路&#xff1a;快慢指針 根據上圖所示的流程&#xff0c;我們可以推測出這樣一個結論&#xff1a;若鏈表帶環&#xff0c;快慢指針一定會相遇。 那么&#xff0c;這個猜測是否正…

智能制造,從工廠建模,工藝建模,柔性制造,精益制造,生產管控,庫存,質量等多方面講述智能制造的落地方案。

智能制造&#xff0c;從工廠建模&#xff0c;工藝建模&#xff0c;柔性制造&#xff0c;精益制造&#xff0c;生產管控&#xff0c;庫存&#xff0c;質量等多方面講述智能制造的落地方案。

Qt 分裂布局:QSplitter 使用指南

在 GUI 開發中&#xff0c;高效管理窗口空間是提升用戶體驗的關鍵。QSplitter 作為 Qt 的核心布局組件&#xff0c;讓動態分割窗口變得簡單直觀。一、QSplitter 核心功能解析 QSplitter 是 Qt 提供的布局管理器&#xff0c;專用于創建可調節的分割區域&#xff1a; 支持水平/垂…

R語言與作物模型(DSSAT模型)技術應用

R語言在DSSAT模型的氣候、土壤、管理措施等數據準備&#xff0c;自動化模擬和結果分析上都發揮著重要的作用。一&#xff1a;DSSAT模型的高級應用 1.作物模型的概念 2.DSSAT模型發展現狀 3.DSSAT與R語言的安裝 4.DSSAT模型的高級應用案例 5.R語言在作物模型參數優化中的應用 6.…

JavaSE:學習輸入輸出編寫簡單的程序

一、打印輸出到屏幕 Java提供了三種核心輸出方法&#xff0c;適合不同場景&#xff1a; System.out.println() 打印內容后 自動換行 System.out.println("Welcome"); System.out.println("to ISS"); // 輸出&#xff1a; // Welcome // to ISSSystem.out…

訪問者模式感悟

訪問者模式 首先有兩個東西: 一個是訪問者vistor (每一個訪問者類都代表了一類操作) 一個是被訪問者entity (model /info/pojo/node等等這些都行)也就是是說是一個實體類 其操作方法被抽離給了其他類。 訪問者模式的核心思想就是**“把操作從數據結構中分離出來,每種操作…

從零到部署:基于Go和Docker的全棧短鏈接服務實戰(含源碼)

摘要&#xff1a;本文將手把手帶你使用Go語言&#xff0c;并遵循依賴倒置、分層架構等最佳實踐&#xff0c;構建一個高性能、高可用的全棧短鏈接生成器。項目采用Echo框架、GORM、Redis、MySQL&#xff0c;并通過Docker和Docker Compose實現一鍵式容器化部署到阿里云服務器。文…

MyBatis_3

上一篇文章&#xff0c;我們學習了使用XML實現MyBatis進行增、刪、查、改等操作&#xff0c;本篇文章&#xff0c;我們將學習#{ }和${ }獲取方法參數的區別和使用MyBatisXML實現動態SQL語句。 #{ }和${ }的區別 在之前的文章中我們都是使用#{ }進行賦值&#xff0c;但實際上M…

智能圖書館管理系統開發實戰系列(一):項目架構設計與技術選型

項目背景 智能圖書館管理系統&#xff08;ILMS&#xff09;是一個現代化的桌面應用程序&#xff0c;采用前后端分離架構&#xff0c;結合了Web技術的靈活性和桌面應用的用戶體驗。本項目從高保真原型設計開始&#xff0c;經過完整的軟件開發生命周期&#xff0c;最終實現為一個…

應急前端“黃金3分鐘”設計:極端場景下的操作界面極速搭建技術

摘要**地震突發&#xff0c;應急指揮系統的操作界面卻因加載緩慢無法及時調取數據&#xff1b;火災現場&#xff0c;消防員手持終端的操作步驟繁瑣&#xff0c;延誤救援時機。在分秒必爭的極端場景中&#xff0c;傳統前端操作界面為何頻頻 “掉鏈子”&#xff1f;怎樣才能在 “…

【Android】三種彈窗 Fragment彈窗管理

三三要成為安卓糕手 零&#xff1a;布局轉換 在很多工程當中用的都是LinearLayout和relativelayout&#xff0c;這兩者都可以轉化為Constrainlayout 注&#xff1a;這種用法并不能精確轉換&#xff0c;具體還是要根據自己的需求來做布局約束一&#xff1a;snackbar顯示彈窗 ((2…

【AI繪畫】Stable Diffusion webUI 與 ComfyUI 全解析:安裝、模型、插件及功能對比

一、Stable Diffusion 與 UI 工具概述 Stable Diffusion 是當前最主流的開源 AI 繪畫模型&#xff0c;通過文本描述生成高質量圖像。為降低使用門檻&#xff0c;開發者推出了多種圖形界面&#xff08;UI&#xff09;工具&#xff0c;其中AUTOMATIC1111 webUI&#xff08;簡稱 …