Making an android Python service to run in suspend state

谁说胖子不能爱 提交于 2019-12-03 10:32:30

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:

  1. Acquire handle to the PowerManager service.
  2. Create a wake lock and specify the power management flags for screen, timeout, etc.
  3. Acquire wake lock.
  4. Perform operation (play MP3, open HTML page, etc.).
  5. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!