Android四大組件之廣播BroadcastReceiver詳解
定義
BroadcastReceiver,“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。在Android系統(tǒng)中,廣播體現(xiàn)在方方面面,例如當(dāng)開機(jī)完成后系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能實現(xiàn)開機(jī)啟動服務(wù)的功能;當(dāng)網(wǎng)絡(luò)狀態(tài)改變時系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能及時地做出提示和保存數(shù)據(jù)等操作;當(dāng)電池電量改變時,系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能在電量低時告知用戶及時保存進(jìn)度等等。Android中的廣播機(jī)制設(shè)計的非常出色,很多事情原本需要開發(fā)者親自操作的,現(xiàn)在只需等待廣播告知自己就可以了,大大減少了開發(fā)的工作量和開發(fā)周期。而作為應(yīng)用開發(fā)者,就需要數(shù)練掌握Android系統(tǒng)提供的一個開發(fā)利器,那就是BroadcastReceiver。
在我們詳細(xì)分析創(chuàng)建BroadcastReceiver的兩種注冊方式前,我們先羅列本次分析的大綱:
(1)對靜態(tài)和動態(tài)兩種注冊方式進(jìn)行概念闡述以及演示實現(xiàn)步驟
(2)簡述兩種BroadcastReceiver的類型(為后續(xù)注冊方式的對比做準(zhǔn)備)
(3)在默認(rèn)廣播類型下設(shè)置優(yōu)先級和無優(yōu)先級情況下兩種注冊方式的比較
(4)在有序廣播類型下兩種注冊方式的比較
(5)通過接受打電話的廣播,在程序(Activity)運行時和終止運行時,對兩種注冊方式的比較
(6)總結(jié)兩種方式的特點
一、靜態(tài)和動態(tài)注冊方式
? 構(gòu)建Intent,使用sendBroadcast方法發(fā)出廣播定義一個廣播接收器,該廣播接收器繼承BroadcastReceiver,并且覆蓋onReceive()方法來響應(yīng)事件注冊該廣播接收器,我們可以在代碼中注冊(動態(tài)注冊),也可以AndroidManifest.xml配置文件中注冊(靜態(tài)注冊)。
案例解析:
1.主界面設(shè)計
<?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.后臺代碼設(shè)計
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 {
//定義對象;村長:一樣權(quán)威,光輝的存在,拿著大喇叭,講話;
Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //取值
btnSend=(Button) findViewById(R.id.btnSend);
//這對這個按鈕做監(jiān)聽事件;發(fā)送信息,大喇叭...
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
//設(shè)置intent的動作;后面字符串是自定義的
intent.setAction("android.intent.action.receiverdata");
intent.putExtra("msg","羊村各位村民開會了");
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();
//針對上述做判斷;第一個判斷是否為空也可以寫成action.isEmpty
if(!TextUtils.isEmpty(action)&&"android.intent.action.receiverdata".equals(action)){
String msg=intent.getStringExtra("msg");//不習(xí)慣可以使用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.運行效果
在這里插入圖片描述


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

