how to be ensure the google speech api return values - java

Alexa [Bot]

New Member
i try to write a code for android app that take a string that returned from google speech api and use it to send to other object "levenshte.testLevenshteindistance", within same thread.the problem i can't make it to be synchronize work!i mean my code call the object before the google speech api return value "depended on internet speed"?!\[code\]ublic class MainActivity extends Activity {public ListView sList;ArrayList<String> names;int sfound;mp3Player mp3Player; Levenshteindistance levenshte;public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); voiceinputbuttons(); starVoiceRecognation();}private void starVoiceRecognation() { Thread timer = new Thread(){ public void run(){ new VoiceRecognizer().execute(); //names is an ArrayList returnd by google speech api sfound=levenshte.testLevenshteindistance(names.get(0).toString()); } };timer.start(); }public void voiceinputbuttons() { sList = (ListView) findViewById(R.id.list); //default value if create mp3player before set sName or rName or actionNumbe names=null; levenshte = new Levenshteindistance();}public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN")); }public void startVoiceRecognitionActivity() {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 recognition demo"); try { startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); } catch (ActivityNotFoundException e) { }}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { // i want the timer thread wait until assign a value to this name variable names = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); if (names.contains("information")) { informationMenu(); } super.onActivityResult(requestCode, resultCode, data); }}private class VoiceRecognizer extends AsyncTask<String, Integer, String> {@Override protected String doInBackground(String... url1) { startVoiceRecognitionActivity(); return null; }}}\[/code\]i want be sure that the names not null when call the levenshte.testLevenshteindistance(names.get(0).toString());
 
Top