Java 我可以做一个始终运行语音识别的后台服务吗
我想做一个应用程序,我可以在语音上运行它,比如siri或google assistant,所以为了实现它,我必须在后台或前台服务中实现代码,该服务将运行语音识别器 所以这是可能的,因为安卓ORO的工作服务的局限性更高,这使我陷入了无处无中的困境。 而我不是那么专业,所以我可以自己解决这个问题 我曾尝试使用IntentService创建一个前台服务,这样它就可以在后台工作,而不会冻结UI 而且语音识别器不工作Java 我可以做一个始终运行语音识别的后台服务吗,java,android,speech-recognition,background-service,foreground-service,Java,Android,Speech Recognition,Background Service,Foreground Service,我想做一个应用程序,我可以在语音上运行它,比如siri或google assistant,所以为了实现它,我必须在后台或前台服务中实现代码,该服务将运行语音识别器 所以这是可能的,因为安卓ORO的工作服务的局限性更高,这使我陷入了无处无中的困境。 而我不是那么专业,所以我可以自己解决这个问题 我曾尝试使用IntentService创建一个前台服务,这样它就可以在后台工作,而不会冻结UI 而且语音识别器不工作 package com.example.intentservice; import a
package com.example.intentservice;
import android.app.IntentService;
import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import static com.example.intentservice.App.CHANNEL_ID;
public class ExampleService extends IntentService {
public static final String TAG = "ExampleService";
private PowerManager.WakeLock wakeLock;
private TextToSpeech textToSpeech;
private SpeechRecognizer recognizer;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
initRec();
startListening();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ExampleService:wakelock");
wakeLock.acquire();
Log.e(TAG, "wakelock acquired");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("noti")
.setContentText("running")
.build();
startForeground(1, notification);
}
}
private void startListening() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
recognizer.startListening(intent);
}
private void initRec() {
if (SpeechRecognizer.isRecognitionAvailable(this)) {
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
List<String> res = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Toast.makeText(ExampleService.this, res.get(0), Toast.LENGTH_SHORT).show();
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
}
}
public ExampleService() {
super("ExampleService");
// to create service again
setIntentRedelivery(true);
}
// this just to test the code
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.e(TAG, "onHandleIntent");
String string = intent.getStringExtra("key");
for (int i = 0; i < 10; i++) {
Log.e(TAG, string + "-" + i);
// Toast.makeText(this, "i", Toast.LENGTH_SHORT).show();
SystemClock.sleep(1000);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
wakeLock.release();
Log.e(TAG, "wakelock realised");
}
}
package com.example.intentservice;
导入android.app.IntentService;
导入android.app.Notification;
导入android.content.Intent;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.PowerManager;
导入android.os.SystemClock;
导入android.speech.RecognitionListener;
导入android.speech.RecognizerIntent;
导入android.SpeechRecognizer;
导入android.speech.tts.TextToSpeech;
导入android.util.Log;
导入android.widget.Toast;
导入androidx.annotation.Nullable;
导入java.util.List;
导入java.util.Locale;
导入静态com.example.intentservice.App.CHANNEL\u ID;
公共类ExampleService扩展了IntentService{
公共静态最终字符串TAG=“ExampleService”;
私有PowerManager.WakeLock WakeLock;
私有texttospeechtexttospeech;
私人语音识别器;
@凌驾
public void onCreate(){
super.onCreate();
Log.e(标记为“onCreate”);
initRec();
惊人的倾听();
PowerManager PowerManager=(PowerManager)getSystemService(POWER\u服务);
wakeLock=powerManager.newWakeLock(powerManager.PARTIAL_WAKE_LOCK,“示例服务:wakeLock”);
wakeLock.acquire();
Log.e(标签“已获取wakelock”);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
Notification Notification=new Notification.Builder(此,通道ID)
.setContentTitle(“noti”)
.setContentText(“正在运行”)
.build();
启动前(1,通知);
}
}
私人监听(){
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(RecognizerIntent.EXTRA语言模型,RecognizerIntent.LANGUAGE模型自由形式);
intent.putExtra(识别器intent.EXTRA_MAX_结果,1);
识别器。惊人监听(意图);
}
私有void initRec(){
if(SpeechRecognizer.isRecognitionAvailable(this)){
识别器=SpeechRecognizer.createSpeechRecognizer(此);
recognizer.setRecognitionListener(新的RecognitionListener(){
@凌驾
ReadyForSpeech上的公共无效(捆绑){
}
@凌驾
开始时的公共无效fSpeech(){
}
@凌驾
公开无效(浮动v){
}
@凌驾
接收到的公用void onbuffer(字节[]字节){
}
@凌驾
公共无效onEndOfSpeech(){
}
@凌驾
公共无效申报人(int i){
}
@凌驾
公共void onResults(Bundle){
List res=bundle.getStringArrayList(SpeechRecognitor.RESULTS\u RECOGNITION);
Toast.makeText(例如service.this、res.get(0)、Toast.LENGTH_SHORT.show();
}
@凌驾
公共void onPartialResults(Bundle){
}
@凌驾
公共void onEvent(int i,Bundle){
}
});
}
}
公共示例服务(){
超级(“示例服务”);
//重新创建服务
setIntentRedelivery(真);
}
//这只是为了测试代码
@凌驾
受保护的无效onHandleIntent(@Nullable Intent){
Log.e(标签“onHandleIntent”);
String String=intent.getStringExtra(“key”);
对于(int i=0;i<10;i++){
Log.e(标签,字符串+“-”+i);
//Toast.makeText(这个“i”,Toast.LENGTH_SHORT).show();
系统时钟。睡眠(1000);
}
}
@凌驾
公共空间{
super.ondestory();
Log.e(标签“onDestroy”);
wakeLock.release();
日志e(标签“wakelock”);
}
}
这可能是android 4的重复,我需要应用程序在更高的api级别上运行,顺便说一句,前台服务不会在主线程上运行,语音识别器只在主线程上工作。嗨,我正在尝试做同样的事情。你是否以某种方式解决了这个问题?Tnx!!!这无法完成,因为服务需要在主线程上运行,您必须访问加速计传感器,当它检测到变化时,您可以打开应用程序