M5: Provider 前后台生命周期(非激活槽真正停 native 活动)
forEachForeground/forEachBackground 此前扇出但多数 Provider 未实现 → 非激活槽 native 监听仍跑。补齐:
- ShakeProvider:onBackground 停传感器、onForeground 按 H5 意图(wantShake)恢复;区分 H5 请求态与实际监听态
- LocationProvider:onBackground 停连续定位、onForeground 按 wantContinuous 恢复(单次定位无需恢复)
- NetworkProvider:onBackground 注销网络订阅、onForeground 重订阅(抽 unsubscribe)
- AudioProvider:onBackground 停语音/音效播放(非激活端不出声)
至此"同时只有一个激活"在 native 侧也落实:去激活槽停传感器/定位/网络/音频;H5 侧由 appservice('2') 自停。
WeChat resp 路由保持现状(微信整体延期;双 Web 下发起槽持续存活,边缘情形见 m3-deferred 记录)。
devecocli build + scripts/test.sh 通过。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -48,6 +48,12 @@ export class AudioProvider implements CapabilityProvider {
|
||||
});
|
||||
}
|
||||
|
||||
/** 槽去激活:停语音/音效播放(非激活端不出声,M5 §7.5)。H5 再激活后按需重触发。 */
|
||||
onBackground(): void {
|
||||
this.releaseVoice();
|
||||
this.releaseSfx();
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
this.releaseVoice();
|
||||
this.releaseSfx();
|
||||
|
||||
@@ -22,6 +22,7 @@ export class LocationProvider implements CapabilityProvider {
|
||||
private context: common.UIAbilityContext | undefined = undefined;
|
||||
private lastInfo: MaplocationInfo | undefined = undefined;
|
||||
private continuousOn: boolean = false;
|
||||
private wantContinuous: boolean = false;
|
||||
private readonly onLocation: (loc: geoLocationManager.Location) => void;
|
||||
|
||||
constructor() {
|
||||
@@ -39,7 +40,20 @@ export class LocationProvider implements CapabilityProvider {
|
||||
});
|
||||
}
|
||||
|
||||
/** 槽激活:恢复连续定位(若 H5 仍要)。 */
|
||||
onForeground(): void {
|
||||
if (this.wantContinuous) {
|
||||
this.startContinuous();
|
||||
}
|
||||
}
|
||||
|
||||
/** 槽去激活:停连续定位监听(保留 H5 意图)。 */
|
||||
onBackground(): void {
|
||||
this.stopContinuous();
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
this.wantContinuous = false;
|
||||
this.stopContinuous();
|
||||
}
|
||||
|
||||
@@ -54,6 +68,7 @@ export class LocationProvider implements CapabilityProvider {
|
||||
this.push(this.lastInfo);
|
||||
return;
|
||||
}
|
||||
this.wantContinuous = continuous;
|
||||
if (continuous) {
|
||||
this.startContinuous();
|
||||
} else {
|
||||
|
||||
@@ -30,7 +30,23 @@ export class NetworkProvider implements CapabilityProvider {
|
||||
this.subscribe();
|
||||
}
|
||||
|
||||
/** 槽激活:恢复网络变化订阅。 */
|
||||
onForeground(): void {
|
||||
if (this.conn === undefined) {
|
||||
this.subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
/** 槽去激活:注销网络变化订阅(非激活端不监听)。 */
|
||||
onBackground(): void {
|
||||
this.unsubscribe();
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
this.unsubscribe();
|
||||
}
|
||||
|
||||
private unsubscribe(): void {
|
||||
const c = this.conn;
|
||||
if (c !== undefined) {
|
||||
c.unregister((_e: BusinessError) => { });
|
||||
|
||||
@@ -28,6 +28,7 @@ export class ShakeProvider implements CapabilityProvider {
|
||||
private feedbackOn: boolean = false;
|
||||
private pending: boolean = false;
|
||||
private endTimer: number = -1;
|
||||
private wantShake: boolean = false;
|
||||
private readonly onAccel: (data: sensor.AccelerometerResponse) => void;
|
||||
|
||||
constructor() {
|
||||
@@ -37,17 +38,32 @@ export class ShakeProvider implements CapabilityProvider {
|
||||
register(bridge: BridgeController, _ctx: CapabilityContext): void {
|
||||
this.bridge = bridge;
|
||||
bridge.registerHandler(InboundHandlers.StartShake, (_d: string, _cb: (resp: string) => void) => {
|
||||
this.wantShake = true;
|
||||
this.start();
|
||||
});
|
||||
bridge.registerHandler(InboundHandlers.SwitchShake, (data: string, _cb: (resp: string) => void) => {
|
||||
this.feedbackOn = data === '1';
|
||||
});
|
||||
bridge.registerHandler(InboundHandlers.StopShake, (_d: string, _cb: (resp: string) => void) => {
|
||||
this.wantShake = false;
|
||||
this.stop();
|
||||
});
|
||||
}
|
||||
|
||||
/** 槽激活:若 H5 仍要摇一摇则恢复监听(M5 §7.5)。 */
|
||||
onForeground(): void {
|
||||
if (this.wantShake) {
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
|
||||
/** 槽去激活:停传感器监听(保留 H5 意图,激活时恢复)。 */
|
||||
onBackground(): void {
|
||||
this.stop();
|
||||
}
|
||||
|
||||
onDestroy(): void {
|
||||
this.wantShake = false;
|
||||
this.stop();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user