Android 在启动时启动服务,如何在设备重启后重新启动服务类?
我需要在启动时启动一项服务。我搜索了很多。他们正在谈论广播接收器。由于我是 Android 开发新手,所以我对 Android 上的服务并没有清楚的了解。请提供一些源代码。
I need to start a service at boot time. I searched a lot. They are talking about Broadcastreceiver. As I am new to android development, I didn't get a clear picture about services on Android. Please provide some source code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的接收者:
您的 AndroidManifest.xml:
Your receiver:
Your AndroidManifest.xml:
创建一个
BroadcastReceiver
并注册它来接收ACTION_BOOT_COMPLETED。您还需要 RECEIVE_BOOT_COMPLETED 权限。阅读:监听和广播全局消息, 并设置闹钟
Create a
BroadcastReceiver
and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.Read: Listening For and Broadcasting Global Messages, and Setting Alarms
来自 http://www.jjoe64.com/2011/06/autostart-service -on-device-boot.html
From http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html
这里发布的大多数解决方案都缺少一个重要的部分:在没有唤醒锁的情况下执行此操作可能会导致您的服务在处理完成之前被终止。在另一个帖子中看到这个解决方案,也在这里回答。
由于 WakefulBroadcastReceiver 在 API 26 中已被弃用,因此建议对于低于 26 的 API 级别
您需要获取唤醒锁。幸运的是,支持库为我们提供了一个类来执行此操作:
然后,在您的服务中,确保释放唤醒锁:
不要忘记添加 WAKE_LOCK 权限并在清单中注册您的接收器:
Most the solutions posted here are missing an important piece: doing it without a wake lock runs the risk of your Service getting killed before it is finished processing. Saw this solution in another thread, answering here as well.
Since WakefulBroadcastReceiver is deprecated in api 26 it is recommended for API Levels below 26
You need to obtain a wake lock . Luckily, the Support library gives us a class to do this:
then, in your Service, make sure to release the wake lock:
Don't forget to add the WAKE_LOCK permission and register your receiver in the manifest:
您应该注册 BOOT_COMPLETE 以及 REBOOT
you should register for BOOT_COMPLETE as well as REBOOT
要在
Android O
或更高版本(即 OS >28)中重新启动服务,请使用此代码KOTLIN VERSION1) 在清单中添加权限
2) 创建一个
Class
并使用BroadcastReceiver
扩展它3) 在应用程序标签下的清单文件中声明
To Restart service in
Android O
or more ie OS >28 Use this code KOTLIN VERSION1) Add permission in manifest
2) Create a
Class
and extend it withBroadcastReceiver
3) Declare in Manifest file like this under application tag
还要在清单中注册您创建的服务并使用权限
,然后在 braodcast 接收器中调用您的服务
Also register your created service in the Manifest and uses-permission as
and then in braod cast Reciever call your service
首先在您的manifest.xml文件中注册一个接收器:
然后为该接收器编写一个广播,如下所示:
First register a receiver in your manifest.xml file:
and then write a broadcast for this receiver like:
请检查 JobScheduler 是否有 26 以上的 api< /em>
WakeLock 是最佳选择,但它在 API 级别 26 中已弃用
请检查 此链接(如果您考虑 api 级别高于 26)
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context,%20android.content.Intent)
它说
正如它所说的那样 JobScheduler
https://developer.android.com/reference/android/app/job/ JobScheduler
如果它要执行某些操作而不是启动并保留它,您可以接收广播 ACTION_BOOT_COMPLETED
如果它与前台无关,请检查辅助服务是否可以执行
另一个选项是从广播接收器启动活动,并且在 onCreate() 中启动服务后完成它,因为较新的 android 版本不允许从接收器启动服务
Pls check JobScheduler for apis above 26
WakeLock was the best option for this but it is deprecated in api level 26
Pls check this link if you consider api levels above 26
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context,%20android.content.Intent)
It says
so as it says cosider JobScheduler
https://developer.android.com/reference/android/app/job/JobScheduler
if it is to do something than to start and to keep it you can receive the broadcast ACTION_BOOT_COMPLETED
If it isn't about foreground pls check if an Accessibility service could do
another option is to start an activity from broadcast receiver and finish it after starting the service within onCreate() , since newer android versions doesnot allows starting services from receivers