Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
Sridhar Ratnakumar Avatar asked Jan 16 '10 06:01

Sridhar Ratnakumar


People also ask

How you can control your android device with Python?

You can install the pure-python-adb library using pip install pure-python-adb . Optional: To make things easier for us while developing our scripts, we can install an open-source program called scrcpy which allows us to display and control our android device with our computer using a mouse and keyboard.

How do I keep a Python script running?

sleep() Function To Run Script repeatedly. So, if you do not want to use the above code and just want to run your script repeatedly then you can use the time. sleep() function. This function allows your script to keep running after sleeping for a certain amount of time.

Can I run Python script on android studio?

Chaquopy is a plugin for Android Studio's Gradle-based build system. Chaquopy enables you to freely intermix Java and Python in your app, using whichever language is best for your needs: With the Python API , you can write an app partly or entirely in Python.

Can Python run an app?

Use Termux and Flask to create, develop, and run a web app on your mobile device. Learning and using Python is fun.


1 Answers

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.

like image 135
Sean Reifschneider Avatar answered Oct 04 '22 23:10

Sean Reifschneider