# Conflicts: # app/src/main/java/com/ttstd/dialer/service/MyVoiceInteractionService.java # app/src/main/java/com/ttstd/dialer/service/MyVoiceInteractionSession.java
65 lines
2.3 KiB
Java
65 lines
2.3 KiB
Java
package com.ttstd.dialer.service;
|
||
|
||
import android.content.ComponentName;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.service.voice.VoiceInteractionService;
|
||
import android.service.voice.VoiceInteractionSession;
|
||
|
||
import com.ttstd.dialer.utils.Logger;
|
||
|
||
/**
|
||
* 语音交互服务类,作为语音助手的入口。
|
||
*/
|
||
public class MyVoiceInteractionService extends VoiceInteractionService {
|
||
private static final String TAG = "MyVoiceInteractionService";
|
||
|
||
/**
|
||
* 自定义 Action,用于从外部启动语音交互会话
|
||
*/
|
||
public static final String ACTION_SHOW_SESSION = "com.ttstd.dialer.ACTION_SHOW_SESSION";
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
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) {
|
||
String action = intent.getAction();
|
||
if (ACTION_SHOW_SESSION.equals(action)) {
|
||
// 确保服务是当前活跃的语音交互服务
|
||
if (isActiveService(this, new ComponentName(this, MyVoiceInteractionService.class))) {
|
||
Logger.d(TAG, "Showing session with assist and screenshot flags");
|
||
// 核心:调用 showSession 并请求辅助数据(AssistStructure)和截图
|
||
Bundle args = new Bundle();
|
||
showSession(args,
|
||
VoiceInteractionSession.SHOW_WITH_ASSIST |
|
||
VoiceInteractionSession.SHOW_WITH_SCREENSHOT);
|
||
} else {
|
||
Logger.w(TAG, "Service is not the active voice interaction service");
|
||
}
|
||
}
|
||
}
|
||
return START_STICKY;
|
||
}
|
||
|
||
@Override
|
||
public void onShutdown() {
|
||
super.onShutdown();
|
||
Logger.d(TAG, "onShutdown");
|
||
}
|
||
}
|