Reputation: 3971
I am creating a constant running service in android so I'm thinking foreground services are the best option.I have some general questions regarding foreground services in android as I am about to implement it. Google docs didnt help me out.
Regards Utsav.
Upvotes: 2
Views: 436
Reputation: 17615
1. When a foreground service can be killed?
The process hosting the foreground service is considered as a Foreground process. Such a process is considered as most important and is killed last. From docs:
Generally, only a few foreground processes exist at any given time. They are killed only as a last resort—if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.
2. Do we need to define foreground service as START_STICKY?
Depends on task that your are performing. Since you just said constant running service, I believe you may need it. The flag has same meaning for both normal and foreground service. START_STICKY
flag indicates, if the service is restarted after it's hosting process got killed, the service's onStartCommand
will be called with null intent. This is a way to make the service aware that it got restarted.
3. Do we need to restart foreground services on android BOOt or they automatically start.
No. You need to register a broadcast receiver for Intent.ACTION_BOOT_COMPLETED and start the service manually from onReceive
.
4. How many foreground services can we start from the same application at a time.
I have read somewhere in very obsequious manner in google docs that we can nominate 3 foreground service at a time, what does that mean?
Not sure, it would be great if you can share the source. Whether you have 1 or 3 foreground service, the process hosting it is considered as foreground process. Hosting multiple foreground services results in multiple notifications in status bar, which makes user aware of those services running and know how to stop it. You can also have one foreground service with multiple threads running different operations.
5. As the name suggests does the foreground service interrupts the main or UI thread?
As explained above, foreground service makes process hosting it as foreground process and user aware of that service. Apart from that there is absolutely no difference from normal service. A service will by default run on UI thread, long running operations need to be run on separate thread. If device has multiple core processors, the threads may or may not run on the available cores parallely. You can read this blog written by @fadden. From blog:
Sometimes people wonder why their multi-thread application doesn't appear to be using all available cores. This generally happens for two reasons:
- Migrating threads between cores is expensive, performance-wise;
- Activating a CPU core is expensive, battery-wise.
P.S. : The constant running services are always considered as anti-patterns in android, as they continuously use system resources thereby indirectly affecting other applications. So if ever have to use them, design them carefully.
Upvotes: 4