how to import android in python script

Question:

When I attempt to import android in my python script, I get an error along the lines of No Module, etc. Please let me know how I can fix this issue. I am using the Eclipse IDE for written python scripts.

Asked By: Quick learner

||

Answers:

When you install the Python interpreter, you get a number of sample programs, or scripts, including the traditional “Hello World” code. The code looks like this:

import android

droid = android.Android()

droid.makeToast('Hello, Android!')

The makeToast call will display the text in a pop-up dialog box. While you might be able to type a few lines on your mobile phone screen,

You certainly won’t be doing any serious coding and debugging that way. Fortunately, you can connect to your phone using either WiFi or USB enabling you to write the code on your desktop and execute it on the phone. This does require the SDK to be installed and uses the Android Debug Bridge (adb) utility found in the /tools directory.

To enable this feature you use the adb tool to forward a dummy IP port to the IP port on the device. This requires the following commands:

$ adb forward tcp:9999 tcp:4321

$ export AP_PORT=9999

The first command forwards the local port 9999 to port 4321 on the device while the second defines an environment variable that will be used later. Now all you have to do is run Python on your desktop machine, import the android.py file and you’re off and running.

One nice feature available on the phone is the API browser. This handy tool pops up a listing of all available API calls and what parameters are required. It will even insert the code into the editing window if you touch and hold the desired function. Using the API browser gives you an idea of the kinds of things that you can do with this scripting tool.

()

Automating the changing of multiple phone settings is simple with a few lines of Python code. One common use case is when you return home and want to set the phone in WiFi mode, turn the screen brightness all the way up and the screen saver off, turn off GPS and Bluetooth on and set the ringer . Here’s the code to make that happen:

# This script will turn on the charging profile

#

import android
droid = android.Android()

�

droid.setRingerVolume(0)

droid.setScreenBrightness(255)

droid.setScreenTimeout(9999)

droid.toggleWifiState(True)

droid.toggleBluetoothState(True)

SL4A brings the power of Python and multiple other languages to your Android phone. Having the ability to change multiple phone settings with a single action is worth the price of admission. There’s a really quick way to make your scripts easy to launch from your Android screen. Simply touch and hold an empty space on one of your screens. Then choose “Add folder” and select the “Scripts” folder. Now all you have to do is touch the folder on the screen, and then the DockProfile.py and your charging profile is all set.

Answered By: Manan Sharma

You can’t install the android module to my knowledge. It exists only on an android Tablet or Phone. So if you run the code on an Android device you don’t throw an error. But running it on the PC does throw an Error.

I would recommend using the following code to prevent the error:

import android
from kivy.utils import platform

if(platform == 'android'):
    droid = android.Android()

# etc.
Answered By: Edmund Witkowski

Install the python-for-android module using pip:

    pip install python-for-android
Answered By: Vincenzo Grimaldi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.