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.
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 android
import threading
import logging
def 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=600
droid=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!
I don't know much about Python binding, so I am going to answer as general Android issue. See Power Management.
PARTIAL_WAKE_LOCK
sounds interesting: "Wake lock that ensures that the CPU is running. The screen might not be on."
Exploring a Wake Lock Example
All power management calls follow the same basic format:
- Acquire handle to the PowerManager service.
- Create a wake lock and specify the power management flags for screen, timeout, etc.
- Acquire wake lock.
- Perform operation (play MP3, open HTML page, etc.).
- Release wake lock. The snippet below illustrates this process.
PowerManager pm = (PowerManager)mContext.getSystemService(
Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE,
TAG);
wl.acquire();
// ...
wl.release();
Possible solution: use some scheduler software and start your script regularly. This way you'll not need to call time.sleep().
Maybe scripting is not a best solution for such periodic tasks. You will not face this problem if you write a simple Java app.
来源:https://stackoverflow.com/questions/2076381/making-an-android-python-service-to-run-in-suspend-state