Search code examples
javaandroidmultithreadinggoogle-glassgoogle-gdk

How do I start a Thread as soon as my Google Glass application starts?


So I've built a small Google Glass application using the GDK which basically fakes a push notification by opening a Thread in the background and polling a RESTful webservice every few seconds.

Currently when I open the application it loads and runs the service but the onStart() and onCreate() for the activity (where the Thread is) doesn't get called until the user interacts with the app by opening the menu.

I've never done Android development before so I don't know why my activity might not be loading as soon as the user starts the app.

Here's an overview of some of the code.

public class MainActivity extends Activity {


@Override
protected void onStart()
{
    super.onStart();
    startThread();
}

Then the actual thread (I know it's terrible it's a prototype :-P)

public void startThread()
{
    thread = new Thread()
    {
        @Override
        public void run() {
            try {
                while(true) {

                    sleep(5000);
                    boolean tmp = poll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
}

And the Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testpack"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="15" />

<application
    android:allowBackup="true"
    android:icon="@drawable/logo_box"
    android:label="Test">
    <uses-library
        android:name="com.google.android.glass"
        android:required="true" />
    <activity
        android:name="com.testpack.MainActivity"
        android:theme="@style/MenuTheme"
        android:enabled="true"/>
    <service
        android:name="com.testpack.MainService"
        android:label="@string/app_name"
        android:icon="@drawable/logo_box"
        android:enabled="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/hello_show" />
    </service>
</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


Solution

  • Unlike most regular android apps, Glassware typically launches a Service (which runs in the background) rather then an activity (which is a foreground task). Is is because a lot of the time you will want to interact with the timeline (i.e. add live cards etc) rather than take over the whole screen with an activity (or an immersion in Glass speak).

    In your manifest you are launching MainService with a voice command, but your code is in MainActivity.

    If you put your code to start the thread in the onStartCommand method of MainService that should start as soon as the app is launched from the menu/voice command.

    I hope that makes things clearer.