Timers

Timers

It is possible to set timers using setTimeout. Pay special attention to clear these timers when needed, LL won’t do this for you. If you don’t clear timers, you may severly increase CPU use and battery consumption.

I have created a timer with setTimeout for 5 Minutes.

When i switched off the display the timer is not executed in deep sleep mode. I expected that the CPU would have been awoken after the specified time to execute some things and sleep after that.

is that right?

When the display is switched on again the timer seemd to be executed after a time.

Can someone explain whats going on behind the curtains?

the Code:

LL.bindClass(“android.provider.Settings”);

var myItem = LL.getEvent().getItem();

var myId   = myItem.getId();

function setTimeout15Seconds() {

   Settings.System.putInt(LL.getContext().getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15*1000);

   Android.makeNewToast(“setting timeout to 15 sec”,true).show();

   myItem.setTag(“timeout”, 0);

}    

function setTimeout5Minutes() {

  var timeout = myItem.getTag(“timeout”)||0;

  if (timeout == 0) {

    Settings.System.putInt(LL.getContext().getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,5*60*1000);

    myItem.setTag(“timeout”, setTimeout(setTimeout15Seconds, 5*60*1000));

    Android.makeNewToast(“setting timeout to 5 min”,true).show();

  }  

}

setTimeout5Minutes();

]]>

3 Commentsto Timers

  1. Anonymous says:

    < ![CDATA[

    It’s something for TrianguloY​

    ]]>

  2. Anonymous says:

    < ![CDATA[

    Thanks for mentioning me, but this seems like a ‘under the hood’ implementation description. And Pierre Hébert​ is the one with the answer.

    ]]>

  3. Anonymous says:

    < ![CDATA[

    When power resources are involved you should use an alarm instead, timeouts are events posted in the local event queue, and the system has no knowledge of them. As a consequence when the processing of this event queue is suspended for whatever reason timeouts won’t fire neither. Alarms are managed by the system and can hold a cpu wake lock (see http://developer.android.com/reference/android/app/AlarmManager.html)

    ]]>

Leave a Reply

Your email address will not be published. Required fields are marked *