java - 服务线程在启动时终止

标签 java android multithreading

我正在尝试在 Android 应用程序后台运行连续语音识别服务。我希望服务将处理后的文本发送回主线程以用于 UI 目的。

当我在线程的运行函数中时,线程在绑定(bind)服务后立即终止。你们中的任何人都可以指导我正确的方向来解释为什么会发生这种情况吗?

这是从here借用的服务代码

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.example.quadcontrol;

//import android.annotation.SdkConstant;
//import android.annotation.SdkConstant.SdkConstantType;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.speech.RecognitionListener;
import android.speech.RecognitionService;
import android.speech.SpeechRecognizer;
import android.util.Log;



public abstract class SimpleVoiceService extends Service 
{   
    /**
     * The {@link Intent} that must be declared as handled by the service.
     */
    //@SdkConstant(SdkConstantType.SERVICE_ACTION)
    public static final String SERVICE_INTERFACE = "android.speech.RecognitionService";

    /**
     * Name under which a RecognitionService component publishes information about itself.
     * This meta-data should reference an XML resource containing a
     * <code>&lt;{@link android.R.styleable#RecognitionService recognition-service}&gt;</code> tag.
     */
    public static final String SERVICE_META_DATA = "android.speech";

    /** Log messages identifier */
    private static final String TAG = "SimpleVoiceService";

    /** Debugging flag */
    private static final boolean DBG = false;

    /** Binder of the recognition service */
    private RecognitionServiceBinder mBinder = new RecognitionServiceBinder(this);

    /**
     * The current callback of an application that invoked the
     * {@link RecognitionService#onStartListening(Intent, Callback)} method
     */
    private Callback mCurrentCallback = null;

    private static final int MSG_START_LISTENING = 1;

    private static final int MSG_STOP_LISTENING = 2;

    private static final int MSG_CANCEL = 3;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_START_LISTENING:
                    StartListeningArgs args = (StartListeningArgs) msg.obj;
                    dispatchStartListening(args.mIntent, args.mListener);
                    break;
                case MSG_STOP_LISTENING:
                    dispatchStopListening((RecognitionListener) msg.obj);
                    break;
                case MSG_CANCEL:
                    dispatchCancel((RecognitionListener) msg.obj);
            }
        }
    };

    private void dispatchStartListening(Intent intent, android.speech.RecognitionService.Callback mListener) {
        if (mCurrentCallback == null) {
            //if (DBG) Log.d(TAG, "created new mCurrentCallback, listener = " + mListener.asBinder());
            mCurrentCallback = new Callback(mListener);
            SimpleVoiceService.this.onStartListening(intent, mCurrentCallback);
        } else {
            try {
                mListener.error(SpeechRecognizer.ERROR_RECOGNIZER_BUSY);
            } catch (RemoteException e) {
                Log.d(TAG, "onError call from startListening failed");
            }
            Log.i(TAG, "concurrent startListening received - ignoring this call");
        }
    }

    private void dispatchStopListening(RecognitionListener listener) {
        if (mCurrentCallback == null) {
            listener.onError(SpeechRecognizer.ERROR_CLIENT);
            Log.w(TAG, "stopListening called with no preceding startListening - ignoring");
        } else if (mCurrentCallback.mListener != listener) {
            listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY);
            Log.w(TAG, "stopListening called by other caller than startListening - ignoring");
        } else { // the correct state
            SimpleVoiceService.this.onStopListening(mCurrentCallback);
        }
    }

    private void dispatchCancel(RecognitionListener listener) {
        if (mCurrentCallback == null) {
            if (DBG) Log.d(TAG, "cancel called with no preceding startListening - ignoring");
        } else if (mCurrentCallback.mListener != listener) {
            Log.w(TAG, "cancel called by client who did not call startListening - ignoring");
        } else { // the correct state
            SimpleVoiceService.this.onCancel(mCurrentCallback);
            mCurrentCallback = null;
            if (DBG) Log.d(TAG, "canceling - setting mCurrentCallback to null");
        }
    }

    private class StartListeningArgs {
        public final Intent mIntent;

        public final android.speech.RecognitionService.Callback mListener;

        public StartListeningArgs(Intent intent, android.speech.RecognitionService.Callback listener) {
            this.mIntent = intent;
            this.mListener = listener;
        }
    }

    /**
     * Checks whether the caller has sufficient permissions
     * 
     * @param listener to send the error message to in case of error
     * @return {@code true} if the caller has enough permissions, {@code false} otherwise
     */
    private boolean checkPermissions(android.speech.RecognitionService.Callback listener) {
        if (DBG) Log.d(TAG, "checkPermissions");
        if (SimpleVoiceService.this.checkCallingOrSelfPermission(android.Manifest.permission.
                RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        Log.e(TAG, "call for recognition service without RECORD_AUDIO permissions");
        try {
            listener.error(SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Notifies the service that it should start listening for speech.
     * 
     * @param recognizerIntent contains parameters for the recognition to be performed. The intent
     *        may also contain optional extras, see {@link RecognizerIntent}. If these values are
     *        not set explicitly, default values should be used by the recognizer.
     * @param listener that will receive the service's callbacks
     */
    protected abstract void onStartListening(Intent recognizerIntent, Callback listener);

    /**
     * Notifies the service that it should cancel the speech recognition.
     */
    protected abstract void onCancel(Callback listener);

    /**
     * Notifies the service that it should stop listening for speech. Speech captured so far should
     * be recognized as if the user had stopped speaking at this point. This method is only called
     * if the application calls it explicitly.
     */
    protected abstract void onStopListening(Callback listener);

    @Override
    public final IBinder onBind(final Intent intent) {
        if (DBG) Log.d(TAG, "onBind, intent=" + intent);
        return (IBinder) mBinder;
    }

    @Override
    public void onDestroy() {
        if (DBG) Log.d(TAG, "onDestroy");
        mCurrentCallback = null;
        mBinder.clearReference();
        super.onDestroy();
    }

    /**
     * This class receives callbacks from the speech recognition service and forwards them to the
     * user. An instance of this class is passed to the
     * {@link RecognitionService#onStartListening(Intent, Callback)} method. Recognizers may call
     * these methods on any thread.
     */
    public class Callback {
        private final android.speech.RecognitionService.Callback mListener;

        private Callback(android.speech.RecognitionService.Callback mListener2) {
            mListener = mListener2;
        }

        /**
         * The service should call this method when the user has started to speak.
         */
        public void beginningOfSpeech() throws RemoteException {
            if (DBG) Log.d(TAG, "beginningOfSpeech");
            mListener.beginningOfSpeech();
        }

        /**
         * The service should call this method when sound has been received. The purpose of this
         * function is to allow giving feedback to the user regarding the captured audio.
         * 
         * @param buffer a buffer containing a sequence of big-endian 16-bit integers representing a
         *        single channel audio stream. The sample rate is implementation dependent.
         */
        public void bufferReceived(byte[] buffer) throws RemoteException {
            mListener.bufferReceived(buffer);
        }

        /**
         * The service should call this method after the user stops speaking.
         */
        public void endOfSpeech() throws RemoteException {
            mListener.endOfSpeech();
        }

        /**
         * The service should call this method when a network or recognition error occurred.
         * 
         * @param error code is defined in {@link SpeechRecognizer}
         */
        public void error(int error) throws RemoteException {
            mCurrentCallback = null;
            mListener.error(error);
        }

        /**
         * The service should call this method when partial recognition results are available. This
         * method can be called at any time between {@link #beginningOfSpeech()} and
         * {@link #results(Bundle)} when partial results are ready. This method may be called zero,
         * one or multiple times for each call to {@link SpeechRecognizer#startListening(Intent)},
         * depending on the speech recognition service implementation.
         * 
         * @param partialResults the returned results. To retrieve the results in
         *        ArrayList&lt;String&gt; format use {@link Bundle#getStringArrayList(String)} with
         *        {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter
         */
        public void partialResults(Bundle partialResults) throws RemoteException {
            mListener.partialResults(partialResults);
        }

        /**
         * The service should call this method when the endpointer is ready for the user to start
         * speaking.
         * 
         * @param params parameters set by the recognition service. Reserved for future use.
         */
        public void readyForSpeech(Bundle params) throws RemoteException {
            mListener.readyForSpeech(params);
        }

        /**
         * The service should call this method when recognition results are ready.
         * 
         * @param results the recognition results. To retrieve the results in {@code
         *        ArrayList&lt;String&gt;} format use {@link Bundle#getStringArrayList(String)} with
         *        {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter
         */
        public void results(Bundle results) throws RemoteException {
            mCurrentCallback = null;
            mListener.results(results);
        }

        /**
         * The service should call this method when the sound level in the audio stream has changed.
         * There is no guarantee that this method will be called.
         * 
         * @param rmsdB the new RMS dB value
         */
        public void rmsChanged(float rmsdB) throws RemoteException {
            mListener.rmsChanged(rmsdB);
        }
    }

    /** Binder of the recognition service */
    private static class RecognitionServiceBinder extends RecognitionService {
        private SimpleVoiceService mInternalService;

        public RecognitionServiceBinder(SimpleVoiceService simpleVoiceService) {
            mInternalService = simpleVoiceService;
        }

        @Override
        protected void onStartListening(Intent recognizerIntent, Callback listener) {
            //if (DBG) Log.d(TAG, "startListening called by:" + listener.asBinder());
            if (mInternalService != null && mInternalService.checkPermissions(listener)) {
                mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler,
                        MSG_START_LISTENING, mInternalService.new StartListeningArgs(
                                recognizerIntent, listener)));
            }
        }

        @Override
        protected void onStopListening(Callback listener) {
            //if (DBG) Log.d(TAG, "stopListening called by:" + listener.asBinder());
            if (mInternalService != null && mInternalService.checkPermissions(listener)) {
                mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler,
                        MSG_STOP_LISTENING, listener));
            }
        }

        @Override
        protected void onCancel(Callback listener) {
            //if (DBG) Log.d(TAG, "cancel called by:" + listener.asBinder());
            if (mInternalService != null && mInternalService.checkPermissions(listener)) {
                mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler,
                        MSG_CANCEL, listener));
            }
        }

        public void clearReference() {
            mInternalService = null;
        }    
    }
}

这是我的主要 Activity

public class MainActivity extends Activity 
{
    private static final String TAG = "Activity";
    private TextView txtMsg;    // TextView that shows the number
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtMsg = (TextView)findViewById(R.id.lbl_info);
        final ServiceConnection serviceConnection = new ServiceConnection()
        {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onServiceDisconnected(ComponentName name) 
            {
                // TODO Auto-generated method stub

            }

        };

        Thread t = new Thread()
        {
            public void run()
            {
                //Start service
                getApplicationContext().bindService(
                            new Intent(getApplicationContext(), SimpleVoiceService.class), 
                            serviceConnection,
                            Context.BIND_AUTO_CREATE);

            }
        };

        t.start();

        //Create handler for receiving messages
        mHandler = new Handler(Looper.getMainLooper())
        {
            @Override
            public void handleMessage(Message inputMessage)
            {
                //switch(inputMessage.what)
                //{
                    //case 4:
                    //{
                        txtMsg.setText((String)inputMessage.obj);
                        //break;
                    //}             
                //}
            }
        };

        //mHandler.sendMessage(Message.obtain(mHandler, 1));
        txtMsg.setText("Service Started");
    }

这是我的 list (这是一个 Google glass 项目)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.quadcontrol"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />    

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:immersive="true" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:icon="@drawable/ic_launcher" >                      

             <intent-filter>                
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />               
            </intent-filter>

              <intent-filter>
                <action android:name="android.speech.action.RECOGNIZE_SPEECH" />
                <category android:name="android.intent.category.DEFAULT" />
              </intent-filter>

            <intent-filter>
                <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
            </intent-filter>
            <meta-data 
                android:name="com.google.angroid.glass.VoiceTrigger"
                android:resource="@xml/voice_trigger" />    
        </activity>

        <recognition-service
            android:name="WordService" 
            android:icon="@drawable/ic_launcher"
            android:label="@string/service_name">

            <intent-filter>
                <action android:name="android.speech.RecognitionService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </recognition-service>

    </application>
</manifest>

谢谢

最佳答案

当你说:

the thread dies immediately after binding the service

我预计您的日志中会出现堆栈跟踪异常。这可能会让我们更清楚地了解到底发生了什么。

<小时/>

仔细一看,你指定的服务类是抽象的......

public abstract class SimpleVoiceService extends Service

我现在没有 Android 开发环境来确认这一点,但我不确定这是否可以这样工作。删除 abstract 修饰符,或绑定(bind)到扩展此服务的另一个非抽象服务。

<小时/>

另外,为什么要绑定(bind)到线程中的服务?而不是这个:

Thread t = new Thread()
{
    public void run()
    {
        //Start service
        getApplicationContext().bindService(
                    new Intent(getApplicationContext(), SimpleVoiceService.class), 
                    serviceConnection,
                    Context.BIND_AUTO_CREATE);

    }
};
t.start();

为什么不简单地这样做:

getApplicationContext().bindService(
            new Intent(getApplicationContext(), SimpleVoiceService.class), 
            serviceConnection,
            Context.BIND_AUTO_CREATE);

关于java - 服务线程在启动时终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25675867/

相关文章:

ios - 通过自定义运算符在主线程上快速执行闭包

java - 从后台线程更均匀地分散负载

java - 如何从外部应用程序检索加载在 Tomcat 中的 Spring 上下文

Java Swing : setting variables inside actionlistener that were defined outside

java - 设置计时器 GWT 后更新相同标签

java - ScrollView 落后于 Android 中的按钮

java - 在控制台上打印并打印到文件中

Android 在RelativeLayout中填充左动画

android - 在 onDestroy() 之后 Activity 实例保留在内存中

c# - 线程启动期间的竞争条件?