尝试在 Android 上启动时启动服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784441/
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
Trying to start a service on boot on Android
提问by Alex
I've been trying to start a service when a device boots up on android, but I cannot get it to work. I've looked at a number of links online but none of the code works. Am I forgetting something?
当设备在 android 上启动时,我一直试图启动服务,但我无法让它工作。我在网上查看了许多链接,但没有一个代码有效。我是不是忘记了什么?
AndroidManifest.xml
AndroidManifest.xml
<receiver
android:name=".StartServiceAtBootReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver" >
<intent-filter>
<action android:name="android.intent.action._BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="com.test.RunService"
android:enabled="true" />
BroadcastReceiver
广播接收器
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceLauncher = new Intent(context, RunService.class);
context.startService(serviceLauncher);
Log.v("TEST", "Service loaded at start");
}
}
回答by Timo Bruck
The other answers look good, but I thought I'd wrap everything up into one complete answer.
其他答案看起来不错,但我想我会将所有内容汇总为一个完整的答案。
You need the following in your AndroidManifest.xml
file:
您的AndroidManifest.xml
文件中需要以下内容:
In your
<manifest>
element:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In your
<application>
element (be sure to use a fully-qualified [or relative] class name for yourBroadcastReceiver
):<receiver android:name="com.example.MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
(you don't need the
android:enabled
,exported
, etc., attributes: the Android defaults are correct)In
MyBroadcastReceiver.java
:package com.example; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent startServiceIntent = new Intent(context, MyService.class); context.startService(startServiceIntent); } }
在您的
<manifest>
元素中:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在您的
<application>
元素中(确保为您的 使用完全限定的 [或相关] 类名BroadcastReceiver
):<receiver android:name="com.example.MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
(你不需要的
android:enabled
,exported
等等,属性:Android的默认值是正确的)在
MyBroadcastReceiver.java
:package com.example; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent startServiceIntent = new Intent(context, MyService.class); context.startService(startServiceIntent); } }
From the original question:
从原来的问题:
- it's not clear if the
<receiver>
element was in the<application>
element - it's not clear if the correct fully-qualified (or relative) class name for the
BroadcastReceiver
was specified - there was a typo in the
<intent-filter>
- 不清楚
<receiver>
元素是否在<application>
元素中 - 不清楚是否
BroadcastReceiver
为 指定了正确的完全限定(或相对)类名 - 有一个错字
<intent-filter>
回答by inazaruk
As an additional info: BOOT_COMPLETE is sent to applications beforeexternal storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
作为附加信息: BOOT_COMPLETE在安装外部存储之前发送到应用程序。因此,如果应用程序安装到外部存储,它将不会收到 BOOT_COMPLETE 广播消息。
More details herein section Broadcast Receivers listening for "boot completed"
此处的广播接收器侦听“启动已完成”部分中的更多详细信息
回答by user3439968
How to start service on device boot(autorun app, etc.)
如何在设备启动时启动服务(自动运行应用程序等)
For first: since version Android 3.1+ you don't receive BOOT_COMPLETE if user never started your app at least once or user "force closed" application. This was done to prevent malware automatically register service. This security hole was closed in newer versions of Android.
首先:从 Android 3.1+ 版本开始,如果用户从未启动过您的应用程序至少一次或用户“强制关闭”应用程序,您将不会收到 BOOT_COMPLETE。这样做是为了防止恶意软件自动注册服务。此安全漏洞已在较新版本的 Android 中关闭。
Solution:
解决方案:
Create app with activity. When user run it once app can receive BOOT_COMPLETE broadcast message.
使用活动创建应用程序。当用户运行它一次应用程序可以接收 BOOT_COMPLETE 广播消息。
For second: BOOT_COMPLETE is sent before external storage is mounted. If app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
其次:在安装外部存储之前发送 BOOT_COMPLETE。如果应用程序安装到外部存储,它不会收到 BOOT_COMPLETE 广播消息。
In this case there is two solution:
在这种情况下,有两种解决方案:
- Install your app to internal storage
- Install another small app in internal storage. This app receives BOOT_COMPLETE and run second app on external storage.
- 将您的应用安装到内部存储
- 在内部存储中安装另一个小应用程序。此应用程序接收 BOOT_COMPLETE 并在外部存储上运行第二个应用程序。
If your app already installed in internal storage then code below can help you understand how to start service on device boot.
如果您的应用程序已经安装在内部存储中,那么下面的代码可以帮助您了解如何在设备启动时启动服务。
In Manifest.xml
在 Manifest.xml 中
Permission:
允许:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your BOOT_COMPLETED receiver:
注册您的 BOOT_COMPLETED 接收器:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Register your service:
注册您的服务:
<service android:name="org.yourapp.YourCoolService" />
In receiver OnBoot.java:
在接收器 OnBoot.java 中:
public class OnBoot extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Create Intent
Intent serviceIntent = new Intent(context, YourCoolService.class);
// Start service
context.startService(serviceIntent);
}
}
For HTC you maybe need also add in Manifest this code if device don't catch RECEIVE_BOOT_COMPLETED:
对于 HTC,如果设备没有捕获 RECEIVE_BOOT_COMPLETED,您可能还需要在 Manifest 中添加此代码:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Receiver now look like this:
接收器现在看起来像这样:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
How to test BOOT_COMPLETED without restart emulator or real device? It's easy. Try this:
如何在不重启模拟器或真实设备的情况下测试 BOOT_COMPLETED?这很简单。尝试这个:
adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
How to get device id? Get list of connected devices with id's:
如何获取设备ID?获取带有 id 的已连接设备列表:
adb devices
adb in ADT by default you can find in:
默认情况下,ADT 中的 adb 您可以在以下位置找到:
adt-installation-dir/sdk/platform-tools
Enjoy! )
享受!)
回答by Tony
Along with
随着
<action android:name="android.intent.action.BOOT_COMPLETED" />
also use,
还用,
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
HTC devices dont seem to catch BOOT_COMPLETED
HTC 设备似乎没有捕捉到 BOOT_COMPLETED
回答by Evgeny Erlihman
note that at the beginning of the question, there is a typo mistake:
请注意,在问题的开头,有一个错字错误:
<action android:name="android.intent.action._BOOT_COMPLETED"/>
<action android:name="android.intent.action._BOOT_COMPLETED"/>
instead of :
代替 :
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
one small "_" and all this trouble :)
一个小“_”和所有这些麻烦:)
回答by Omer Akhter
I found out just now that it might be because of Fast Boot
option in Settings
> Power
我刚刚发现这可能是因为> 中的Fast Boot
选项Settings
Power
When I have this option off, my application receives a this broadcast but not otherwise.
当我关闭此选项时,我的应用程序会收到此广播,但不会收到其他广播。
By the way, I have Android 2.3.3
on HTC Incredible S
.
顺便说一句,我Android 2.3.3
的HTC Incredible S
。
Hope it helps.
希望能帮助到你。
回答by RickNotFred
I think your manifest needs to add:
我认为您的清单需要添加:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
回答by Amir
After trying all of mentioned answers and tricks, I finally find why the code is not work in my phone. Some Android phones like "Huawei Honor 3C Android 4.2.2" have a Statup Managermenu in their settings and your app must be checked in the list. :)
在尝试了所有提到的答案和技巧之后,我终于找到了为什么代码在我的手机中不起作用。一些像“Huawei Honor 3C Android 4.2.2”这样的安卓手机在他们的设置中有一个Statup Manager菜单,你的应用必须在列表中检查。:)
回答by Nick
I have an additional <category>
-tag, don't know if that makes any difference.
我有一个额外的<category>
-tag,不知道这是否有什么区别。
<receiver android:name="BootIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Have you tried ommiting the if-clause "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()
, as the receiver probably only receives that intent anyway?
您是否尝试过省略 if-clause "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()
,因为接收者可能只会收到该意图?
回答by Jaichander
Refer This Link http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.htmlStep by Step procedure to use boot on Service
请参阅此链接http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.html逐步程序以在服务上使用引导