3

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>

DaveOrrock
  • 63
  • 1
  • 1
  • 8
  • So you want to start a thread when your app starts, but are saying this code merely starts a thread when the user opens the app. Is there a typo in here? sounds like exactly what you want. – Sean Owen May 08 '14 at 08:58
  • @SeanOwen No, he's saying the activity doesn't start until the user INTERACTS with the app. After opening it, they still have to do something before everything gets fired up. Which seems really strange to me, because your oncreate() should have just been called when the app opened. – SvenT23 May 08 '14 at 09:01
  • I included a breakpoint on the onStart and onCreate functions but neither of them are called at all until the menu is opened – DaveOrrock May 08 '14 at 09:07
  • But your service is starting immediately? I think there's something wrong in how your manifest handles your startup. Try adding `` and `` to an intent-filter for your MainActivity – SvenT23 May 08 '14 at 09:24
  • Unfortunately not, I just added it like this: `` `` `` `` `` – DaveOrrock May 08 '14 at 09:38
  • Sorry that wasn't too clear. I've added the action and category to the intent filter for the activity, but it hasn't had any effect. The MainActivity hits the OnCreate, OnStart, onResume in that order once the menu has been opened. Is it possible that another Activity is calling it on onCreateOptionsMenu and starting it up then? – DaveOrrock May 08 '14 at 10:52
  • I believe so, yes. It seems like only the service is started but not your mainactivity, and I have no idea why ... – SvenT23 May 08 '14 at 11:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52300/discussion-between-daveorrock-and-svent23) – DaveOrrock May 08 '14 at 12:20
  • I've worked out (with the help of @SvenT23) that the activity doesn't start until the Service is interacted with because the Intent directs it straight to the service. So I assume the App starts a default activity to hold it, then when the Activity is called (Somehow... I think it might have something to do with IBinders) it just layers the new activity over the old one. – DaveOrrock May 08 '14 at 15:03

1 Answers1

2

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.

Ben
  • 1,767
  • 16
  • 32