網站首頁 編程語言 正文
Broadcast
應用程序之間傳輸信息的機制
BroadcastReceiver
接收來自系統和應用中的廣播
作用:在特定時間發送特定的標識,讓程序進行操作
使用方法
注:
- BroadcastReceiver生命周期只有十秒左右
- BroadcastReceiver里不能做一些比較耗時的操作
- 應該通過Intent給Service,有Service完成
- 不能使用子線程
廣播的種類
普通廣播(Normal broadcasts)
所有監聽該廣播的廣播接收者都可以監聽到該廣播
特點:
- 同級別接收先后是隨機的
- 級別低的后收到廣播
- 接收器不能截斷廣播的繼續傳播也不能處理廣播
- 同級別動態注冊高于靜態注冊
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: //發送一條普通廣播
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: 點擊發送廣播");
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="發送一條普通廣播" 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指定接收者包路徑和類路徑
如果需要發給多個類廣播,就使用intent.setPackage(“com.example.broadcastsdemo”);
同一包下的BroadcastReceiver都可以接收到廣播
設置優先級
<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中設置android:priority就可以了
值-1000到1000越大優先級越高
截斷廣播
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();關鍵字
發現普通廣播無法中斷廣播的發送
靜態注冊是在xml中注冊
動態注冊
@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);
}
動態注冊大于靜態注冊,但是他的作用域太低,容易死掉
測試BC1發廣播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","廣播處理的數據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得到的數據"+test );
}
}
發現普通廣播無法傳送數據
有序廣播(ordered broadcasts)
按照接收者的優先級順序接收廣播,優先級在intent-filter中的priority中聲明。-1000到1000之間,值越大,優先級越高。可以終止廣播意圖的繼續傳播,接收者可以篡改內容
特點:
- 同級別接收順序是隨機的
- 能截斷廣播的繼續傳播,高級別的廣播接收器收到該廣播后,可以決定把該廣播是否截斷
- 接收器能截斷廣播的繼續傳播,也能處理廣播
- 同級別動態注冊高于靜態注冊
添加按鈕
<?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="發送一條普通廣播" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:onClick="doclick" android:id="@+id/send2" android:text="發送一條有序廣播" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
添加事件處理
case R.id.send2: //發送一條普通廣播
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: 點擊發送有序廣播");
break;
發現有序廣播可以實現BC2發消息給BC1,且可以中斷廣播
異步廣播(粘滯性滯留廣播)
不能將處理結果傳給下一個接收者,無法終止廣播
添加按鈕及事件
<Button android:onClick="doclick" android:id="@+id/send3" android:text="發送一條異步廣播" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
case R.id.send3: //發送一條異步廣播
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;
可以發送和接收分開,先發送再接收
發送完廣播要卸載
原文鏈接:https://blog.csdn.net/liuwushuang233/article/details/126365731
相關推薦
- 2022-06-13 Docker鏡像的commit操作示例及作用_docker
- 2022-07-21 IDEA報錯Error running ‘Application‘: Command line is
- 2022-08-05 Python?list列表查找元素詳情_python
- 2022-08-14 Shell腳本實現監測文件變化的示例詳解_linux shell
- 2022-06-18 kubernetes(k8s)安裝metrics-server實現資源使用情況監控方式詳解_云其它
- 2022-04-11 following signatures couldn‘t be verified because
- 2022-01-26 iconv(): Detected an illegal character in input st
- 2022-11-11 Android利用Canvas類繪制圖形_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支