如何在安装时启动android服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2127044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start android service on installation
提问by Rob Charlton
I have a device management application, which essentially runs as a service in the background from boot. I'd like to start this application immediately after installation. How do I achieve this?
我有一个设备管理应用程序,它本质上是从启动开始在后台作为服务运行的。我想在安装后立即启动这个应用程序。我如何实现这一目标?
回答by CommonsWare
You cannot do this -- there is no way to automatically start your service merely because it was installed.
你不能这样做——没有办法仅仅因为它被安装就自动启动你的服务。
The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent
via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED
Intent
filter can get control.
应用程序必须首先由用户通过某种活动调用。或者,您将需要Intent
通过清单连接到一些相关的广播,以便在这些事件之一发生时获得控制并以这种方式启动您的服务。或者,您将需要要求用户重新启动,以便您的BOOT_COMPLETED
Intent
过滤器可以得到控制。
回答by Tom
There was a hole - the Android Analytics SDK used to send an intent right after installation - but that got closed (producing lots of confusion, of course).
有一个漏洞——Android Analytics SDK 过去常常在安装后立即发送一个意图——但它被关闭了(当然会产生很多混乱)。
But the final answer, I believe, is here:
但我相信,最终答案在这里:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
This seems to suggest that, as of 3.1, Google made the decision that apps are in a stopped state until the user explicitly activates them, e.g. by launching app or placing widget.
这似乎表明,从 3.1 开始,Google 决定应用程序处于停止状态,直到用户明确激活它们,例如通过启动应用程序或放置小部件。
This means that the strategy of listening of a common broadcast (i.e. to get your app launched surreptitiously) won't work either.
这意味着侦听公共广播(即秘密启动您的应用程序)的策略也将不起作用。