需求:Service后台离线语音识别,将识别结果回调给Activity。

YyService.java

package cn.ilell.ihome.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import cn.ilell.ihome.utils.PocketSphinxUtil;
import edu.cmu.pocketsphinx.demo.RecognitionListener;
/**
 * Created by xubowen on 2017/3/5.
 */
public class YyService extends Service implements RecognitionListener {
    public static final String TAG = "YyService";
    private Context context;
    private final IBinder binder = new MyBinder();
    @Override
    public void onCreate() {
        super.onCreate();
        PocketSphinxUtil.get(this).setListener(this).start();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
        PocketSphinxUtil.get(this).stop();
    }
    public interface YyResultListener {
        public void YyR(String str);
    }
    private YyResultListener yyResultListener=null;                                     ///
    public void setYyResultListener(YyResultListener yyListener) {                   ///
        this.yyResultListener = yyListener;                                       ///
    }
    @Override
    public void onPartialResults(String b) {
        yyResultListener.YyR(b);
    }
    @Override
    public void onResults(String b) {

    }
    @Override
    public void onError(int err) {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    public class MyBinder extends Binder {
        public YyService getService(){
            return YyService.this;
        }
    }
}
MainActivity.java

 private YyService serviceBinder;

    //离线语音服务
    protected ServiceConnection mConnection = new ServiceConnection(){
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Called when the connection is made.
            serviceBinder = ((YyService.MyBinder)service).getService();
            serviceBinder.setYyResultListener(new YyService.YyResultListener() {
                @Override
                public void YyR(String s) {
                    Toast.makeText(mContext,"哈哈哈"+s,Toast.LENGTH_LONG).show();
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("content", s);
                    params.put("homeid", BaseData.home_id);
                    params.put("userid", BaseData.user_id);
                    String result = HttpXmlClient.post(url, params);
                    if (result.equals("success")){
                        Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        public void onServiceDisconnected(ComponentName className) {
            // Received when the service unexpectedly disconnects.
            serviceBinder = null;
        }
    };
 //绑定离线语音服务
        Intent bindIntent = new Intent(this, YyService.class);
        bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
        startService(bindIntent);//防止Activity finish后Service也被kill
@Override
    public void onDestroy() {
        unbindService(mConnection);
        super.onDestroy();
    }

在AndroidManifest.xml中注册Service




Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐