Implement infinitely running broadcast receiver - android

I want a broadcast receiver to be listening all the time, as long as the app is downloaded the receiver should always be listening, so how do I achieve this?
My current method is to use a foreground service to keep the receiver alive, but is this recommended?
In all the articles I've read no one mentioned infinitely running service, so should I avoid using a service in this case?
If not then how do I do it? I have some app which I'm pretty sure that they use infinitely running broadcast receivers.

Related

What is better for battery_changed? broadcast receivers, or service, or intentService?

I want my app to know battery percentage change if the phone is charging.
So I used BroadcastReceiver with ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED to register another BroadcastReceiver with ACTION_BATTERY_CHANGED when the phone is charging, and then I thought "wouldn't it be better if I used Service?"
Then I searched about Service, and I saw IntentService, so now I wonder what is better for ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED, and what is better for ACTION_BATTERY_CHANGED?
I think I should use BroadcastReceiver for ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED, and IntentService for ACTION_BATTERY_CHANGED, am I wrong?
I want ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED to run always and forever(except if I found a better way), and run ACTION_BATTERY_CHANGED when the phone is charging.
BroadcastReceivers and service are two different things
Broadcast receivers are designed to listen to some events and perform action on it while services are used to do some task in background(without activity) like playing music in background service
Intent service is designed on top of service for long running tasks that are to be performed on a separate thread
In your scenario the best answer would be to use a broadcast receiver. It will be called everytime and there is no need to shut it down after you have performed your task unlike services

Scheduing an event in android

I have to schedule an event in Android. My event is starting a service and just doing the work in background even when the application is not running.
Can I do this even without Broadcast Receiver?
For starting the service, do I need Broadcast Receiver?
I saw some posts, only some of them used Broadcast Receiver :
How to start Service using Alarm Manager in Android?
Scheduling an event in Android
http://www.learn-android-easily.com/2013/05/android-alarm-manager_31.html
http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/
UPDATE:
From the posts that I read further, I realize that I have to use Broadcast Receiver if the app is not running at the time of event, but I don't need Broadcast Receiver if my application is running at the time of event. Please let me know if I have reached the right conclusion.
You can use JobScheduler to achieve your requirements .

Relationship between broadcast receiver and Service

Im confused with Service and Broadcast receiver.what is the relationship between these two?why we have to call broadcast receiver when we start a service.Can anyone kindly explain the concept between these two elements
You don't have to register a BroadcastRecevier when you start a Service. That is, even if you don't register a BroadcastReceiver, our Service will work as expected. There is no must have dependency between the two.
As explained by Gridtestmail, a Service is a process you want to run in the background, without having an interface to the user.
A BroadcastReceiver is registered, when you want to be notified about certain events happening - for example, discovering a new bluetooth device or receiving an incoming call.
If you register a BroadcastReceiver for receiving incoming calls , then your Receiver's onReceive() method is called whenever there is an incoming all, so you can process it.
Similarly, for other event detection stuff.
I hope the concept is clear to you now.
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method. Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Example : Service and BroadcastReceiver

Start Again Already Running Service?

I have a broadcast receiver listening called PACKAGE_ADDED and an other broadcast receiver listening called BOOT_COMPLETED. The bootcompleted broadcast receiver starts my service.When the new app is installed, I want to send a message to my service .The first solution that came to my mind was to start the service again with
intent.setAction("NEW_APP_INSTALLED");
startService(intent);
without stopping the service and check the intent.getAction() value in theservice.onStart() method. If the result is NEW_APP_INSTALLED, then call newAppInstalled().I don't think this is an elegant solution.
Is starting the service repeatedly a problem? And what happens when my activity binds to it via ipc(aidl) while fetching data and the new app installed broadcast receiver starts it again? Lastly what is the best way to solve my problem?
Is starting the service again and again problem?
Lastly what is the best way to solve my problem?
It is perfectly fine for you to call startService on an already running service. And you can either do it the way you suggested or have two different services (one for boot, one for new_app) or you can register a BroadcastReceiver in the service after it's started, but that wouldn't be effective because then if you try to send a message to it and it's not running already, it won't get the message.. I prefer one service as you suggested and using startService.
And what happens when my activity bind it via ipc (AIDL) fetching data
and new app installed broadcast receiver starts it again?
Well, I don't know anything about AIDL, really. This might help. That page does state "Most applications should not use AIDL to create a bound service". This is because it makes multi-threading needed and makes it more complicated.
Please let me know if I failed to answer to your satisfaction - though I can't really elaborate on AIDL specifically because I don't know anything about it.

Registering Receivers dynamically

I have registered receiver dynamically with service. Will receiver work after service will die?
Now I have a question: will receiver work after service will die?
No. It will work briefly. Eventually, Android will terminate your process, if it thinks there are no services running (or they have been running too long) and that there are no activities in the foreground.
It is bad form for a service (or an activity) to leak a receiver.

Categories

Resources