简体   繁体   English

使用intent启动没有Activity的Android Service

[英]Start Android Service with no Activity using an intent

i created an android application with no activity. 我创建了一个没有活动的Android应用程序。 I want to start a service using a system intent like BOOT_COMPLETED. 我想使用像BOOT_COMPLETED这样的系统意图来启动服务。 I use the following receiver: 我使用以下接收器:

<receiver android:name=".autostart" >
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.SCREEN_ON" />

        </intent-filter>

I got the problem, that the intent is not received when power is connected/disconnect or boot completed. 我遇到了问题,当连接电源/断开连接或启动完成时,没有收到意图。 Is an Application with no Activity even in stopped mode after install? 安装后,即使在停止模式下,应用程序是否也没有活动? How can I start the service? 我该如何开始服务? UI is not possible because the application has no activity... UI是不可能的,因为应用程序没有活动...

Is an Application with no Activity even in stopped mode after install? 安装后,即使在停止模式下,应用程序是否也没有活动?

Yes. 是。

UI is not possible because the applicatio has no activity... UI是不可能的,因为应用程序没有活动...

Then add one. 然后添加一个。 You need one anyway, to present your license agreement, your online help, your configuration for this background processing, and so forth. 无论如何,您需要提供许可协议,在线帮助,此后台处理的配置等。 And, since your app will not run until the user launches this activity, you need to for that reason as well. 而且,由于您的应用在用户启动此活动之前不会运行,因此您也需要这样做。

Every Android application needs to be launched at least once after installation and only then it will receive any intents from system. 每个Android应用程序都需要在安装后至少启动一次,然后它才会收到系统的任何意图。 This means an application without any gui will not work in your case. 这意味着没有任何gui的应用程序将不适用于您的情况。

Many applications include only "about" activity, which is a common way to deal with that. 许多应用程序仅包含“约”活动,这是处理此问题的常用方法。

Please see: 请参见:

http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html http://developer.android.com/about/versions/android-3.1.html#launchcontrols http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html http://developer.android.com/about/versions/android-3.1.html#launchcontrols

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM