Android四大組件之廣播BroadcastReceiver詳解
定義
BroadcastReceiver,“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。在Android系統(tǒng)中,廣播體現(xiàn)在方方面面,例如當(dāng)開機完成后系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能實現(xiàn)開機啟動服務(wù)的功能;當(dāng)網(wǎng)絡(luò)狀態(tài)改變時系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能及時地做出提示和保存數(shù)據(jù)等操作;當(dāng)電池電量改變時,系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能在電量低時告知用戶及時保存進度等等。Android中的廣播機制設(shè)計的非常出色,很多事情原本需要開發(fā)者親自操作的,現(xiàn)在只需等待廣播告知自己就可以了,大大減少了開發(fā)的工作量和開發(fā)周期。而作為應(yīng)用開發(fā)者,就需要數(shù)練掌握Android系統(tǒng)提供的一個開發(fā)利器,那就是BroadcastReceiver。
在我們詳細分析創(chuàng)建BroadcastReceiver的兩種注冊方式前,我們先羅列本次分析的大綱:
(1)對靜態(tài)和動態(tài)兩種注冊方式進行概念闡述以及演示實現(xiàn)步驟
(2)簡述兩種BroadcastReceiver的類型(為后續(xù)注冊方式的對比做準備)
(3)在默認廣播類型下設(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?Studio實現(xiàn)購買售賣系統(tǒng)
這篇文章主要為大家詳細介紹了Android?Studio實現(xiàn)購買售賣系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Android Fragment動態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動態(tài)創(chuàng)建詳解的相關(guān)資料,并附實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android 通過Base64上傳圖片到服務(wù)器實現(xiàn)實例
這篇文章主要介紹了Android 通過Base64上傳圖片到服務(wù)器實現(xiàn)實例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android自定義控件(實現(xiàn)狀態(tài)提示圖表)
本篇文章主要介紹了android實現(xiàn)狀態(tài)提示圖表的功能,實現(xiàn)了動態(tài)圖表的顯示,有需要的朋友可以了解一下。2016-11-11

