feat(service): 增强 VoiceInteraction 服务与界面解析

This commit is contained in:
2026-07-28 22:33:53 +08:00
parent 157bb5a2c4
commit 764816c655
2 changed files with 41 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
package com.ttstd.dialer.service;
import android.content.Intent;
import android.os.Bundle;
import android.service.voice.VoiceInteractionService;
import android.service.voice.VoiceInteractionSession;
import com.ttstd.dialer.utils.Logger;
@@ -12,4 +15,28 @@ public class MyVoiceInteractionService extends VoiceInteractionService {
super.onCreate();
Logger.d(TAG, "onCreate");
}
@Override
public void onReady() {
super.onReady();
Logger.d(TAG, "onReady");
// 服务准备就绪,通常在此之后可以开启 Session 获取界面信息
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.d(TAG, "onStartCommand: action=" + (intent != null ? intent.getAction() : "null"));
// 如果需要主动触发获取界面信息,可以调用 showSession
// 这会触发 MyVoiceInteractionSessionService 创建 Session,并回调 onHandleAssist
if (intent != null && "ACTION_SHOW_SESSION".equals(intent.getAction())) {
showSession(new Bundle(), VoiceInteractionSession.SHOW_WITH_ASSIST | VoiceInteractionSession.SHOW_WITH_SCREENSHOT);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onShutdown() {
super.onShutdown();
Logger.d(TAG, "onShutdown");
}
}

View File

@@ -22,17 +22,28 @@ public class MyVoiceInteractionSession extends VoiceInteractionSession {
// 这是核心方法,structure 包含了整个屏幕的 View 树
Logger.d(TAG, "onHandleAssist");
if (structure != null) {
AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(0);
if (windowNode != null) {
int windowCount = structure.getWindowNodeCount();
Logger.d(TAG, "Window count: " + windowCount);
for (int i = 0; i < windowCount; i++) {
AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(i);
Logger.d(TAG, "Window[" + i + "]: title=" + windowNode.getTitle());
traverseNode(windowNode.getRootViewNode(), "");
}
} else {
Logger.w(TAG, "AssistStructure is null");
}
}
private void traverseNode(AssistStructure.ViewNode node, String prefix) {
if (node == null) return;
Logger.d(TAG, prefix + "View: text=" + node.getText() + ", id=" + node.getIdEntry());
CharSequence text = node.getText();
CharSequence contentDescription = node.getContentDescription();
String id = node.getIdEntry();
if (text != null || contentDescription != null || id != null) {
Logger.d(TAG, prefix + "View: text=" + text + ", desc=" + contentDescription + ", id=" + id + ", class=" + node.getClassName());
}
for (int i = 0; i < node.getChildCount(); i++) {
traverseNode(node.getChildAt(i), prefix + " ");