java - 我可以制作一个始终运行语音识别的后台服务吗
问题描述
我想制作一个应用程序,我可以通过 siri 或谷歌助手等语音运行它,所以为了实现它,我必须在运行语音识别器的后台或前台服务中实现代码,所以这是可能的,因为android Oreo 和更高版本上工作服务的限制这让我陷入了困境,而且我不是那么专业,所以我可以自己解决这个问题
我试图用 IntentService 做一个前台服务,这样它就可以在不冻结 UI 的情况下在后台工作,并且语音识别器不起作用
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");
}
}
解决方案
推荐阅读
- node.js - MongoDB中的数学问题
- c# - C# VSTO Excel 相互之间的几个领域
- python - 分割和保存部分输入文本
- android - 如何让单一布局在横向和纵向中看起来不同?
- sql - 如何在 Tableau 中对 running_sum 计算进行分区?
- reactjs - 带有列和分隔线的导航栏在它们之间垂直
- javascript - 检索上传到 Firebase 存储的图像并在我的 React 本机博览会代码中使用该 URL
- python - 将 pycuda 与 reikna fft 一起使用
- android - 如何在 CircleImageView 中显示完整图像?
- java - 当供应商在 Spring Cloud Stream 3.x 中返回 null 时出现 NullPointerException