1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
returnedText = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
progressBar.setVisibility(View.INVISIBLE);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});
ttsManager = new TTSManager();
ttsManager.init(this);
Pause = (Button) findViewById(R.id.pause);
Stop = (Button) findViewById(R.id.stop);
Resume = (Button) findViewById(R.id.resume);
Pause.setVisibility(View.INVISIBLE);
Resume.setVisibility(View.INVISIBLE);
Stop.setVisibility(View.INVISIBLE);
// -------------------------------------
//----------------------------------------
handler = new android.os.Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE: // if receive massage
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
Log.e("TAG", sbprint);
if(sbprint.contains("ello")){
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
toggleButton.setChecked(true);
}
}
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
break;
}
};
};
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
//--------------------------------------
}
我試圖激活語音像我按下toogleButton,我拷入從toogleButton代碼的處理程序。 一切都好,我的意思是如果我點擊toogleButton它的工作原理,但是當我在處理程序中收回了支票「ello」的消息時,speechRecognizer開始但很快關閉,每當我發送該消息時都會發生這種情況。語音識別在haddler似乎再次打開該活動每次
另外,它似乎是一個新的活動,當我觸發與該消息處理程序,但toogleButton工作相同的功能。
任何ideea我該如何做這項工作?我需要toogleButton的功能,但是在處理程序中也是這樣,所以我可以通過消息或處理程序內的某個東西來觸發speechRecognition。
我會檢查這是5分鐘:d –
是它的工作原理,但還是打開了一個新的活動或如果我按回來的東西關閉.. –
在處理程序,它啓動一個新的活動,並與tooglebutton是好的 –