Ok I am stuck. Can someone please point me in the right direction? I have no idea where to start with pendingintents. I need to start custom service that sends some data back to the activity that started it. How would I do that?
I would probably register a Broadcast Receiver in my Activity and if I needed to communicate from Service to Activity, send a broadcast from the Service and the Activity's receiver would pick it up as long as the Activity was currently running. This, by the way, would not require the use of a PendingIntent. PendingIntents are more used with alarms from the AlarmManager or with Notifications.
You should consider using AsyncTask if you expect your Activity to be active (visible) all the time the Service runs.
Related
I need to be able to have a service "tell" a BroadcastReceiver to do some work, and then wait until that work is done, and once it's finished, the BroadcastReceiver needs to send the result back to the service. And at that point, the service can continue execution.
So, "sending" the work from the service to the BroadcastReceiver is easy -- I just send a broadcast with the intent using extras for the work that needs to be done. But I don't know how to have the BroadcastReceiver then send the results back to the service. What's the best way to do this?
Edit:
I should have mentioned that the service in question is actually a contact sync adapter service. I'm not sure if that makes a difference, but maybe it does. Some of the work can't be done in the scope of the sync adapter, and that's why I'm offloading it to be done by the broadcastreceiver.
Register a Second inline Broadcast Receiver from the Service just to receive the results from the First Broadcast receiver. Pass the data via Intent. In the Second Broadcast receivers onReceive() do whatever you want.
Unregister the Second Broadcast Receiver in onDestroy of the Service.
I'm following this basic tutorial:
http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/
As you can see, it basically does the following (correct me if i'm wrong, please):
Once MainActivity is launched, sets an Alarm for a specific date and time.
As soon as this date and time is reached, it starts an intent to MyReceiver.class
First stop.: MyReceiver is extending BroadcastReceiver. Is it possible to start an Intent to a normal Activity?
BroadcastReceiver just triggers a new Intent to open a Service, which just shows a Notification.
Second stop.: Same as before: BroadcastReceiver, can only launch Intents which are Services? Or can it launch normal activities? Why do I even have to call any new intent? Why can't I just do the job in BroadcastReceiver (like downloading some content over internet)?
I'm a bit new about Services, so I'm sorry if I said something extremly weird.
Thank you so much.
Why can't I just do the job in BroadcastReceiver (like downloading
some content over internet)?
As per Android Documentation Broadcast receivers onReceive method is called on Main Thread so you can not perform downloading task in onReceive.Since it will freeze the UI and may throw exception in 3.0 and above version.
If you want to perform download,then the best bet would be to trigger IntentService inside onReceive ( IntentService by default runs on background thread.)
BroadcastReceiver is just a means to do anything you want upon receiving a broadcast.
A Service is something that won't easily be killed by the Android OS unlike an Activity. Service has no GUI though.
BroadcastReceivers can launch anything you want (Activities, Services) with an intent.
The reason you dont want to stuff in a BroadcastReceiver is because they only live so long where as a service runs until you tell it to stop.
If you are doing polling or something you really dont need a BroadcastReceiver and you can just use a service (IntentService specifically) with your alarm manager. An IntentService only runs a long as it has something to do meaning it will stop itself unlike a normal service where you have to stop it when its done.
My app needs to grab some data from the web at a specific time each day. So I use an AlarmManager to schedule the task and that works ok.
But when looking at various examples there seems to be two ways to deal with the AlarmManager when it comes to the pending intent.
One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.
Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.
Can someone explain to me the difference between the two approaches so I can decide on which one to rely?
EDIT: One more question is where to acquire the wake lock when using getService()?
For example, when using a BroadcastReceiver I have the following line in onReceive():
WakeReminderIntentService.acquireStaticLock(context);
How should I acquire the wake lock if I instead call the service directly like:
PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, OnAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
Should I simply acquire it from within the service instead?
One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.
it has one more step in starting service than
Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.
then you should use the second approach as it is reducing your one step in execution..
Reading your edit I presume you found out yourself: If you want to make sure that your service is started when using AlarmManager, you better take the detour of first sending to a receiver and acquiring a wake lock there.
Otherwise it is possible that the phone will sleep before the requested service is launched.
That's what the javadoc of AlarmManager says and I also read it in post by Google engineer.
So now for your edit: When to acquire the lock?
The whole point of using the receiver is to acquire the lock within the onReceive() method of the receiver, because Android will not fall asleep during the execution of this method.
For an example see this question.
I need help in my android app development.
It goes something like this,
I will be having two separate applications (2 projects). In one application, i have to start a repeating alarm and in the other application i have to cancel the same alarm that was started in the first application.
The Android documentation says, the same pending intent and the intent object that was used to start the alarm
should be used the cancel the alarm.
So in this scenario, the pending intent and the intent object that was used to start the alarm will belong to application1 so i cannot used the same objects in application2
How do I proceed?
In summary -
The problem is, I need to start a repeating alarm from one application and i have to cancel the same alarm from another application.
Can this be done. If so, How?
Thanks in advance.
ifreeman
It is not that straightforward. Only original activity can cancel the alarm.
So I think you can configure a custom broadcast. When the second activity needs to cancel an alarm it will send this broadcast. The first activity will be listening to the broadcast and cancel the appropriate alarm on receiving it.
I guess you can do. Alarms are considered as same if intents passed to them via pending intent are same. filterEquals method of Intent class defins if intents are same or not. If intents are same then alarms are same so u can cancel that alarm. Check once.
I've a little problem in my "timer" development.
I've an activity that shows a timer and when the user press start it schedules an alarm with AlarmManager service.
In the broadcast receiver I want to check if the activity Timer is running and visible in order to avoid unneeded notification.
So the broadcast receiver should set notification only if the activity isn't in foreground.
Any suggestion?
Thx,
SL3
You can use a Singletone pattern or SharedPreference property and store there some kind of state.
So, from your activity you can write the state and from your broadcast receiver you can read that state and then decide what to do.