I've used 3 services in my android application.
1st Service: Listening to broadcast receivers.
2nd: In this service, I've used a Runnable, which runs every second to fetch the details of foreground application.
3rd: Uploading the data into server.
Now, I need to develop a library project which has above application's functionality.
Any services or receivers used in a library project will have to be declared in the Client application's manifest file. Here comes my questions:
Can I use Threads instead of services so that I can skip the problems of declaring services definition into manifest files?? How ever, I'll have to use one service to implement broad cast listeners.
Does number of services used in an application impacts on application's efficiency?? Because, Client's application might have one or two Services. If they wishes to include my library, then their number of services will be raised to 4 or 5. This will be a definite problem.
If I use Threads whose life cycle is dependent on application's process, will I be having problems of getting it killed if there's no memory?? Services will get restarted if it gets killed but I do not know how to encounter this problem if I use threads.
1- Threads and services are meant for diferent type of operations. For example a Foreground service would be to stream audio/video, a ThreadPool would be use for downloading lot of images and display them in imageviews (UniversalImageLoader, Volley) For single/simple operations you can use Asynctask, but when you need several threads you would prefer classic Threads and Handlers.
2- You can use several services in one application it won't affect the performance. Of course it depends on the quality of your code and the way the services interact with each others. For exemple in one of my application I use a service for downloads, 1 for connectivity, 1 for streaming live audio and audio files and 1 to manage these two. With that I use intent and broadcast receiver. No lags.
3- If application is killed, background services are killed too. It's up to you to terminate your threads cleanly in onDestroy() for example or with a broadcast receiver you can listen to the kill intent.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments