Alarm manager for every second in android 5.1


I want to start the alarm service every second in my application. It works fine under version 5.1. but it does not start on 5.1 devices. I use commonsware service wake intention. A logcat report says, "A suspiciously short interval of 1000 milliseconds expanding to 60 seconds ." How can I interrogate every second in 5.1? Can anyone suggest me how to do this?

The explanation is a bit more:

My use case is that I need to perform some operation every 30 minutes. AFAIK For this Using Alarm Manager is an effective way, but here

1) I need to display a countdown timer for the user. (Timer task, countdown timer, ScheduledExecutorService is very useful for this)
2) I need to notify the user every 30 minutes (via notification), even if the application is in the background (Alarm Service is enough for this)

but my problem here is that the application is in the background, when you delete the application from retentates (i.e. the application process is killed), none of the services or timers, handlers, executing services will work). In this case, how can I notify the user after 30 minutes. Please guide me if I am wrong.

Thanks,
Chaitanya

+7
android alarmmanager android-wake-lock background-service
source share
7 answers

This is normal behavior in Android Lollipop.

Suspiciously short interval of 1000 milliseconds; up to 60 seconds

Reports that the system does not like these short periods of time.

Problem # 161244 has documented that:

This works as intended, although it is currently inadequately documented (and we know about this side of the problem).

Generally speaking: short-period and near-future alarms are extremely expensive in a battery; applications that require short-term or immediate work should use other mechanisms to plan their activities.

Therefore, do not use AlarmService for this. Prefer a stream or Executors or TimerTask or something else:

 // Using Handler new Handler().postDelayed(runnable, TimeUnit.SECONDS.toMillis(1)); // Using Executors Executors.newSingleThreadScheduledExecutor().schedule(runnable, 1, TimeUnit.SECONDS); 
+6
source share

Why would you do this?
Use a handler instead:

 Runnable runnable = new Runnable() { @Override public void run() { // do your stuff here, called every second mHandler.postDelayed(this, 1000); } }; // start it with: mHandler.post(runnable); 

And use the following to stop your 1 second timer:

 mHandler.removeCallbacks(runnable); 
+3
source share

Use both 1 and 2:

  • Use AlarmManager to alert the user every 30 minutes.

  • If the activity on which you need to show updates is in the foreground, and also do something cheap, such as postDelayed() , to provide the user with periodic updates in this activity.

+1
source share

I definitely don’t understand your use case, but setting up an alarm for every second is unnecessary. You can use Timer . Take a look at this class.

0
source share

This is an issue message for Android 5.1 that occurs when you try to set an alarm for less than 60,000 milliseconds.

This warning occurs because setting a very low interval like this drains the battery very quickly.

A platform project participant said:

Speaking very broadly: short and near-future alarms are amazingly expensive in a battery; applications that require a short period or in the near future work should use other mechanisms to plan their activities.

Therefore, it is not recommended to use Alarm in your case.

According to your question update. You want to keep the Awake background service even if the user manually pulled it from the list of recent applications. This can be made simple using the START_STICKY flag, for example the following. Add this code to your service in the onStartCommand method:

 @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("LocalService", "Received start id " + startId + ": " + intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; } 

Source: Answer

0
source share

Try these steps.

  • Use the Alarmmaager periodic log after 1 min.
  • Inside this alarm manager, a handler is used that calls in a few seconds and performs your task.

Remember

This is not a good idea. Since the alarm manager does not know about the current situation of the device, for example, it does not take into account whether the device is connected to the power connector, does not work or is connected to the network. Also, the alarm manager is a waste of resources, because he does not care when the device has more resources available.

0
source share

I successfully changed the minimum AlarmManager interval from 1 minute to 30 seconds.

On your device, copy /system/framework/services.jar to your computer. Extract .dex classes from it, for example, open Winrar.

Download baksmali

java -jar baksmali.jar -o extractfolder classes.dex

edit extractfolder \ com \ android \ server \ AlarmManagerService $ Constants.smali

Replace all 0xea60 values ​​(60000ms / 1min in hexadecimal format) with any number of ms you want the minimum interval to be an example of 30 seconds 0x7530

Save and Smaly return 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 .dex classes into services.jar.

Copy back to /system/framework/services.jar

Reboot the device.

Also on my Samsung device, when the word alert OR alert (not verified) is added to the package name, it is added to the white list. My alarms are triggered exactly when I do this.

-2
source share

All Articles