Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Detect Android

I'm surprised to find very little info on this topic. I want to detect if a user is running Android. I'm using:

platform.dist()

This perfectly detects all all OSs and different Linux distros. However, when running this on Android, it system name is always returned as "Linux". Is there any way to detect Android? I do not want to include any 3rd party modules, since this has to be as portable as possible. Is there maybe some specific Android function that I can call in a try-catch?

like image 313
goocreations Avatar asked Jul 15 '26 06:07

goocreations


2 Answers

You can do this with Kivy

from kivy.utils import platform

if platform == 'android':
    # do something

EDIT:

As I said, I cannot use any 3rd party libs

Take a look at how Kivy implemented it.

def _get_platform():
    # On Android sys.platform returns 'linux2', so prefer to check the
    # presence of python-for-android environment variables (ANDROID_ARGUMENT
    # or ANDROID_PRIVATE).
    if 'ANDROID_ARGUMENT' in environ:
        return 'android'
like image 98
Ronie Martinez Avatar answered Jul 22 '26 06:07

Ronie Martinez


As of Python 3.7, you can check for the existence of sys.getandroidapilevel():

import sys

if hasattr(sys, 'getandroidapilevel'):
    # Yes, this is Android
else:
    # No, some other OS
like image 35
rdb Avatar answered Jul 22 '26 06:07

rdb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!