網站首頁 編程語言 正文
廣播結合消息跨進程通信
需求:
要實現一個在Framework中與上層app進行通信,不添加接口去實現。
項目描述:
項目中,我是在framework中進行獲取遙控器Mac信息,但是這些數據要去上層的app中顯示出來。但是不想去使用AILD的方式去實現進程之間通信。然后就想到了使用廣播結合消息去實現上述功能。
直接上代碼,重點代碼會使用start - end 標識出來; 此代碼只貼重點部分
// Android App層
public class JinJuManager implements Handler.Callback {
private final Handler mHandler = new Handler(Looper.getMainLooper(), this);
//start; framework中發送過來消息會回調到handleMessage
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (msg.obj instanceof Bundle) {
Bundle result = (Bundle) msg.obj;
//獲取到framewrok層發送過來的Mac信息
String macAddress = result.getString("MacAddress");
mActivity.updateMacAddress(macAddress);
Log.d(TAG, "Response -> MacAddress: " + macAddress);
} else {
Log.d(TAG, "Response is null.");
}
return true;
default:
return false;
}
}
// end;
// start 首先創建一個Messenger對象出來,然后將要傳遞的數據通過bundle(key,vlue)形式進行保存。
// 然后使用廣播把數據發送到framework端進行處理
public void requestMacAddress(Context context, int vendorId, int productId) {
Messenger messenger = new Messenger(mHandler);
Intent intent = new Intent("com.toptech.action.REQUEST_MAC_ADDRESS");
Bundle bundle = new Bundle();
//相當于將messnger對象保存在binder中,然后跨進程通信
bundle.putBinder("Messenger", messenger.getBinder());
bundle.putInt("VendorId", vendorId);
bundle.putInt("ProductId", productId);
intent.putExtras(bundle);
context.sendBroadcast(intent);
}
// end;
}
// framework層
public class UsbDeviceManager extends BroadcastReceiver {
public static final int VID = 4310;
public static final int PID = 45062;
private final Context mContext;
public UsbDeviceManager(Context context) {
mContext = context;
IntentFilter filter = new IntentFilter();
filter.addAction("com.toptech.action.REQUEST_MAC_ADDRESS");
mContext.registerReceiver(this, filter);
}
//start; 接收到攜帶數據的廣播
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG, "action: " + action);
if ("com.toptech.action.REQUEST_MAC_ADDRESS".equals(action)) {
Bundle extras = intent.getExtras();
if (extras == null || extras.isEmpty()) {
return;
}
//拿到app層發送過來的VID,PID
int vendorId = extras.getInt("VendorId", 0);
int productId = extras.getInt("ProductId", 0);
if (vendorId == 0 && productId == 0) {
return;
}
Log.d(TAG, "Request mac address -> vid: " + vendorId + ", pid: " + productId);
//這個方法是在framework中獲取Mac信息的方法
String macAddress = getMacAddress(vendorId, productId);
if (TextUtils.isEmpty(macAddress)) {
return;
}
//這里就是相當于獲取到app層發送過來的messenger對象
IBinder binder = extras.getBinder("Messenger");
if (binder == null) {
return;
}
//將獲取到的mac信息存入MacAddress中
Bundle result = new Bundle();
result.putString("MacAddress", macAddress);
Message message = Message.obtain();
message.what = 0;
message.obj = result;
//向上層發送消息
Messenger messenger = new Messenger(binder);
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
return;
}
}
// end;
總結:
app層(創建廣播,創建消息,發送數據) -> framewrok層(接收廣播,接收消息,發送數據) -> app層(回調handleMessage,接收數據)
原文鏈接:https://blog.csdn.net/qq_42989195/article/details/125716222
- 上一篇:Python的私有屬性及@property
- 下一篇:Handler機制相關流程
相關推薦
- 2022-10-08 淺談數據庫日期類型字段設計應該如何選擇_MsSql
- 2022-04-14 Python中五種列表拷貝的方法_python
- 2022-10-01 Iptables防火墻limit模塊擴展匹配規則詳解_安全相關
- 2022-04-29 C++?Queue隊列類模版實例詳解_C 語言
- 2022-07-22 DML語言-insert、update、delete操作
- 2023-01-10 Redis秒殺實現方案講解_Redis
- 2022-09-13 Linux中一對多配置日志服務器的詳細步驟_Linux
- 2022-08-16 python中的屬性管理機制詳解_python
- 最近更新
-
- 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同步修改后的遠程分支