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

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

Related

Android Broadcast receiver: ignoring for a certain period of time

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

Restoring Alarms set with AlarmManager after device reboot

I want to create an Alarm for Android.
I've used so far an AlarmManager that sets on the given time and day.
And onReceive(), I have a NotificationManager that warns for a specific event.
My problem is that if the event is set for a week or month later, I may restart my device and my alarm will be lost.
Can someone explain me how can I save this alarm and restore it after reboot?
you need to use a broadcast receiver that listens for the broadcast
android.intent.action.BOOT_COMPLETED
then restart your alarms from your broadcast receiver

How to set a Timer to a Service?

In my app I have a Service that runs when i open my app (through onCreate method) and it just displays a simple notification, so I want the Service to close and re-open ( stopService > startService ) every 60seconds that is set by the timer. How do I set the timer to do it?
Please, look at and use AlarmManager
With AlarmManager you will be able to allow a service to start at a stablished time, alse you can use the setrepeating alarm to schedule an alarm to go off after a certain amount of time and then repeating constantly as long as you want it to.
Use AlarmManager to be poked every minute. Then in your BroadcastReceiver's onReceive() poke service to do the job by i.e. doing startService() with special intent.

AlarmReceiver working in incoming call android

i have alarm service that listin to android.intent.action.PHONE_STATE and it work good but when i have incoming call it start my alarm.
what am i doing wrong?
An incoming call is a change in phone state, so naturally that triggers your alarm if you've set your alarm service to trigger an alarm on that intent.
Take a look at EXTRA_STATE. That might help you filter down exactly when you should trigger your alarm.

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?

Categories

Resources