简体   繁体   English

我的Google Glass应用程序启动后如何启动线程?

[英]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. 因此,我使用GDK构建了一个小型Google Glass应用程序,它基本上通过在后台打开一个Thread并每隔几秒轮询一次RESTful Web服务来伪造推送通知。

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. 目前,当我打开应用程序时,它会加载并运行服务,但是在用户通过打开菜单与应用程序交互之前,不会调用活动的onStart()和onCreate()(Thread所在的位置)。

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. 我以前从未做过Android开发,所以我不知道为什么我的活动可能会在用户启动应用程序后立即加载。

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) 那么实际的线程(我知道它很可怕它是原型:-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 和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>

Unlike most regular android apps, Glassware typically launches a Service (which runs in the background) rather then an activity (which is a foreground task). 与大多数常规Android应用程序不同,Glassware通常会启动一个服务(在后台运行)而不是一个活动(这是一个前台任务)。 Is is because a lot of the time you will want to interact with the timeline (ie add live cards etc) rather than take over the whole screen with an activity (or an immersion in Glass speak). 是因为很多时候你会想要与时间线互动(即添加真人卡等)而不是通过活动接管整个屏幕(或沉浸在Glass中)。

In your manifest you are launching MainService with a voice command, but your code is in MainActivity. 在清单中,您将使用语音命令启动MainService,但您的代码位于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. 如果你把你的代码启动线程的onStartCommand的方法MainService应尽快应用程序从菜单/语音命令发起启动。

I hope that makes things clearer. 我希望这会让事情更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的代码启动一个线程,为JavaFX Application启动一个线程? - How do I start one thread for my code and one for a JavaFX Application? 一旦我的线程中断,我怎么能中断RestTemplate调用? - How can I interrupt RestTemplate call as soon as my thread is interrupted? 如何创建Java Web Start应用程序的线程转储 - How do I create a thread dump of a Java Web Start application 应用程序启动后立即为活动加载数据 - Loading data for an Activity as soon as application starts 如何通过Google Cloud Storage对Java应用程序进行身份验证? - How do I authenticate my Java application with Google Cloud Storage? 如何创建一个对象,该对象将启动将与程序一起运行的线程? - How do I create an object that will start a thread that will run alongside my program? 如何在opengl中创建玻璃效果? - How do I create a Glass effect in opengl? 我的java web start应用程序仅在设置了详细信息时启动 - My java web start application only starts when verbose is set SpringMVC如何使用应用程序启动来启动线程 - SpringMVC How to start a thread with start of application 如何在EditText获得焦点后立即显示AlertDialog? - How do I make my AlertDialog appear as soon as EditText has focus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM