QPython Interface API¶
Execute QPython scripts and manage shared variables from other apps.
Script Execution Methods¶
executeQPy()¶
Execute a QPython script.
Parameters:
- path (str): Path to the script file
- arg (str, optional): Command line arguments
Returns: True if started successfully
executeQPyAsSrv()¶
Execute a QPython script as a service.
Parameters:
- path (str, optional): Path to the script file
Returns: True if started successfully
executeQPyCode()¶
Execute Python code directly.
Parameters:
- code (str, optional): Python code to execute
Returns: True if started successfully
executeQPyCodeAsSrv()¶
Execute Python code as a service.
Parameters:
- code (str, optional): Python code to execute
Returns: True if started successfully
Shared Variables¶
Shared variables allow communication between QPython and other apps.
sharedVariableSet()¶
Set a Java shared variable.
Parameters:
- key (str): Variable name
- value (str): Variable value
Returns: The stored value
sharedVariableGet()¶
Get a Java shared variable.
Parameters:
- key (str): Variable name
Returns: The stored value
sharedVariableRemove()¶
Remove a Java shared variable.
Parameters:
- key (str): Variable name to remove
Returns: The removed value
getLastLog()¶
Get the last log output from QPython.
Returns: String log content
Usage Example¶
import androidhelper
droid = androidhelper.Android()
# Execute a script
droid.executeQPy("/sdcard/my_script.py", arg="test")
# Execute code directly
code = "print('Hello from QPython!')"
droid.executeQPyCode(code)
# Use shared variables
droid.sharedVariableSet("username", "alice")
username = droid.sharedVariableGet("username").result
print(f"Username: {username}")
# Remove variable
droid.sharedVariableRemove("username")
# Get recent log
log = droid.getLastLog().result
print(f"Log: {log}")