scheduling alarm for every second in android 5.1

2019-01-14 18:13发布


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

7条回答
太酷不给撩
2楼-- · 2019-01-14 18:14

Why would you do that?
Use an 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 sec timer:

mHandler.removeCallbacks(runnable);
查看更多
够拽才男人
3楼-- · 2019-01-14 18:15

This is normal behavior in Android Lollipop.

Suspiciously short interval 1000 millis; expanding to 60 seconds

Tells you that the system does not like those short time intervals anymore.

Issue #161244 documented that:

This is working as intended, though is at present inadequately documented (and we're aware of that side of the problem).

Speaking very generally: short-period and near-future alarms are startlingly costly in battery; apps that require short-period or near-future work should use other mechanisms to schedule their activity.

So don't use an AlarmService for this. Prefer a thread 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);
查看更多
贼婆χ
4楼-- · 2019-01-14 18:15

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.

查看更多
再贱就再见
5楼-- · 2019-01-14 18:18

Try These Steps.

  1. Use a Alarmmaager periodical after 1 min
  2. Inside that alarm manager use a handler that call after some seconds and perform your task.

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.

查看更多
趁早两清
6楼-- · 2019-01-14 18:20

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.

查看更多
Luminary・发光体
7楼-- · 2019-01-14 18:26

Use both 1 and 2:

  • Use the AlarmManager for the role of alerting the user every 30 minutes

  • If 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

查看更多
登录 后发表回答