Android Broadcast receiver: ignoring for a certain period of time - android

One of my background service has registered a broadcast receiver with the intent ACTION_USER_PRESENT. Upon receiving this I start another service. However, I wish to receive this intent only once per hour. Whats the good practice:
Should I unregister the broadcast receiver in the 1st service after my 2nd service finishes and put some alarms in the 1st service?
I just ignore the broadcast intents received for the next hour?
Any other strategy?
PS. I need to be battery efficient :)

You can register ACTION_TICK that send intents every minute and only start activity when an internal variable reach > 60 for example. But this may not be the most efficient use of battery. You can use AlarmMannager too, but I dont know how it works.

You can achieved it by AlarmService
1) Register your broadcast first.
2) When you received your intent start alarm service with repeating time
3) When alarm start you can either start service or receiver
See below link Alarm Service Alarm Service

Related

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 .

The broadcast receiver's lifecycycle?Why my service gone?

My app runs in background. I use alarmmanager to trig a alarm every minutes.When my Broadcast receiver receive the alarm I start a service.But I found that the service only last for about 4~5 seconds.I think every time the alarmmanager trig a alarm and a new Broadcast receiver instance created.the Broadcast receiver goes quickly.So the service gone with it?But the android.os.Proccess.myPid() is always the same.I am confusing with it.
Thanks,
zhangkai

In Android, what's the best way to communicate between a service and a BroadcastReceiver?

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.

How to keep a task alive after phone sleeps?

my application needs to send a message to my server every 30 seconds or so.
I understand I need to use AlarmManager using RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP.
now I don't understand two things:
1) If the AlarmManager wakes up the device, why do I need to aquire a WakeLock?
2) I saw an example for using AlarmManager with WakeLock. In this example, its setting the alarm to send a broadcast to a broadcast receiver which then acquires a static wake lock and then start an IntentService which runs a task.
now, my question is, in my case, I need to follow this example entirely? why don't set the alarm to start a service instead?

Android PendingIntent howto

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.

Categories

Resources