简体   繁体   中英

How to keep a Android app always running (Service and BroadcastReceiver)?

I realized an android application which always has to remain activated in the background. To make that, it is enough to use a Service which remains active after the user is left the application.

Nevertheless, if the user rebooting, the application as well as the Service will not be any more active.

How to solve this problem?

I think of having an idea with BroadcastReceiver but I am not on whether it is the best solution.

Thanks in Advance.

So you want the service to restart if the phone is rebooted ?

Create a BroadcastReceiver that listens for BOOT_COMPLETE (you need a permission for this). Have it start your service.

Android may stop your service due to lack of memory.If you have onStartCommand return START_STICKY and have your onCreate of the service call startService on itself, you'll restart when memory is available.

如果服务未运行,您可以设置警报以唤醒服务,并在启动时设置广播接收器,以便在设备启动时启动服务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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