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 .
Related
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.
after an extensive search I still can't find a solution to my problem:
I need a Broadcast that runs once a day, no matter if the App is running or not. However, IF the App is running, I also need to update the UI at the end of/after the Broadcast.
I can't use a programmatically registered Broadcast because it ends with the Apps lifecycle. But from a static manifest-registered Broadcast I can't access the UI (at-least I don't know HOW).
One option would be to have 2 different Broadcasts and cancel/start them in onPause and onResume, but I wonder if there is an easier solution?
The thing you need is not broadcast receiver along with AlarmManager or JobScheduler for api above 21 and greenrobot event bus.
AlarmManager Schedules the broadcast call once a day or at any time you want and every time if the broadcast is called you can trigger event from eventbus and receive that event in the place where you want it. The thing why to use event bus is we do not need to handle if the view is visible or not.iF the view is in re use state it triggers the event the view and one method is called by event bus and in that method you can do anything you want to do with view.
personally i don't prefer service because service execution is really expensive now a days.
Note: the package name where you put alarm manager and broadcast
receiver should be "alert" some samsung mobile are very optimized so
they will only let the package name with "alert to run fully". You
will also need on boot receiver to register receiver and schedule
alarmmanager in case if the phone is booted.
I have a broadcast receiver that listens for an sms or call from a specific number. when it triggers it starts a location service which runs from that point on.
I need to be able to stop this service once it is no longer needed from my app.
My app does not need to be running for this broadcast receiver to trigger the service.
Any advice would be very much appreciated
Thank You
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
I currently have a service setup that emails a bunch of files. What I want to do is add a scheduling system setup so that at a certain time each night, that service runs (those emails are sent).
I thought maybe a Broadcast Receiver triggered by an AlarmManager would work, and it does except it only runs when the app is running. I read that Broadcast Receivers only run in the UI thread. I need this to work regardless if the app is running or not.
Im going to assume that what I need is a broadcast receiver to start [blank] to run in the background and when the AlarmManager sends an alarm that [blank] will start the service I already have setup.
If that is the correct procedure, what is [blank] ? If its not the correct procedure then what is ?
Thanks
You may want to run a RemoteService (http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html), and this article explains how to use the AlarmManager to start up a Service.
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
I actually made this change today, and my program is working better at work.
The RemoteService is so that the Service doesn't die when your Activity dies, basically.
Your procedure is correct,if you don't need an IPC ,then no necessary to implement a remote service.