2017-07-26 201 views
-2

我正在使用Google API進行語音識別,但希望在應用程序從所有頁面開始識別開始時使其無間斷連續。Android中的語音識別

這是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    edtUserName = (EditText) findViewById(R.id.edtUserName); 
    edtCode = (EditText) findViewById(R.id.edtCode); 
    btnLogin = (Button) findViewById(R.id.btnLogin); 
    tx =(TextView)findViewById(R.id.tx); 
    speechButton = (Button) findViewById(R.id.speechButton); 
    btnLogin.setOnClickListener(new View.OnClickListener(){ 
     public void onClick (View v){ 
      Login(); 
     } 
    }); 
    speechButton.setOnClickListener(new View.OnClickListener(){ 
     public void onClick (View v){ 
      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
      intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to text"); 
      startActivityForResult(intent, 1); 
     } 
    }); 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == 1 && resultCode == RESULT_OK) { 
     ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
     switch (matches.get(0).toString()) { 
      case "prénom": 
       edtUserName.requestFocus(); 
       break; 
      case "code": 
       edtCode.requestFocus(); 
       break; 
      case "login": 
       Login(); 
       break; 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

} 

回答

0

簡單地講識別碼外的onClick(你並不真的需要一個事件)時,會的onCreate你所需要的。

+0

但它不是連續的 –

+0

@MarzoukAla你連續的意思是什麼? –

+0

我的意思是一直運行的語音識別服務 –