기본적으로 안드로이드에 있는 음성인식을 사용하는것 startActivityForResult 를 활용하게 된다.
startActivityForResult 이 뭐냐... 자세한건 다음을 참고 하시면됩니다.
Starting Activities and Getting Results
The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.
Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent,int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int,int, Intent) method.
When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.
If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.
publicclassMyActivityextendsActivity{ ... staticfinalint PICK_CONTACT_REQUEST =0; protectedboolean onKeyDown(int keyCode,KeyEventevent){ if(keyCode ==KeyEvent.KEYCODE_DPAD_CENTER){ // When the user center presses, let them pick a contact.
startActivityForResult( newIntent(Intent.ACTION_PICK, newUri("content://contacts")),
PICK_CONTACT_REQUEST); returntrue; } returnfalse; } protectedvoid onActivityResult(int requestCode,int resultCode, Intent data){ if(requestCode == PICK_CONTACT_REQUEST){ if(resultCode == RESULT_OK){ // A contact was picked. Here we will just display it // to the user.
startActivity(newIntent(Intent.ACTION_VIEW, data));}} } }
그냥 간단히 말해서 인텐트에 정보를 실어 다른 액티비티로 보내주고 되돌아오는 것을 onActivityResult 메소드로 받아서 처리를 해주는것입니다. 이런식으로 많은곳에서 정보를 주고 받고 하는 일을 많이 하게 됩니다. 결과만 받아오는것을 체크해주는것은 requestCode == VOICE_CODE 이부분이지만 뒤에 resultCode == RESULT_OK가
없게 된다면 중간에 취소되거나 제대로된 결과가 안나올 경우 널포인트 에러를 반환해버립니다. 결과적으로 넣어줍시다.
댓글 영역