android语音识别服务,android – 语音识别作为后台服务

是否可以实现一项活动作为服务?我的活动是语音识别活动.我希望在应用程序的后台运行的活动不断检查语音,当用户说出命令时,它会识别它,然后执行该操作.我的问题是…有可能这样做,如果是这样,后台服务如何通知当前的活动或应用程序?有一个透明的帖子,但没有明确的答案…感谢任何输入或帮助.这是声音活动…从另一个stackoverflow文章:

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.speech.RecognitionListener;

import android.speech.RecognizerIntent;

import android.speech.SpeechRecognizer;

import android.widget.Button;

import android.widget.TextView;

import java.util.ArrayList;

import android.util.Log;

public class voiceRecognitionTest extends Activity implements OnClickListener

{

private TextView mText;

private SpeechRecognizer sr;

private static final String TAG = "MyStt3Activity";

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button speakButton = (Button) findViewById(R.id.btn_speak);

mText = (TextView) findViewById(R.id.textView1);

speakButton.setOnClickListener(this);

sr = SpeechRecognizer.createSpeechRecognizer(this);

sr.setRecognitionListener(new listener());

}

class listener implements RecognitionListener

{

public void onReadyForSpeech(Bundle params)

{

Log.d(TAG, "onReadyForSpeech");

}

public void onBeginningOfSpeech()

{

Log.d(TAG, "onBeginningOfSpeech");

}

public void onRmsChanged(float rmsdB)

{

Log.d(TAG, "onRmsChanged");

}

public void onBufferReceived(byte[] buffer)

{

Log.d(TAG, "onBufferReceived");

}

public void onEndOfSpeech()

{

Log.d(TAG, "onEndofSpeech");

}

public void onError(int error)

{

Log.d(TAG, "error " + error);

mText.setText("error " + error);

}

public void onResults(Bundle results)

{

String str = new String();

Log.d(TAG, "onResults " + results);

ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

for (int i = 0; i < data.size(); i++)

{

Log.d(TAG, "result " + data.get(i));

str += data.get(i);

}

mText.setText("results: "+String.valueOf(data.size()));

}

public void onPartialResults(Bundle partialResults)

{

Log.d(TAG, "onPartialResults");

}

public void onEvent(int eventType, Bundle params)

{

Log.d(TAG, "onEvent " + eventType);

}

}

public void onClick(View v) {

if (v.getId() == R.id.btn_speak)

{

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);

sr.startListening(intent);

Log.i("111111","11111111");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值