[英]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.