Android BroadcastReceiver傳輸機(jī)制詳解
Broadcast
應(yīng)用程序之間傳輸信息的機(jī)制
BroadcastReceiver
接收來(lái)自系統(tǒng)和應(yīng)用中的廣播
作用:在特定時(shí)間發(fā)送特定的標(biāo)識(shí),讓程序進(jìn)行操作
使用方法
注:
- BroadcastReceiver生命周期只有十秒左右
- BroadcastReceiver里不能做一些比較耗時(shí)的操作
- 應(yīng)該通過(guò)Intent給Service,有Service完成
- 不能使用子線(xiàn)程
廣播的種類(lèi)
普通廣播(Normal broadcasts)
所有監(jiān)聽(tīng)該廣播的廣播接收者都可以監(jiān)聽(tīng)到該廣播
特點(diǎn):
- 同級(jí)別接收先后是隨機(jī)的
- 級(jí)別低的后收到廣播
- 接收器不能截?cái)鄰V播的繼續(xù)傳播也不能處理廣播
- 同級(jí)別動(dòng)態(tài)注冊(cè)高于靜態(tài)注冊(cè)
package com.example.broadcastsdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doclick(View view) {
switch (view.getId()){
case R.id.send1: //發(fā)送一條普通廣播
Intent intent = new Intent();
intent.putExtra("msg","這是一條普通廣播");
intent.setAction("BC_MSG");
intent.setComponent(new ComponentName("com.example.broadcastsdemo", "com.example.broadcastsdemo.BC1"));
sendBroadcast(intent);
Log.e("TAG", "doclick: 點(diǎn)擊發(fā)送廣播");
break;
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:onClick="doclick"
android:id="@+id/send1"
android:text="發(fā)送一條普通廣播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
BC1
package com.example.broadcastsdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BC1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.e("TAG", "BC1接收到的廣播信息 "+msg);
}
}
高版本需要用intent.setComponent指定接收者包路徑和類(lèi)路徑
如果需要發(fā)給多個(gè)類(lèi)廣播,就使用intent.setPackage(“com.example.broadcastsdemo”);
同一包下的BroadcastReceiver都可以接收到廣播
設(shè)置優(yōu)先級(jí)
<receiver android:name=".BC1"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="BC_MSG"/>
</intent-filter>
</receiver>
<receiver android:name=".BC2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="400">
<action android:name="BC_MSG"/>
</intent-filter>
</receiver>
只需要在intent-filter中設(shè)置android:priority就可以了
值-1000到1000越大優(yōu)先級(jí)越高
截?cái)鄰V播
public class BC2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.e("TAG", "BC2接收到的廣播信息 "+msg);
abortBroadcast();
}
}
使用 abortBroadcast();關(guān)鍵字
發(fā)現(xiàn)普通廣播無(wú)法中斷廣播的發(fā)送
靜態(tài)注冊(cè)是在xml中注冊(cè)
動(dòng)態(tài)注冊(cè)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter("BC_MSG");
BC2 bc2 = new BC2();
registerReceiver(bc2,intentFilter);
}
動(dòng)態(tài)注冊(cè)大于靜態(tài)注冊(cè),但是他的作用域太低,容易死掉
測(cè)試BC1發(fā)廣播BC2收
public class BC1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.e("TAG", "BC1接收到的廣播信息 "+msg);
Bundle bundle = new Bundle();
bundle.putString("test","廣播處理的數(shù)據(jù)BC1");
setResultExtras(bundle);
}
}
public class BC2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.e("TAG", "BC2接收到的廣播信息 "+msg);
// abortBroadcast();
Bundle bundle = getResultExtras(true);
String test = bundle.getString("test");
Log.e("TAG", "BC2得到的數(shù)據(jù)"+test );
}
}
發(fā)現(xiàn)普通廣播無(wú)法傳送數(shù)據(jù)
有序廣播(ordered broadcasts)
按照接收者的優(yōu)先級(jí)順序接收廣播,優(yōu)先級(jí)在intent-filter中的priority中聲明。-1000到1000之間,值越大,優(yōu)先級(jí)越高??梢越K止廣播意圖的繼續(xù)傳播,接收者可以篡改內(nèi)容
特點(diǎn):
- 同級(jí)別接收順序是隨機(jī)的
- 能截?cái)鄰V播的繼續(xù)傳播,高級(jí)別的廣播接收器收到該廣播后,可以決定把該廣播是否截?cái)?/li>
- 接收器能截?cái)鄰V播的繼續(xù)傳播,也能處理廣播
- 同級(jí)別動(dòng)態(tài)注冊(cè)高于靜態(tài)注冊(cè)
添加按鈕
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:onClick="doclick"
android:id="@+id/send1"
android:text="發(fā)送一條普通廣播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:onClick="doclick"
android:id="@+id/send2"
android:text="發(fā)送一條有序廣播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
添加事件處理
case R.id.send2: //發(fā)送一條普通廣播
Intent intent2 = new Intent();
intent2.putExtra("msg","這是一條有序廣播");
intent2.setAction("BC_MSG");
//intent.setComponent(new ComponentName("com.example.broadcastsdemo", "com.example.broadcastsdemo.BC1"));
intent2.setPackage("com.example.broadcastsdemo");
sendOrderedBroadcast(intent2,null);
Log.e("TAG", "doclick: 點(diǎn)擊發(fā)送有序廣播");
break;
發(fā)現(xiàn)有序廣播可以實(shí)現(xiàn)BC2發(fā)消息給BC1,且可以中斷廣播
異步廣播(粘滯性滯留廣播)
不能將處理結(jié)果傳給下一個(gè)接收者,無(wú)法終止廣播
添加按鈕及事件
<Button
android:onClick="doclick"
android:id="@+id/send3"
android:text="發(fā)送一條異步廣播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
case R.id.send3: //發(fā)送一條異步廣播
Intent intent3 = new Intent();
intent3.putExtra("msg","這是一條有序廣播");
intent3.setAction("BC_MSG");
//intent3.setComponent(new ComponentName("com.example.broadcastsdemo", "com.example.broadcastsdemo.BC3"));
intent3.setPackage("com.example.broadcastsdemo");
sendStickyBroadcast(intent3);
IntentFilter intentFilter = new IntentFilter("BC_MSG");
BC3 bc3 = new BC3();
registerReceiver(bc3,intentFilter);
break;
可以發(fā)送和接收分開(kāi),先發(fā)送再接收
發(fā)送完廣播要卸載
到此這篇關(guān)于A(yíng)ndroid BroadcastReceiver傳輸機(jī)制詳解的文章就介紹到這了,更多相關(guān)Android BroadcastReceiver 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
100行Android代碼輕松實(shí)現(xiàn)帶動(dòng)畫(huà)柱狀圖
這篇文章主要教大家通過(guò)100行Android代碼輕松實(shí)現(xiàn)帶動(dòng)畫(huà)柱狀圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
RxJava之網(wǎng)絡(luò)請(qǐng)求最常見(jiàn)的三種場(chǎng)景
本文想闡述一下當(dāng)你開(kāi)發(fā)Android應(yīng)用并采用RxJava作為你的架構(gòu),尤其是有關(guān)網(wǎng)絡(luò)請(qǐng)求時(shí)最常見(jiàn)的三種場(chǎng)景。這篇文章主要介紹了RxJava之網(wǎng)絡(luò)請(qǐng)求最常見(jiàn)的三種場(chǎng)景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android基于騰訊云實(shí)時(shí)音視頻仿微信視頻通話(huà)最小化懸浮
這篇文章主要為大家詳細(xì)介紹了Android基于騰訊云實(shí)時(shí)音視頻實(shí)現(xiàn)類(lèi)似微信視頻通話(huà)最小化懸浮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android開(kāi)發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android RippleDrawable 水波紋/漣漪效果的實(shí)現(xiàn)
這篇文章主要介紹了Android RippleDrawable 水波紋/漣漪效果的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
基于A(yíng)ndroid實(shí)現(xiàn)數(shù)獨(dú)游戲
這篇文章主要為大家詳細(xì)介紹了基于A(yíng)ndroid實(shí)現(xiàn)數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
學(xué)習(xí)使用Material Design控件(二)使用DrawerLayout實(shí)現(xiàn)側(cè)滑菜單欄效果
這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,使用DrawerLayout和NavigationView實(shí)現(xiàn)側(cè)滑菜單欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

