Android四大組件之廣播BroadcastReceiver詳解
定義
BroadcastReceiver,“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。在Android系統(tǒng)中,廣播體現(xiàn)在方方面面,例如當(dāng)開機(jī)完成后系統(tǒng)會(huì)產(chǎn)生一條廣播,接收到這條廣播就能實(shí)現(xiàn)開機(jī)啟動(dòng)服務(wù)的功能;當(dāng)網(wǎng)絡(luò)狀態(tài)改變時(shí)系統(tǒng)會(huì)產(chǎn)生一條廣播,接收到這條廣播就能及時(shí)地做出提示和保存數(shù)據(jù)等操作;當(dāng)電池電量改變時(shí),系統(tǒng)會(huì)產(chǎn)生一條廣播,接收到這條廣播就能在電量低時(shí)告知用戶及時(shí)保存進(jìn)度等等。Android中的廣播機(jī)制設(shè)計(jì)的非常出色,很多事情原本需要開發(fā)者親自操作的,現(xiàn)在只需等待廣播告知自己就可以了,大大減少了開發(fā)的工作量和開發(fā)周期。而作為應(yīng)用開發(fā)者,就需要數(shù)練掌握Android系統(tǒng)提供的一個(gè)開發(fā)利器,那就是BroadcastReceiver。
在我們?cè)敿?xì)分析創(chuàng)建BroadcastReceiver的兩種注冊(cè)方式前,我們先羅列本次分析的大綱:
(1)對(duì)靜態(tài)和動(dòng)態(tài)兩種注冊(cè)方式進(jìn)行概念闡述以及演示實(shí)現(xiàn)步驟
(2)簡(jiǎn)述兩種BroadcastReceiver的類型(為后續(xù)注冊(cè)方式的對(duì)比做準(zhǔn)備)
(3)在默認(rèn)廣播類型下設(shè)置優(yōu)先級(jí)和無優(yōu)先級(jí)情況下兩種注冊(cè)方式的比較
(4)在有序廣播類型下兩種注冊(cè)方式的比較
(5)通過接受打電話的廣播,在程序(Activity)運(yùn)行時(shí)和終止運(yùn)行時(shí),對(duì)兩種注冊(cè)方式的比較
(6)總結(jié)兩種方式的特點(diǎn)
一、靜態(tài)和動(dòng)態(tài)注冊(cè)方式
? 構(gòu)建Intent,使用sendBroadcast方法發(fā)出廣播定義一個(gè)廣播接收器,該廣播接收器繼承BroadcastReceiver,并且覆蓋onReceive()方法來響應(yīng)事件注冊(cè)該廣播接收器,我們可以在代碼中注冊(cè)(動(dòng)態(tài)注冊(cè)),也可以AndroidManifest.xml配置文件中注冊(cè)(靜態(tài)注冊(cè))。
案例解析:
1.主界面設(shè)計(jì)
<?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="發(fā)松" />
</LinearLayout>
如圖:

2.后臺(tái)代碼設(shè)計(jì)
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 {
//定義對(duì)象;村長(zhǎng):一樣權(quán)威,光輝的存在,拿著大喇叭,講話;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //取值
btnSend=(Button) findViewById(R.id.btnSend);
//這對(duì)這個(gè)按鈕做監(jiān)聽事件;發(fā)送信息,大喇叭...
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
//設(shè)置intent的動(dòng)作;后面字符串是自定義的
intent.setAction("android.intent.action.receiverdata");
intent.putExtra("msg","羊村各位村民開會(huì)了");
MainActivity.this.sendBroadcast(intent);
}
});
}
}
3.創(chuàng)建自己的廣播接收器類
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:接受從主端傳遞過來的數(shù)據(jù),action數(shù)據(jù);
String action=intent.getAction();
//針對(duì)上述做判斷;第一個(gè)判斷是否為空也可以寫成action.isEmpty
if(!TextUtils.isEmpty(action)&&"android.intent.action.receiverdata".equals(action)){
String msg=intent.getStringExtra("msg");//不習(xí)慣可以使用Bundle
Log.i("喜洋洋-->",msg);
}
}
}
4.注冊(cè)廣播
<?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.運(yùn)行效果
在這里插入圖片描述


到此這篇關(guān)于Android四大組件之廣播BroadcastReceiver詳解的文章就介紹到這了,更多相關(guān)Android 四大組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android數(shù)據(jù)庫(kù)Room的實(shí)際使用過程總結(jié)
這篇文章主要給大家介紹了關(guān)于Android數(shù)據(jù)庫(kù)Room的實(shí)際使用過程,詳細(xì)介紹了如何創(chuàng)建實(shí)體類、數(shù)據(jù)訪問對(duì)象(DAO)和數(shù)據(jù)庫(kù)抽象類,需要的朋友可以參考下2025-01-01
Kotlin語(yǔ)言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析
這篇文章主要介紹了Kotlin語(yǔ)言中CompileSdkVersion和targetSdkVersion有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
基于Android SQLiteOpenHelper && CRUD 的使用
本篇文章小編為大家介紹,基于Android SQLiteOpenHelper && CRUD的使用。需要的朋友可以參考一下2013-04-04
Android 為L(zhǎng)istView添加分段標(biāo)頭的方法
下面小編就為大家?guī)硪黄狝ndroid 為L(zhǎng)istView添加分段標(biāo)頭的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android實(shí)現(xiàn)中國(guó)象棋游戲(局域網(wǎng)版)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)局域網(wǎng)版的中國(guó)象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android Compose實(shí)現(xiàn)伸縮ToolBar的思路詳解
這篇文章主要介紹了Android Compose之伸縮ToolBar的實(shí)現(xiàn),本文給大家分享主要實(shí)現(xiàn)思路及實(shí)現(xiàn)過程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
當(dāng)Flutter遇到節(jié)流與防抖的思路和流程優(yōu)化
這篇文章主要給大家介紹了關(guān)于當(dāng)Flutter遇到節(jié)流與防抖的思路和流程優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Android開發(fā)之滑動(dòng)圖片輪播標(biāo)題焦點(diǎn)
這篇文章主要介紹了Android開發(fā)之滑動(dòng)圖片輪播標(biāo)題焦點(diǎn)的相關(guān)資料,需要的朋友可以參考下2016-05-05
Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開發(fā)過程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方,需要的朋友可以參考下2019-05-05

