2017-07-26 165 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

我的意思是一直运行的语音识别服务 –