服务泄漏了最初绑定在此处的ServiceConnection android.speech.SpeechRecognizer$Connection@2e1ecaf

Apollocy:

我正在尝试运行一项服务,该服务一旦开始使用Speech Recognizer录制音频5秒钟。但是,无论何时启动该服务,它都会onError在语音识别器类中调用该方法,而一旦我停止该服务,它就会给我错误:

服务泄漏了最初绑定在此处的ServiceConnection android.speech.SpeechRecognizer$Connection@2e1ecaf

我已经尝试了一切,四处寻找,但似乎找不到解决方案。请帮忙。

这是我的服务代码,它使用中的按钮启动和停止mainActivity

MicrophoneService.java

package com.example.daisymicrophone;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import static com.example.daisymicrophone.App.CHANNEL_ID;

public class MicrophoneService extends Service {
    protected static SpeechRecognizer mSpeechRecognizer;
    protected Intent mSpeechRecognizerIntent;
    Context c;

    public static final String TAG = "MicrophoneService";

    @Override
    public void onCreate() {
        super.onCreate();
        c = getApplicationContext();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Daisy App")
                .setContentText("Daisy is listening to you")
                .setSmallIcon(R.drawable.ic_mic)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        Toast.makeText(this,"start Service.", Toast.LENGTH_SHORT).show();

        //if condition is met then do this
        SpeechRecognitionListener h = new SpeechRecognitionListener();
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        mSpeechRecognizer.setRecognitionListener(h);
        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        Log.d("avail", " " + mSpeechRecognizer.isRecognitionAvailable(this));
        if (mSpeechRecognizer.isRecognitionAvailable(this))
            Log.d("created", "onBeginingOfSpeech");
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                this.getPackageName());
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);


        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSpeechRecognizer.stopListening();
            }
        }, 5 * 1000);


        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    class SpeechRecognitionListener implements RecognitionListener {

        @Override
        public void onReadyForSpeech(Bundle bundle) {

            Log.d("onReady", "service");
        }

        @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) {
            Log.d("ERROR","ERROR");
        }

        @Override
        public void onResults(Bundle resultsBundle) {
            Log.d("Results", "onResults: " + resultsBundle.toString());
        }

        @Override
        public void onPartialResults(Bundle bundle) {

        }

        @Override
        public void onEvent(int i, Bundle bundle) {

        }
    }
}

错误记录

2020-05-09 00:13:00.199 19488-19488/com.example.daisymicrophone D/MicrophoneService: startMicService: 
2020-05-09 00:13:00.202 19488-19488/com.example.daisymicrophone V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@331f924 className=com.example.daisymicrophone.MicrophoneService packageName=com.example.daisymicrophone intent=null}
2020-05-09 00:13:00.211 19488-19488/com.example.daisymicrophone D/ActivityThread: SVC-Calling onStartCommand: com.example.daisymicrophone.MicrophoneService@c083c8d, flags=0, startId=1
2020-05-09 00:13:00.238 19488-19488/com.example.daisymicrophone D/avail:  true
2020-05-09 00:13:00.243 19488-19488/com.example.daisymicrophone D/created: onBeginingOfSpeech
2020-05-09 00:13:00.402 19488-19488/com.example.daisymicrophone D/onReady: service
2020-05-09 00:13:00.547 19488-19488/com.example.daisymicrophone D/ERROR: ERROR
2020-05-09 00:13:02.982 19488-19488/com.example.daisymicrophone D/MicrophoneService: stopMicService: 
2020-05-09 00:13:02.984 19488-19488/com.example.daisymicrophone V/ActivityThread: SVC-Destroying service com.example.daisymicrophone.MicrophoneService@c083c8d
2020-05-09 00:13:02.993 19488-19488/com.example.daisymicrophone E/ActivityThread: Service com.example.daisymicrophone.MicrophoneService has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@2e1ecaf that was originally bound here
    android.app.ServiceConnectionLeaked: Service com.example.daisymicrophone.MicrophoneService has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@2e1ecaf that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1376)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1271)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1465)
        at android.app.ContextImpl.bindService(ContextImpl.java:1437)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:636)
        at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:287)
        at com.example.daisymicrophone.MicrophoneService.onStartCommand(MicrophoneService.java:66)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3394)
        at android.app.ActivityThread.-wrap21(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1632)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6259)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
还阮:

停止服务时,您应该销毁语音识别器。

@Override
public void onDestroy() {
    mSpeechRecognizer.destroy();
    super.onDestroy();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

android.app.ServiceConnectionLeaked:活动... MainActivity泄漏了最初绑定在此处的ServiceConnection ... MainActivity $ 1 @ e794142

如何修复“活动泄漏了最初绑定在此处的ServiceConnection net.openid.appauth.browser.CustomTabManager$1@41fb56d0”错误

服务泄漏了最初绑定在此处的ServiceConnection

泄漏了ServiceConnection android.speech.tts.TextToSpeech $ Connection

带有MediaController的Android MediaPlayer:LogCat错误“活动泄漏了最初在此处添加的窗口”

活动com.ui.MapViewer泄漏了最初在此处添加的窗口android.widget.PopupWindow

TTS泄漏了原本在此处绑定的服务连接

服务未注册:android.speech.tts.TextToSpeech

在服务器上为 GCP Speech to Text 生成 AccessToken 以在 Android/iOS 中使用

在Android中无法导入com.google.cloud.speech.v1.SpeechGrpc

Android Text to Speech 非谷歌引擎

Android Speech to Text Api Google-通知

活动泄漏了最初在此处添加的窗口(登录数据错误时)

Android:BoundService和泄漏的ServiceConnection

相对于时间控制 Android Text to Speech 的速率

Watson Speech Android SDK显示INSTALL_FAILED_NO_MATCHING_ABI

Android Text-To-Speech以编程方式用“字母”说粤语

适用于Android的Google Cloud Speech API

Throwable = 格式错误的 URL。基础:https://speech.googleapis.com/v1/,相对:speech:longrunningrecognize

chromecustomtab xamarin android MainActivity 泄露了原本绑定在这里的 ServiceConnection CustomTabsServiceConnectionImpl@43a61ad

Google Speech API:在此服务器上找不到请求的 URL

Google Speech Api v1无法正常工作吗?

您可以在此处使用Android SDK从地图上删除2D地标吗?

如何在android studio中从Text To Speech中获取所有支持的声音

Android Speech-To-Text:语音停止后保持活动状态的选项

Web Speech API无法在Android版Chrome中正确加载语音

有什么方法可以从Android小部件使用Google Speech API(默认免费版本)吗?

Google Cloud Speech API在Android中提供了TRANSIENT_FAILURE状态的UNAVAILABLE:频道

经过长时间的演讲,Android ACTION_RECOGNIZE_SPEECH意向永远不会完成