Hello,
Recently in one of my project we had a requirement to perform certain background task and I Was confused what to use for this. I was confused between BroadCast Receiver or Service. So I thought of digging more into it and here I am sharing my thoughts on this. After long time I am writing a blog without any code.
First lets understand what is BroadCast Receiver
BroadCast Receiver is meant to respond to certain events and intent. For example phone restart, or GCM message or certain other system events. So when this event occurs an intent is created and broadcast receiver are notified through this intent. Broadcast receiver will get this in onReceive method and then we can perform certain tasks.
Now let's understand what is service.
Service is meant to perform certain tasks in background without affecting user's activities in foreground. So for example a downloader. User started downloading and that can be handled in service so user don't have to wait for it to be finished. While the background service is running user can do his other activities.
So we can see both BroadCast Receiver and service do the same thing. They perform certain task in background so what shall we use for background task? The answer depends on how much time do you need to perform background tasks.
Broadcast receiver has time limit of 10 seconds. If the tasks is not completed in 10 seconds then this process is eligible to be killed or terminated by systems. So it could be chance that it gets terminated before process is finished.
While service has no time limits it can perform as long as possible and would be terminated only in extreme conditions. Android system will kill services at last only.
So the concept is clear for the time consuming tasks you have to use Service and for less time consuming tasks you shall use BroadCast Receiver. So for tasks like connecting to internet or downloading something so synching data to server a Service is recommended. While for the tasks like adding to local database or storing something to shared preferences or updating UI on certain events a BroadCast Receiver is recommended.
I hope my blog clears your doubt and helps you in choosing right thing for you. Do share your opinion in comments.
Recently in one of my project we had a requirement to perform certain background task and I Was confused what to use for this. I was confused between BroadCast Receiver or Service. So I thought of digging more into it and here I am sharing my thoughts on this. After long time I am writing a blog without any code.
First lets understand what is BroadCast Receiver
BroadCast Receiver is meant to respond to certain events and intent. For example phone restart, or GCM message or certain other system events. So when this event occurs an intent is created and broadcast receiver are notified through this intent. Broadcast receiver will get this in onReceive method and then we can perform certain tasks.
Now let's understand what is service.
Service is meant to perform certain tasks in background without affecting user's activities in foreground. So for example a downloader. User started downloading and that can be handled in service so user don't have to wait for it to be finished. While the background service is running user can do his other activities.
So we can see both BroadCast Receiver and service do the same thing. They perform certain task in background so what shall we use for background task? The answer depends on how much time do you need to perform background tasks.
Broadcast receiver has time limit of 10 seconds. If the tasks is not completed in 10 seconds then this process is eligible to be killed or terminated by systems. So it could be chance that it gets terminated before process is finished.
While service has no time limits it can perform as long as possible and would be terminated only in extreme conditions. Android system will kill services at last only.
So the concept is clear for the time consuming tasks you have to use Service and for less time consuming tasks you shall use BroadCast Receiver. So for tasks like connecting to internet or downloading something so synching data to server a Service is recommended. While for the tasks like adding to local database or storing something to shared preferences or updating UI on certain events a BroadCast Receiver is recommended.
I hope my blog clears your doubt and helps you in choosing right thing for you. Do share your opinion in comments.
No comments:
Post a Comment