Android四大組件之廣播BroadcastReceiver詳解
定義
BroadcastReceiver,“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。在Android系統(tǒng)中,廣播體現(xiàn)在方方面面,例如當(dāng)開機(jī)完成后系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能實(shí)現(xiàn)開機(jī)啟動(dòng)服務(wù)的功能;當(dāng)網(wǎng)絡(luò)狀態(tài)改變時(shí)系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能及時(shí)地做出提示和保存數(shù)據(jù)等操作;當(dāng)電池電量改變時(shí),系統(tǒng)會產(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。
在我們詳細(xì)分析創(chuàng)建BroadcastReceiver的兩種注冊方式前,我們先羅列本次分析的大綱:
(1)對靜態(tài)和動(dòng)態(tài)兩種注冊方式進(jìn)行概念闡述以及演示實(shí)現(xiàn)步驟
(2)簡述兩種BroadcastReceiver的類型(為后續(xù)注冊方式的對比做準(zhǔn)備)
(3)在默認(rèn)廣播類型下設(shè)置優(yōu)先級和無優(yōu)先級情況下兩種注冊方式的比較
(4)在有序廣播類型下兩種注冊方式的比較
(5)通過接受打電話的廣播,在程序(Activity)運(yùn)行時(shí)和終止運(yùn)行時(shí),對兩種注冊方式的比較
(6)總結(jié)兩種方式的特點(diǎn)
一、靜態(tài)和動(dòng)態(tài)注冊方式
? 構(gòu)建Intent,使用sendBroadcast方法發(fā)出廣播定義一個(gè)廣播接收器,該廣播接收器繼承BroadcastReceiver,并且覆蓋onReceive()方法來響應(yīng)事件注冊該廣播接收器,我們可以在代碼中注冊(動(dòng)態(tài)注冊),也可以AndroidManifest.xml配置文件中注冊(靜態(tài)注冊)。
案例解析:
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.后臺代碼設(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 { //定義對象;村長:一樣權(quán)威,光輝的存在,拿著大喇叭,講話; Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //取值 btnSend=(Button) findViewById(R.id.btnSend); //這對這個(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","羊村各位村民開會了"); 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(); //針對上述做判斷;第一個(gè)判斷是否為空也可以寫成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.運(yùn)行效果
在這里插入圖片描述
到此這篇關(guān)于Android四大組件之廣播BroadcastReceiver詳解的文章就介紹到這了,更多相關(guān)Android 四大組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11Android自定義View app更新動(dòng)畫詳解
這篇文章給大家分享了Android自定義View app更新動(dòng)畫的相關(guān)代碼以及知識點(diǎn)內(nèi)容,有興趣的朋友參考學(xué)習(xí)下。2018-07-07Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05android編程實(shí)現(xiàn)的自定義注釋模板實(shí)例
這篇文章主要介紹了android編程實(shí)現(xiàn)的自定義注釋模板,以完整實(shí)例形式分析了Android自定義魔板的定義及具體實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-11-11Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹
大家好,本篇文章主要講的是Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android自定義控件(實(shí)現(xiàn)狀態(tài)提示圖表)
本篇文章主要介紹了android實(shí)現(xiàn)狀態(tài)提示圖表的功能,實(shí)現(xiàn)了動(dòng)態(tài)圖表的顯示,有需要的朋友可以了解一下。2016-11-11