问题描述
首先,我知道这个问题已经被问过多次了,但是我似乎仍然找不到能够解决我的问题的答案。
所以这是我的服务类(编辑2:,现在几乎所有代码都在这里-抱歉,这些变量是葡萄牙语的->如果需要,我可以翻译所有内容):
public class Google_voice_recognition extends Service implements RecognitionListener {
private SpeechRecognizer speechRecognizer;
private boolean a_ouvir = false;
private String processar_fala_string = "";
private String ultima_fala_processada = "";
private String fala_processada_total = "";
private long ativar_resultados_parciais_tempo = 0;
private int ativar_resultados_parciais_num = 0;
private boolean ativar_resultados_parciais = true;
private String ultima_fala_nao_util = "";
private Context contexto;
private boolean onEndOfSpeech_ultimo = false;
private long reposicao_reconhecimento_google_erro_tempo = 0;
public static final String ACAO_ENVIAR_TAREFA = "enviar_tarefa()";
@Override
public void onCreate() {
super.onCreate();
contexto = getApplicationContext();
System.out.println(this);
System.out.println("whatever 1");
if (contexto == null) {
System.out.println("null 1");
}
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
ativar_resultados_parciais = false;//Utils_gerais.func_ext_disponiveis();
System.out.println(ativar_resultados_parciais);
System.out.println(this);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(contexto);
Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,contexto.getPackageName());
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE,true);
}
Reconhecimento_voz_Google listener = new Reconhecimento_voz_Google();
speechRecognizer.setRecognitionListener(listener);
speechRecognizer.startListening(mSpeechRecognizerIntent);
processamento_fala_parcial.start();
reposicao_reconhecimento_google_erro.start();
System.out.println("whatever 2");
if (contexto == null) {
System.out.println("null 2");
}
return START_NOT_STICKY;
}
@Override
public void onBeginningOfSpeech() {
ultima_fala_processada = "";
fala_processada_total = "";
processar_fala_string = "";
ativar_resultados_parciais_num = 0;
onEndOfSpeech_ultimo = false;
ultima_fala_nao_util = "";
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
System.out.println("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG");
a_ouvir = false;
onEndOfSpeech_ultimo = true;
reposicao_reconhecimento_google_erro_tempo = System.currentTimeMillis();
ativar_resultados_parciais = false;
}
@Override
public void onError(int error) {
if (!a_ouvir) {
return;
}
a_ouvir=false;
onEndOfSpeech_ultimo = false;
reposicao_reconhecimento_google_erro_tempo = 0;
System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
System.out.println(error);
ativar_resultados_parciais = false;
speechRecognizer.stopListening();
}
@Override
public void onEvent(int eventType,Bundle params) {
}
@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (ativar_resultados_parciais) {
if (matches != null) {
System.out.println("++++++++++++++++++++++++++");
System.out.println(matches.get(0).toLowerCase());
if (!matches.get(0).toLowerCase().equals(ultima_fala_processada) && matches.get(0).toLowerCase().length() - 1 > ativar_resultados_parciais_num) {
processar_fala_string = matches.get(0).toLowerCase().substring(ativar_resultados_parciais_num);
ativar_resultados_parciais_tempo = System.currentTimeMillis();
System.out.println(processar_fala_string);
}
System.out.println("++++++++++++++++++++++++++");
}
}
}
@Override
public void onReadyForSpeech(Bundle params) {
System.out.println("UUUUUUUUUUUUUUUUUUUUUUUUUUUUU");
a_ouvir=true;
}
@Override
public void onResults(Bundle results) {
onEndOfSpeech_ultimo = false;
reposicao_reconhecimento_google_erro_tempo = 0;
a_ouvir=false;
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
System.out.println("--------------------------");
System.out.println(matches);
System.out.println("--------------------------");
if (matches != null) {
System.out.println("A------------------------A");
System.out.println(matches.get(0).toLowerCase().substring(ativar_resultados_parciais_num));
System.out.println("A------------------------A");
}
ativar_resultados_parciais = false;
if (matches != null) {
if (matches.get(0).toLowerCase().length() - 1 > ativar_resultados_parciais_num && !matches.get(0).toLowerCase().equals(fala_processada_total)) {
processar_fala_string = matches.get(0).toLowerCase().substring(ativar_resultados_parciais_num);
if (!processar_fala_string.equals(ultima_fala_processada)) {
System.out.println("whatever 3");
if (contexto == null) {
//System.out.println(getApplicationContext()); --> "getApplicationContext() on a null object reference"
System.out.println(getBaseContext());
System.out.println(this);
System.out.println("null 3");
}
Intent intent = new Intent(ACAO_ENVIAR_TAREFA);
intent.putExtra("extras_frase_str",matches.get(0).toLowerCase().substring(ativar_resultados_parciais_num));
intent.putExtra("extras_resultados_parciais",false);
LocalbroadcastManager.getInstance(this).sendbroadcast(intent);
}
}
}
System.out.println(speechRecognizer);
if (speechRecognizer != null) {
speechRecognizer.stopListening();
speechRecognizer.cancel();
speechRecognizer.destroy();
speechRecognizer = null;
}
Utils_processos.encerrar_pid(Utils_processos.obter_pid_atual());
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
System.out.println("RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR");
if (speechRecognizer != null) {
speechRecognizer.stopListening();
speechRecognizer.cancel();
speechRecognizer.destroy();
speechRecognizer = null;
}
super.onDestroy();
}
}
我删去了许多与问题无关的部分,只保留了我认为与问题有关的部分(在某处使用了Context
)。
当我正常启动服务时(startService
或startForeground
-我使用第一个服务,因为它在Lollipop上),在我从onCreate()
和this
也具有相同的onStartCommand()
值。这很酷。现在我不明白的是为什么当我从this
方法中调用getApplicationContext()
时,会在某些空值上调用它。
我的应用程序方面,因为我似乎并不完全了解Android的RecognitionListener
东西,只是它似乎与流程有关。说到进程,也许说此服务在单独的进程上运行可能会有用吗?它应该自动开始识别,监听内容,并告诉另一个正在运行的服务执行一个功能(因此是广播)。它是在一个单独的过程中,因此即使我告诉它立即停止,我也可以随时取消它,而不必等到它决定停止时才终止。这样,它实际上马上就停止了。但由于这个原因,我无法使其完全发挥作用。它停止并填充,但不返回结果。这是很有用的xD。
LocalbroadcastManager
在@NonNull
public static LocalbroadcastManager getInstance(@NonNull Context context) {
synchronized (mlock) {
if (mInstance == null) {
mInstance = new LocalbroadcastManager(context.getApplicationContext());
}
return mInstance;
}
}
部分。那就是错误所在。我可以将context.getApplicationContext()
变量或context
传递给this
,但是无论如何都会出现错误,因为使用了getInstance()
,这似乎是问题所在。我不知道为什么当我从getApplicationContext()
调用getApplicationContext()
时发生这种情况,或者可能是RecognitionListener的其他任何方法?那是因为它不是来自Service的一部分方法吗?如果是这样,有什么办法可以向其他服务广播消息?我试图避免使用onResults()
或文件之类的东西。
顺便说一句,对我来说很奇怪:“ null 3”出现在logcat上,而其他都没有.....那怎么可能?如果我不告诉它更改变量,为什么会更改它呢?
编辑1:如果有用,这是错误的堆栈跟踪。
SharedPreferences
编辑3:如果有用,START_STICKY,START_STICKY_COMPATIBILITY或START_REDELIVER_INTENT不执行任何操作。仍然是完全相同的错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)