如何推出一款Android服务时,应用程序启动? [英] How to launch a Android Service when the app launches?
问题描述
我还是新鲜到Android和Id认为以下配置工程启动我的服务时,应用程序启动。
I'm still fresh to Android and Id think the below config works for launching my service when the app launches.
<service android:name=".PlaylistUpdaterService">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
但是,这并非如此。我错过了什么?
But this is not the case. What did I miss?
推荐答案
纠正我,如果我错了,但android.intent.category.LAUNCHER仅对活动。所以,并不像开始服务的有效途径。就可以实现,如果你做了以下的相同的:
Correct me if I am wrong, but android.intent.category.LAUNCHER is valid only for Activity. So, does not look like valid way to start Service. The same you can achieve if you do the following:
- 创建透明的活动将只用来启动服务
- 在该活动中,您不需要指定GUI布局。所以,你不需要的setContentView()在活动的onCreate()。你唯一需要的是把
@android:款式/ Theme.NoDisplay
@android:style/Theme.NoDisplay
在此活动在AndroidManifest.xml中的主题标签。
under Theme tag for this Activity in AndroidManifest.xml.
- 从
的onCreate启动服务()
您的活动。 - 呼叫
完成()
在ONSTART()
的活动中来关闭它。
- start Service from
onCreate()
of your Activity. - call
finish()
inonStart()
of your Activity to close it.
所以,你的活动将是对用户不可见,最后不久,没有人会发现,它被用来启动服务。
So, your Activity will be invisible to the user, last shortly and nobody will notice that it was used to start the service.
这篇关于如何推出一款Android服务时,应用程序启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!