服务线程在启动时死亡

时间:2014-09-04 22:22:06

标签: java android multithreading

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

当我在我的线程的run函数中时,线程在绑定服务后立即死亡。你们中的任何人都可以指导我正确的方向发生这种情况吗?

以下是从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;
        }    
    }
}

这是我的MainActivity

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");
    }

这是我的Manifest(这是Google玻璃项目)

<?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>

由于

1 个答案:

答案 0 :(得分:0)

当你说:

  

绑定服务后线程立即死亡

我希望您的日志中会出现堆栈跟踪异常。这可能会对实际发生的事情有所了解。


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

public abstract class SimpleVoiceService extends Service

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


另外,为什么要在线程中绑定服务?而不是:

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);