I want to run alarm service for every second in my application.It is working fine below 5.1 version. but it is not triggering in 5.1 devices. I am using commonsware wakeful intent service.The logcat message is saying that "Suspiciously short interval 1000 millis; expanding to 60 seconds". How can I poll for every second in 5.1? Can anybody suggest me how to achieve this?
Explanation little bit more:
My use case is I need to do some operation for every 30 minutes interval. AFAIK For this Using alarm manager is efficient way, but here
1)I need to display the count down timer to the user. (Timer task,Count down timer,ScheduledExecutorService is pretty useful for this)
2) I need to notify the user for every 30minutes(via notification) even if the app is in background.(Alarm Service is enough for this)
but here my problem is when the app is in background,when you swipe out the application from recents( i.e.,application process is killed) none of the services or timers,handlers,executor services will not work). In this case how can I notify the user after completion of 30 minutes. Please guide me if I am thinking in wrong way.
Thanks,
Chaitanya
Why would you do that?
Use an handler instead:
And use the following to stop your 1 sec timer:
This is normal behavior in Android Lollipop.
Tells you that the system does not like those short time intervals anymore.
Issue #161244 documented that:
So don't use an
AlarmService
for this. Prefer a thread orExecutors
orTimerTask
or something else:I dont exactly understand your use case but setting alarm for every second is overkill. You can use Timer. Take a look into this class.
Try These Steps.
Remember
Its not a Good Idea, As the alarm manager is not aware of the current situation of the device, e.g., it does not consider if the device is connected to a power plug, idle or connected to a network Also the alarm manager waste of resources because its doesn't care about when the device has more resources available.
I've successfully changed the minimum AlarmManager interval from 1 minute to 30 seconds.
On your device copy /system/framework/services.jar to your computer. Extract classes.dex from it, open with Winrar for example.
Download baksmali
java -jar baksmali.jar -o extractfolder classes.dex
edit extractfolder\com\android\server\AlarmManagerService$Constants.smali
Replace all values 0xea60 (60000ms /1min in hex) to however many ms you want the minimum interval to be example 30seconds 0x7530
Save and Smali back to classes.dex
java -Xmx512M -jar smali.jar extractfolder -o classes.dex
Open services.jar again in Winrar, delete classes.dex and drag the newly created classes.dex into services.jar.
Copy back to /system/framework/services.jar
Restart device.
Also on my Samsung device, having the word alarm OR alert(not tested) in the package name adds it to a whitelist. My alarms are fired exactly when I do this.
Use both 1 and 2:
Use the
AlarmManager
for the role of alerting the user every 30 minutesIf the activity where you need to show updates is in the foreground, then also do something cheap, like
postDelayed()
, to give the user periodic updates in that activity