Making an android Python service to run in suspend state

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!

    链接地址: http://www.djcxy.com/p/16196.html

    上一篇: 通过pyqtdeploy和Qt5将PyQt5应用程序部署到Android

    下一篇: 使Android Python服务以挂起状态运行