Making an android Python service to run in suspend state

android, android-scripting, python

Solution

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.

Problem

Here's my Python script written using android-scripting: ``` import android, time droid = android.Android() interval = 1 # every 1 minute while True: # define your own vibrate pattern here droid.vibrate(200) time.sleep(0.3) droid.vibrate(300) time.sleep(60*interval) ``` It basically vibrates every minute (like a motivator). However, when the phone is locked with screen blanked out, I don't sense any vibration. Perhaps Android is freezing the script (and hence the while loop)? Note that I am indeed running this script as a service (long-tap and click 'Start as service'). Is there a way to make this script work all the time regardless of the phone suspend state? Update 1: I do hear the vibration occasionally, not every minute .. but rather like every 5-10 minutes randomly. Update 2: This problems occurs if I run the script normally (not as a service). Seems like "time.sleep" is not sleeping for the specified time.

Original source