简体   繁体   English

通过Application Start vs BOOT_COMPLETED启动服务

[英]Start service via Application Start vs BOOT_COMPLETED

Im sure this is a very common problem but have not seen any questions asked on how to accomplish the following. 我确定这是一个非常常见的问题,但没有看到任何问题如何完成以下。

I created a service that runs every 5 minutes and gets called when the device has finished on BOOT_COMPLETE and this works just fine. 我创建了一个每5分钟运行一次的服务,并在设备完成BOOT_COMPLETE时调用,这样就可以了。

The problem I am encountering is when I am debugging on my device I have to re-boot my phone in order to get the service to launch. 我遇到的问题是当我在我的设备上进行调试时,我必须重新启动手机才能启动服务。 Is there a way for me to start this background service that runs every 5 mintues on Application Startup. 有没有办法让我启动这个在Application Startup上每5分钟运行一次的后台服务。

So what I need is to check if the service is running in the first place and if it is not start it. 所以我需要的是检查服务是否正在运行,如果它没有启动它。

Just to elaborate a little bit more, this service fetches data of the web so my ideal situation of having it get kicked off by BOOT_COMPLETE works just great. 为了进一步详细说明,这项服务可以获取网络数据,因此我的BOOT_COMPLETE启动它的理想情况非常好。 But in the scenarios that the user downloads the app and launches it this service will not get kicked off so I need a manual way of starting. 但是在用户下载应用程序并启动它的情况下,此服务将不会被启动,因此我需要一种手动启动方式。 If the service is not running, which it wouldn't be if the user just installed it I need to start it. 如果服务没有运行,如果用户刚刚安装它就不会运行,我需要启动它。

I actually solved my problem by using the approach used in the following link. 我实际上通过使用以下链接中使用的方法解决了我的问题。

How to send and receive broadcast message 如何发送和接收广播消息

Remove the IntentFilter from the Manifest and manually calling it. 从Manifest中删除IntentFilter并手动调用它。 Hope this helps someone else. 希望这有助于其他人。

    if(!isMyServiceRunning()) {
        Intent intent = new Intent(mContext, OnBootReceiver.class);
        sendBroadcast(intent); 
    }

You can start the service manually with adb shell . 您可以使用adb shell手动启动该服务。 See How to start and stop android service from a adb shell? 请参阅如何从adb shell启动和停止android服务?

The command is: 命令是:

am startservice com.your.package/.ServiceClassName

Depending on the Android version the effective syntax may change slightly, for example: 根据Android版本,有效语法可能略有变化,例如:

am startservice -n com.your.package/.ServiceClassName
am startservice -a com.your.package/.ServiceClassName
am startservice --user 0 -n com.your.package/.ServiceClassName

Sounds like you want to do a sticky service that started by BOOT_COMPLETE event 听起来你想要做一个由BOOT_COMPLETE事件启动的粘性服务

http://developer.android.com/reference/android/app/Service.html#START_STICKY http://developer.android.com/reference/android/app/Service.html#START_STICKY

For development, you can add another intent filter to your service to start it so you can manually trigger it via activity manager from the command line after each code update without having to restart the device. 对于开发,您可以向服务添加另一个intent过滤器以启动它,以便您可以在每次更新代码后从命令行通过活动管理器手动触发它,而无需重新启动设备。

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

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