11package com .example .javabutton ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
36import android .hardware .Sensor ;
47import android .hardware .SensorEvent ;
58import android .hardware .SensorEventListener ;
69import android .hardware .SensorManager ;
710import android .os .Build ;
811import android .os .Bundle ;
912import android .preference .PreferenceManager ;
13+ import android .speech .RecognizerIntent ;
1014import android .annotation .TargetApi ;
1115import android .app .Activity ;
1216import android .content .Intent ;
1317import android .content .SharedPreferences ;
1418import android .content .SharedPreferences .OnSharedPreferenceChangeListener ;
19+ import android .content .pm .ResolveInfo ;
1520import android .view .GestureDetector ;
1621import android .view .KeyEvent ;
1722import android .view .Menu ;
1823import android .view .MenuItem ;
1924import android .view .MotionEvent ;
2025import android .view .View ;
2126import android .widget .ShareActionProvider ;
27+ import android .widget .Toast ;
2228//import android.widget.Toast;
2329import android .media .AudioManager ;
2430import android .media .SoundPool ;
@@ -109,14 +115,29 @@ protected void onDestroy() {
109115 super .onDestroy ();
110116 javaPool .release ();
111117 }
112-
118+ private boolean isSpeechRecognizerAvailable () {
119+ try {
120+ Intent recognitionIntent =new Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH );
121+ List <ResolveInfo > activities =getPackageManager ().queryIntentActivities (recognitionIntent , 0 );
122+ if (activities .size ()>0 ) {
123+ return true ;
124+ }
125+ } catch (Exception e ) {
126+ return false ;
127+ }
128+ return false ;
129+ }
113130 @ Override
114131 public boolean onCreateOptionsMenu (Menu menu ) {
115132 // Inflate the menu; this adds items to the action bar if it is present.
116133 getMenuInflater ().inflate (R .menu .main , menu );
117134 if (Build .VERSION .SDK_INT >=14 ) {
118135 setupShareAction (menu );
119136 }
137+ if (!isSpeechRecognizerAvailable ()) {
138+ MenuItem micAction =menu .findItem (R .id .action_mic );
139+ micAction .setVisible (false );
140+ }
120141 return true ;
121142 }
122143
@@ -141,12 +162,15 @@ public boolean onMenuItemSelected(int featureId, MenuItem item) {
141162 if (itemId ==R .id .action_settings ) {
142163 Intent intent =new Intent (this ,SettingsActivity .class );
143164 startActivity (intent );
144- return true ;
145- }
146- return super .onMenuItemSelected (featureId ,item );
147- }
148-
149- public void JavaButtonClick (View v ) {
165+ } else if (itemId ==R .id .action_mic ) {
166+ startSpeechRecognition ();
167+ } else {
168+ return super .onMenuItemSelected (featureId ,item );
169+ }
170+ return true ;
171+ }
172+
173+ public void JavaButtonClick (View v ) {
150174 playJava (1.f );
151175 }
152176
@@ -217,4 +241,30 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
217241 }
218242 return super .onKeyDown (keyCode , event );
219243 }
244+ private static final int VOICE_RECOGNITION_REQUEST_CODE =0x06a103 ;
245+ private void startSpeechRecognition () {
246+ Intent intent =new Intent (RecognizerIntent .ACTION_RECOGNIZE_SPEECH );
247+ intent .putExtra (RecognizerIntent .EXTRA_LANGUAGE_MODEL , RecognizerIntent .LANGUAGE_MODEL_WEB_SEARCH );
248+ intent .putExtra (RecognizerIntent .EXTRA_MAX_RESULTS , 1 );
249+ startActivityForResult (intent ,VOICE_RECOGNITION_REQUEST_CODE );
250+ }
251+ @ Override
252+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
253+ if (requestCode ==VOICE_RECOGNITION_REQUEST_CODE && resultCode ==RESULT_OK ) {
254+ ArrayList <String > matches = data .getStringArrayListExtra (RecognizerIntent .EXTRA_RESULTS );
255+ if (matches .size ()>0 ) {
256+ try {
257+ String javaStr =matches .get (0 ).toLowerCase (java .util .Locale .US );
258+ if ("java" .equals (javaStr )) {
259+ playJava (1.f );
260+ } else {
261+ Toast .makeText (this , javaStr , Toast .LENGTH_SHORT ).show ();
262+ }
263+ } catch (NullPointerException e ) {
264+ }
265+ }
266+ return ;
267+ }
268+ super .onActivityResult (requestCode , resultCode , data );
269+ }
220270}
0 commit comments