241 lines
5.0 KiB
JavaScript
241 lines
5.0 KiB
JavaScript
/**
|
|
* 广播管理器
|
|
* 负责Android广播的注册、监听和销毁
|
|
*/
|
|
export default class BroadcastManager {
|
|
constructor() {
|
|
// 核心组件
|
|
this.main = null;
|
|
this.filter = null;
|
|
this.receiver = null;
|
|
|
|
// 状态控制
|
|
this.isListening = false;
|
|
this.isEnabled = false;
|
|
|
|
// 外部配置(必须由外部设置)
|
|
this.config = null;
|
|
|
|
// 回调函数
|
|
this.callbacks = null;
|
|
|
|
// 基础初始化
|
|
this._initEnvironment();
|
|
}
|
|
|
|
/**
|
|
* 初始化运行环境
|
|
*/
|
|
_initEnvironment() {
|
|
try {
|
|
// 检查HTML5+环境
|
|
if (typeof plus === 'undefined' || !plus.android) {
|
|
console.warn('[BroadcastManager] 非Android环境');
|
|
this.isEnabled = false;
|
|
return;
|
|
}
|
|
|
|
// 获取主Activity
|
|
this.main = plus.android.runtimeMainActivity();
|
|
if (!this.main) {
|
|
console.warn('[BroadcastManager] 无法获取主Activity');
|
|
this.isEnabled = false;
|
|
return;
|
|
}
|
|
|
|
// 导入必要的Android类
|
|
this.IntentFilter = plus.android.importClass('android.content.IntentFilter');
|
|
this.isEnabled = true;
|
|
console.log('[BroadcastManager] 环境初始化成功');
|
|
|
|
} catch (error) {
|
|
console.error('[BroadcastManager] 环境初始化失败:', error);
|
|
this.isEnabled = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置配置和回调
|
|
*/
|
|
setConfig(config, callbacks) {
|
|
if (!config || !config.broadcastAction) {
|
|
console.error('[BroadcastManager] 配置必须包含broadcastAction');
|
|
return false;
|
|
}
|
|
|
|
this.config = { ...config };
|
|
this.callbacks = callbacks || {};
|
|
|
|
console.log('[BroadcastManager] 配置已设置');
|
|
|
|
// 如果正在监听,重新注册
|
|
if (this.isListening) {
|
|
this._unregister();
|
|
return this._register();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 创建广播接收器
|
|
*/
|
|
_createReceiver() {
|
|
try {
|
|
this.filter = null;
|
|
this.receiver = null;
|
|
|
|
// 创建IntentFilter
|
|
this.filter = new this.IntentFilter();
|
|
this.filter.addAction(this.config.broadcastAction);
|
|
|
|
// 创建接收器
|
|
this.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
|
|
onReceive: (context, intent) => {
|
|
this._handleBroadcast(context, intent);
|
|
}
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
console.error('[BroadcastManager] 创建接收器失败:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理接收到的广播
|
|
*/
|
|
_handleBroadcast(context, intent) {
|
|
try {
|
|
plus.android.importClass(intent);
|
|
|
|
const dataKey = this.config.dataKey || 'value';
|
|
const data = intent.getStringExtra(dataKey);
|
|
|
|
// 诊断日志:每次收到广播都打印(含 data 为空的情况),便于排查“后续扫描无反应”
|
|
console.log('[BroadcastManager] 收到广播 action=', intent.getAction(), ' dataKey=', dataKey, ' data=', data);
|
|
|
|
if (!data) {
|
|
return;
|
|
}
|
|
|
|
const result = {
|
|
data: data,
|
|
timestamp: Date.now(),
|
|
action: intent.getAction(),
|
|
dataKey: dataKey
|
|
};
|
|
|
|
if (this.callbacks.onResult) {
|
|
this.callbacks.onResult(result);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('[BroadcastManager] 处理广播失败:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 注册接收器
|
|
*/
|
|
_register() {
|
|
if (!this.isEnabled || !this.config) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
if (!this._createReceiver()) {
|
|
return false;
|
|
}
|
|
|
|
this.main.registerReceiver(this.receiver, this.filter);
|
|
this.isListening = true;
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
console.error('[BroadcastManager] 注册失败:', error);
|
|
this.isListening = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 注销接收器
|
|
*/
|
|
_unregister() {
|
|
if (!this.isListening || !this.receiver) {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
this.main.unregisterReceiver(this.receiver);
|
|
this.isListening = false;
|
|
return true;
|
|
|
|
} catch (error) {
|
|
console.error('[BroadcastManager] 注销失败:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 开始监听
|
|
*/
|
|
start() {
|
|
if (this.isListening) {
|
|
return true;
|
|
}
|
|
|
|
if (!this.config) {
|
|
console.error('[BroadcastManager] 请先设置配置');
|
|
return false;
|
|
}
|
|
|
|
return this._register();
|
|
}
|
|
|
|
/**
|
|
* 停止监听
|
|
*/
|
|
stop() {
|
|
return this._unregister();
|
|
}
|
|
|
|
/**
|
|
* 暂停监听
|
|
*/
|
|
pause() {
|
|
return this.stop();
|
|
}
|
|
|
|
/**
|
|
* 恢复监听
|
|
*/
|
|
resume() {
|
|
return this.start();
|
|
}
|
|
|
|
/**
|
|
* 获取状态
|
|
*/
|
|
getStatus() {
|
|
return {
|
|
isEnabled: this.isEnabled,
|
|
isListening: this.isListening,
|
|
hasConfig: !!this.config,
|
|
currentAction: this.config ? this.config.broadcastAction : null
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 销毁资源
|
|
*/
|
|
destroy() {
|
|
this.stop();
|
|
this.config = null;
|
|
this.callbacks = null;
|
|
}
|
|
} |