2016-05-07 2 views
3

Je veux savoir s'il existe un moyen de modifier et de personnaliser le style de la boîte de dialogue Reconnaissance vocale dans mon application?Personnaliser la boîte de dialogue Reconnaissance vocale

i.e.: modifier le logo Google ou les textes.
enter image description here

J'utilise ce code, est-ce complet? public void onReadyForSpeech (Paramètres de l'ensemble) { proccessTXT.setText ("Parlez maintenant!"); }

@Override 
public void onBeginningOfSpeech() { 

} 

@Override 
public void onRmsChanged(float rmsdB) { 

} 

@Override 
public void onBufferReceived(byte[] buffer) { 

} 

@Override 
public void onEndOfSpeech() { 
    proccessTXT.setText("Waiting"); 
} 

@Override 
public void onError(int error) { 
    proccessTXT.setText(R.string.toast_disconnect); 
} 

@Override 
public void onResults(Bundle results) { 
     match_text_dialog = new Dialog(MainActivity.this); 
     match_text_dialog.setContentView(R.layout.dialog_maches_flag); 
     match_text_dialog.setTitle(R.string.selection_list); 
     textlist = (ListView) match_text_dialog.findViewById(R.id.list); 
     matches_text = getIntent().getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, matches_text); 
     textlist.setAdapter(adapter); 
     textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 

      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 

       type_texts = matches_text.get(position); 

       speech_text.append(type_texts + " "); 

       match_text_dialog.hide(); 

       // speech_text.setCustomSelectionActionModeCallback(new SelectText()); 
       actionMode = MainActivity.this.startActionMode(new SelectText()); 
      } 
     }); 
     match_text_dialog.show();// show dialog 

} 

@Override 
public void onPartialResults(Bundle partialResults) { 

} 

c'est LogCat:

FATAL EXCEPTION: main 
                       Process: PACKAGE, PID: 25645 
                       java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
                        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 
                        at android.widget.ListView.setAdapter(ListView.java:502) 
                        at PACKAGE.MainActivity.onResults(MainActivity.java:245) 
                        at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:456) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:145) 
                        at android.app.ActivityThread.main(ActivityThread.java:6837) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:372) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

Répondre

5

En option, vous pouvez lancer reconnaissance vocale avec ACTION_RECOGNIZE_SPEECH (sans interface utilisateur) et de montrer tout dialogue dont vous avez besoin

SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 
speechRecognizer.setRecognitionListener(this); 
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); 
speechRecognizer.startListening(speechIntent); 

Ici, vous avez besoin pour implémenter RecognitionListener afin que vous puissiez afficher la boîte de dialogue dans le rappel public void onReadyForSpeech(Bundle params). Rejeter dans public void onResults(Bundle results) ou public void onError(int error)

+0

thaks. je l'utilise. Est-ce complet? parce que comme je l'exécute, il montre la méthode d'erreur. –

+1

@MinaDahesh quelle erreur? –

+0

N'oubliez pas d'ajouter l'autorisation RECORD_AUDIO à votre manifeste et, si vous utilisez un périphérique guimauve, [request permission] (http://developer.android.com/training/permissions/requesting.html) de l'utilisateur . – Michiyo