如果我使用这样的语音识别
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
做一些设定...... 并制作
startActivityForResult(intent, SystemData.VOICE_RECOGNITION_REQUEST_CODE);
有没有办法使用某种意图或以任何其他方式停止服务(如销毁)?
由于 杆
答案 0 :(得分:0)
我找到了如何停止语音识别服务。 我创建了一个SpeechRecognizer类型的通用变量。
把它放在课程的开头,如:
私人SpeechRecognizer speech = null;
然后在语音初始化中,我将它连接到一个监听器并在意图中使用它:
speech = SpeechRecognizer.createSpeechRecognizer(getActivity());
speech.setRecognitionListener(this);
Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
然后在本课程的任何部分,如果我想激活演讲,我就用这个:
speech.startListening(recognizerIntent);
并阻止它我这样做:
speech.stopListening();
所以听众是言语活动的关键。
答案 1 :(得分:-1)
首先将两个按钮[buttonStart和buttonStop]添加到您的活动xml然后再添加 在包含您问题中的代码的活动中添加: @覆盖 public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
MyService.java
mport android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
player.setLooping(false); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}