<service android:name="com.chucknorris.JokeService" android:icon="@drawable/icon" android:label="@string/app_name" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.android.glass.action.VOICE_TRIGGER"/> </intent-filter> <meta-data android:name="com.google.android.glass.VoiceTrigger" android:resource="@xml/voice_trigger_start"/> </service>
<?xml version="1.0" encoding="utf-8"?> <trigger keyword="@string/chuck_norris_joke" />
<string name="chuck_norris_joke">say Chuck Norris joke</string>
<activity android:name="com.google.android.glass.sample.waveform.WaveformActivity" > <intent-filter> <action android:name="com.google.android.glass.action.VOICE_TRIGGER" /> </intent-filter> <meta-data android:name="com.google.android.glass.VoiceTrigger" android:resource="@xml/voice_trigger_start" /> </activity>
<uses-permission android:name="android.permission.INTERNET"/>
HttpClient client = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(); try { // construct a URI object getRequest.setURI(new URI(urls[0])); } catch (URISyntaxException e) { Log.e("URISyntaxException", e.toString()); } // buffer reader to read the response BufferedReader in = null; // the service response HttpResponse response = null; try { // execute the request response = client.execute(getRequest); } catch (ClientProtocolException e) { Log.e("ClientProtocolException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } try { in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IllegalStateException e) { Log.e("IllegalStateException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } StringBuffer buff = new StringBuffer(""); String line = ""; try { while ((line = in.readLine()) != null) { buff.append(line); } } catch (IOException e) { Log.e("IO exception", e.toString()); return e.getMessage(); } try { in.close(); } catch (IOException e) { Log.e("IO exception", e.toString()); }
String joke = ""; try { JSONObject jObject = new JSONObject(buff.toString()); joke = jObject.getJSONObject("value").getString("joke"); } catch (JSONException e) { Log.e("JSON exception", e.toString()); }
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener(){ @Override public void onInit(int i) { //TODO - } });
protected void onPostExecute(String joke) { if (exception != null) { return; } readOutLoud(joke); }
private void readOutLoud(String joke) { tts.speak(joke, TextToSpeech.QUEUE_FLUSH, null); }
private void publishJokeCard(String joke) { Card card = new Card(ctx); card.setText(joke); card.addImage(R.drawable.chuck); TimelineManager.from(ctx).insert(card); }
Source: https://habr.com/ru/post/207716/
All Articles