Google Glass First Program

We already know what Google Glass is and what it can do, so we can start by a simple program to show one of its features.

This program will show the current time and will start on a voice command. So we will need an immersion type of application and a voice command to start that application.

Let's start with the application.

Requirements

  1. Download the Android 4.4.2 (API 19) -> Glass Development Kit Preview in the Android SDK Manager.



  2. Create a new project and set its minimum and target SDK to API 19 and set the compile target to Glass Development Kit preview. Now complete the process and you will have a basic structure of a Glass application.



  3. First of all to start the application we will need a Voice trigger that will show up in the home screen.

    Let's make it "show time".

    1. Create a string in res/values/strings.xml.
      1. <string name="glass_voice_trigger">show time</string>  
    2. Create a XML file res/xml/voice_show_time.xml.

    3. Add <trigger keyword="@string/glass_voice_trigger" /> to the voice_show_time.xml.

      This is the out voice trigger, in other words when we say "OK Glass" in the home screen then we will get this as an option to open our application.

      We need to include this in the menifest file before we can start using it.

  4. Create an activity named TimerActivity.java, we will modify this later. Now open the AndroidManifest.xml file and modify it withthe following.

     

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.    package="com.nav.mytime"  
    4.    android:versionCode="1"  
    5.    android:versionName="1.0" >  
    6.   
    7.    <uses-sdk  
    8.    android:minSdkVersion="19"  
    9.    android:targetSdkVersion="19" />  
    10.   
    11.    <uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />  
    12.   
    13.    <application  
    14.       android:allowBackup="true"  
    15.       android:icon="@drawable/ic_launcher"  
    16.       android:label="@string/app_name" >  
    17.       <activity  
    18.          android:name="com.nav.mytime.TimerActivity"  
    19.          android:label="@string/title_activity_timer" >  
    20.       <intent-filter>  
    21.          <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />  
    22.       </intent-filter>  
    23.       <meta-data  
    24.          android:name="com.google.android.glass.VoiceTrigger"  
    25.          android:resource="@xml/voice_show_time"  
    26.       />  
    27.    </activity>  
    28. </application>  
    29.   
    30. </manifest>  
    31. ------  

     

    1. Here we need to include a permission <uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" /> so that we can have that voice trigger in our application.

    2. Include the intent filter in our <activity> tag so that the glass knows that our application will open using a voice trigger.
      1. <intent-filter>  
      2.    <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />  
      3. </intent-filter>  
    3. Include the meta data for our voice trigger as in the following:
      1. <meta-data  
      2.    android:name="com.google.android.glass.VoiceTrigger"  
      3.    android:resource="@xml/voice_show_time"  
      4. />  
  5. Create activity_timer.xml in the res/layout folder with the following code:
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    2.    xmlns:tools="http://schemas.android.com/tools"    
    3.    android:layout_width="match_parent"    
    4.    android:layout_height="match_parent"    
    5.    tools:context="${packageName}.${activityClass}" >    
    6.     
    7.    <TextView    
    8.       android:id="@+id/timerTextView"    
    9.       android:layout_width="wrap_content"    
    10.       android:layout_height="wrap_content"    
    11.       android:text="@string/hello_world"    
    12.       android:layout_centerInParent="true"    
    13.       android:textAppearance="?android:attr/textAppearanceLarge"    
    14.    />    
    15.     
    16. </RelativeLayout>  
    Here we have a simple layout with a TextView here that will show the time we need to show.

  6. Now open the TimerActivity.java file and write this code:
    1. package com.nav.mytime;  
    2.   
    3. import java.util.Calendar;  
    4.   
    5. import com.google.android.glass.media.Sounds;  
    6. import com.google.android.glass.touchpad.Gesture;  
    7. import com.google.android.glass.touchpad.GestureDetector;  
    8.   
    9. import android.app.Activity;  
    10. import android.os.Bundle;  
    11. import android.os.Handler;  
    12. import android.text.format.DateFormat;  
    13. import android.view.Menu;  
    14. import android.view.MotionEvent;  
    15. import android.widget.TextView;  
    16.   
    17. public class TimerActivity extends Activity {  
    18.   
    19.    private TextView timerTextView;  
    20.    private Handler tickerHandler;  
    21.    private Runnable tickerRunnable;  
    22.      
    23.    /** Listener that displays the options menu when the touchpad is tapped. */  
    24.    private final GestureDetector.BaseListener mBaseListener = new GestureDetector.BaseListener() {  
    25.    @Override  
    26.       public boolean onGesture(Gesture gesture) {  
    27.       if (gesture == Gesture.TAP) {  
    28.          openOptionsMenu();  
    29.          return true;  
    30.       } else {  
    31.          return false;  
    32.       }  
    33.    }  
    34.    };  
    35.   
    36.    /** Gesture detector used to present the options menu. */  
    37.    private GestureDetector mGestureDetector;  
    38.   
    39.    @Override  
    40.    protected void onCreate(Bundle savedInstanceState) {  
    41.       super.onCreate(savedInstanceState);  
    42.       setContentView(R.layout.activity_timer);  
    43.       timerTextView = (TextView)findViewById(R.id.timerTextView);  
    44.       mGestureDetector = new GestureDetector(this).setBaseListener(mBaseListener);  
    45.    }  
    46.   
    47.    @Override  
    48.    public boolean onGenericMotionEvent(MotionEvent event) {  
    49.       return mGestureDetector.onMotionEvent(event);  
    50.    }  
    51.   
    52.    @Override  
    53.    protected void onPause() {  
    54.       // TODO Auto-generated method stub  
    55.       super.onPause();  
    56.       stopClock();  
    57.    }  
    58.   
    59.    @Override  
    60.    protected void onResume() {  
    61.       // TODO Auto-generated method stub  
    62.       super.onResume();  
    63.       startClock();  
    64.    }  
    65.   
    66.    @Override  
    67.    public boolean onCreateOptionsMenu(Menu menu) {  
    68.       // TODO Auto-generated method stub  
    69.       getMenuInflater().inflate(R.menu.timer_options, menu);  
    70.       return true;  
    71.    }  
    72.    /** Handler and Runnable which will run every one second and show the current time on textview*/  
    73.    private void startClock()  
    74.    {  
    75.       tickerHandler=new Handler();  
    76.       tickerRunnable = new Runnable()   
    77.       {  
    78.          public void run()   
    79.          {   
    80.             updated();   
    81.             tickerHandler.postDelayed(tickerRunnable, 1000);   
    82.          }  
    83.       };  
    84.       tickerRunnable.run();  
    85.    }  
    86.   
    87.    /**Stop the timer - called when the application is paused*/  
    88.    private void stopClock(){  
    89.       if (tickerHandler!=null) {  
    90.          tickerHandler.removeCallbacks(tickerRunnable);  
    91.       }  
    92.    }  
    93.   
    94.    /** We get the current time */  
    95.    private void updated(){  
    96.       timerTextView.setText(DateFormat.format("h:mm:ss aa", Calendar.getInstance().getTimeInMillis()).toString());  
    97.    }   
    98. }  

Now just connect the glass and run your application as in the following:



This is what you will get.

Up Next
    Ebook Download
    View all
    Learn
    View all