views:

603

answers:

4

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.

A: 

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.

Fedor
A: 

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();
eed3si9n
+1  A: 

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!

Damon
+2  A: 

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.

Sean Reifschneider