Я пытаюсь создать приложение android, которое планирует задание распознавания речи каждые 15 минут, используя планировщик заданий, так как он мне нужен для работы в фоновом режиме. Мне было интересно, как я мог это сделать. В настоящее время я пытаюсь запланировать задание с помощью планировщика заданий и запускаю распознаватель речи из службы, однако постоянно получаю эту ошибку:
ActivityThread: служба утечка ServiceConnection android .speech. SpeechRecognizer$Connection@3adb7f6, который был изначально привязан здесь
Если это не лучший способ запустить распознаватель речи из фона, пожалуйста, дайте мне знать, какой путь лучше.
Код, который у меня есть пока что все выглядит следующим образом:
MainActivity. java
package com.example.microphonebackgroundtester;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scheduleJob();
}
});
checkPermission();
}
public void scheduleJob() {
ComponentName componentName = new ComponentName(this, JobServiceExample.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.setPeriodic(15 * 60 * 1000)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = jobScheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
} else {
Log.d(TAG, "Job scheduling failed");
}
}
public void cancelJob() {
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(123);
Log.d(TAG, "Job cancelled");
}
private void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivity(intent);
finish();
}
}
}
}
JobServiceExample. java
package com.example.microphonebackgroundtester;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.EditText;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Locale;
public class JobServiceExample extends JobService {
private static final String TAG = "ExampleJobService";
private boolean jobCancelled = false;
protected static SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
Context c;
@Override
public boolean onStartJob(final JobParameters params) {
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(TAG, " " + mSpeechRecognizer.isRecognitionAvailable(this));
if (mSpeechRecognizer.isRecognitionAvailable(this))
Log.d(TAG, "onBeginingOfSpeech");
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
//do nothing, just let it tick
}
public void onFinish() {
Log.d(TAG, "Listening stopped");
mSpeechRecognizer.stopListening();
Log.d(TAG, "Job finished");
jobFinished(params, false);
} }.start();
return true;
}
@Override
public void onCreate() {
super.onCreate();
c= getApplicationContext();
}
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(TAG,"ERROR");
}
@Override
public void onResults(Bundle resultsBundle) {
Log.d(TAG, "RESULTS: " + resultsBundle.getStringArrayList(mSpeechRecognizer.RESULTS_RECOGNITION).get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "Job cancelled before completion");
jobCancelled = true;
return true;
}
}
ErrorLog
D/MainActivity: Job scheduled
V/ActivityThread: SVC-Creating service CreateServiceData{token=android.os.BinderProxy@a4cb6b8 className=com.example.microphonebackgroundtester.JobServiceExample packageName=com.example.microphonebackgroundtester intent=null}
D/ExampleJobService: true
D/ExampleJobService: onBeginingOfSpeech
D/onReady: service
D/ExampleJobService: ERROR
D/ExampleJobService: Listening stopped
D/ExampleJobService: Job finished
D/ExampleJobService: ERROR
V/ActivityThread: SVC-Destroying service com.example.microphonebackgroundtester.JobServiceExample@ca60c91
E/ActivityThread: Service com.example.microphonebackgroundtester.JobServiceExample has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@3adb7f6 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.example.microphonebackgroundtester.JobServiceExample has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@3adb7f6 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.microphonebackgroundtester.JobServiceExample.onStartJob(JobServiceExample.java:46)
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:143)
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)