0

I have 2 applications App A and App B. App A has activities and fragments and App B has just 1 service. The requirement is that: 1. The service in App B should be running 24 * 7 to cater to App A. 2. Based on some cloud response that the service listens to, I might need to Launch App A if it is not running.

I have started the service in App B from the BootReceiver registered in App B. App A will communicate with the service by Binding to it.

How do I ensure that the service keeps running 24 * 7 and not be destroyed when App A is not running or App A unbinds to it? In onStartCommand of the service there is nothing to do. Service needs to wait for App A to communicate with it. Following is the onStartCommand of the service.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}
DevAndroid
  • 1,150
  • 1
  • 10
  • 23
  • 1
    "How do I ensure that the service keeps running 24 * 7 and not be destroyed when App A is not running or App A unbinds to it?" -- you don't, in general. Make it a foreground service and keep your `START_STICKY`, and you will get the most uptime possible. Note that Doze mode may mean that your service cannot do anything useful for periods of time, though. "I might need to Launch App A if it is not running" -- if by this you mean "start an activity in App A", that is not supported on Android Q and higher. – CommonsWare Jun 28 '19 at 13:13
  • @CommonsWare Thank you for your valuable comment. I would also like to mention that both these apps would be running on an Android device dedicated for them i.e. there would be no other user apps running and it is not targeted for a user operated mobile device. It is also a rooted android device on which these apps will run. Would it now make sense to allow the service in App B to run 24*7? I would want it running for 24*7 also for the reason that the service needs to continuously listen to any updates from the cloud. – DevAndroid Jun 30 '19 at 15:43
  • So I need to know a way to keep the service running 24*7 or if that's really not possible, then have the max uptime. – DevAndroid Jun 30 '19 at 15:43
  • "I need to know a way to keep the service running 24*7 or if that's really not possible, then have the max uptime" -- I covered that in my earlier comment. There may be other options on rooted devices, though. "I would want it running for 24*7 also for the reason that the service needs to continuously listen to any updates from the cloud" -- you might consider using Firebase Cloud Messaging instead. – CommonsWare Jul 01 '19 at 00:29

0 Answers0