Making an android Python service to run in suspend state Making an android Python service to run in suspend state python python

Making an android Python service to run in suspend state


The scripting environment is definitely a second-class citizen. What you want is called the AlarmManager, using ELAPSED_REALTIME. If that's not available for the scripting environment, you're stuck.

The scripting environment is not, at least currently, intended to be a full replacement for the development kit environment, where you can create full applications. It's meant to allow you to do some simple scripting tasks, at the cost of not being able to do more complicated things. Sorry.


I am facing the same kind of problem.

time.sleep() is not reliable when your android device is in "locked" mode:

Here are a few things I've tried on SL4A release4 + pythonForAndroid_r5 + android 2.3.3 on samsung galaxy S

  • wrap a time.sleep(interval) loop inside droid.wakeLockAcquirePartial() and droid.wakeLockRelease().This will prevent the CPU from slowing down.
  • use an eventWaitFor(eventName, timeOutInMilliSeconds) call:

droid.eventWaitFor("ThisEventCannotHappen", interval*60000)

  • threading.Timer() object seems also to work as expected, after a few tests on my device (confirmation needed...)

I'm not sure, but you'd better keep in mind that these tricks might drain more power than expected in true "locked/sleeping" mode and therefore reduce the runtime of your device.

Update: eventWaitFor() is not reliable either for long intervals.Here is a snippet showing how Timer() works:

import androidimport threadingimport loggingdef doStuff():    logging.info("testTimer.py: Stuff DONE")    droid.notify('testTimer.py',"doStuff() has been called")    droid.vibrate(500)def createLog(path):    logging.basicConfig(filename=path,                        level=logging.INFO,                        format='%(asctime)s %(message)s')DELAY=600droid=android.Android()logpath="/mnt/sdcard/testTimer.py.log"createLog(logpath)timer=threading.Timer(DELAY,doStuff)logging.info("timer starting now")timer.start()logging.info("doStuff() will be called by timer...Delay=%d" % DELAY)


It's unlikely that this will work in ASE without support for the AlertManager. Your best bet is to file a feature request and wait for that. Or, if you're feeling ambitious, extend ASE yourself and submit a patch!