Basic Application Operations¶
Application-related interfaces allow you to conveniently start and close applications, grant or revoke application permissions, disable or enable applications, and replay any system activities (Activity) including any unexported activities and other functions.
List Installed Applications¶
Get package IDs of all installed applications on the device.
d.enumerate_all_pkg_names()
>>> d.enumerate_all_pkg_names()
['com.android.calendar', 'com.android.camera2', 'com.android.contacts', 'com.android.deskclock', 'com.android.dialer', 'com.android.gallery3d', 'com.android.messaging', 'com.android.settings', 'com.android.vending', 'com.android.documentsui', 'com.android.quicksearchbox', 'com.google.android.googlequicksearchbox']
List Running Applications¶
Get information about applications currently running on the system.
d.enumerate_running_processes()
>>> d.enumerate_running_processes()
[packages: "com.android.launcher3"
processName: "com.android.launcher3"
uid: 10101
pid: 2333
, packages: "com.google.android.gms"
processName: "com.google.android.gms.persistent"
uid: 10075
pid: 2702
...
>>> result = d.enumerate_running_processes()
>>> print (result[0].processName)
com.android.launcher3
Get Application by Name¶
Get an application instance using the application’s common name (when Package ID is not clear).
# Introduced in version 7.75
app = d.get_application_by_name("WeChat")
Get Application by Package Name¶
Get an application instance using the application’s Package ID.
app = d.application("com.tencent.mm")
Get Foreground Application¶
Get the instance of the application currently running in the foreground.
app = d.current_application()
Get Multi-instance Application¶
Get an instance of a multi-instance application (typically, multi-instance applications are distinguished by user and the UID is 999).
app = d.application("com.my.app", user=999)
Start Application¶
You can call this interface to start the APP.
app.start()
Close Application¶
You can call this interface to forcefully close this app.
app.stop()
Check Foreground Running¶
You can call this interface to check if the application is currently running in the foreground.
app.is_foreground()
Get Application Information¶
You can call this interface to get application-related information such as version.
app.info()
>>> app.info()
packageName: "com.android.settings"
uid: 1000
enabled: true
processName: "com.android.settings"
sourceDir: "/system/product/priv-app/Settings/Settings.apk"
dataDir: "/data/user_de/0/com.android.settings"
firstInstallTime: 1230739200000
lastUpdateTime: 1230768000000
versionCode: 1276
versionName: "10"
>>> result = app.info()
>>> print (result.processName)
'com.android.settings'
Check if Installed¶
You can call this interface to check if this application is installed on the device.
app.is_installed()
Uninstall Application¶
You can call this interface to uninstall the application from the device.
app.uninstall()
Get Recent Activities¶
You can call the following interface to get the last 5 activities in the system (maximum 12), and you can also directly replay any activity.
activities = d.get_last_activities(count=5)
>>> d.get_last_activities(count=5)
[{'action': 'android.intent.action.MAIN', 'category': 'android.intent.category.HOME', 'component': 'net.oneplus.launcher/.Launcher', 'extras': {'android.intent.extra.FROM_HOME_KEY': True}}, {'action': 'android.intent.action.MAIN', 'category': 'android.intent.category.HOME', 'component': 'net.oneplus.launcher/.Launcher', 'extras': {'android.intent.extra.FROM_HOME_KEY': True}}, {'action': 'android.intent.action.MAIN', 'category': 'android.intent.category.HOME', 'component': 'net.oneplus.launcher/.Launcher', 'extras': {'android.intent.extra.FROM_HOME_KEY': True}}, {'action': 'android.intent.action.MAIN', 'category': 'android.intent.category.HOME', 'component': 'net.oneplus.launcher/.Launcher', 'extras': {'android.intent.extra.FROM_HOME_KEY': True}}, {'action': 'android.intent.action.MAIN', 'category': 'android.intent.category.HOME', 'component': 'net.oneplus.launcher/.Launcher', 'extras': {'android.intent.extra.FROM_HOME_KEY': True}}]
Start Application Activity¶
You can replay system-level Activities and launch any Activity of any application. The available parameters are as follows. You need to note that additional data (extras) only supports boolean, int, short, long, double, float, and string types. For the definition of the flags parameter, please refer to the documentation developer.android.com/reference/android/content/Intent.
from lamda.const import *
d.start_activity(action="*", category="*", component="*", extras={"boolean": False, "int": 1, "string": "abc", "float": 1.123}, flags=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_CLEAR_TASK, data="*", debug=False)
Now, taking the return of the Get Recent Activities
interface as an example, you can directly replay the last system Activity through the following code.
activity = d.get_last_activities(count=5)[-1]
d.start_activity(**activity)
If you want to launch a multi-instance application, the following code will replay the Activity to the multi-instance application.
d.start_activity(**activity, user=999)
Here are some examples for your reference. The following call will dial the 10000 customer service phone number.
d.start_activity(action="android.intent.action.CALL", data="tel:10000")
The following call will launch the Settings application for you, which is almost equivalent to directly starting the application.
d.start_activity(action="android.intent.action.MAIN", category="android.intent.category.LAUNCHER", component="com.android.settings/.Settings")
The following call will open the settings application in debug mode. If you’ve seen Waiting for debugger
, then it might be useful to you. Of course, your device or APP needs to be debuggable. The only difference between this call and the one above is the addition of a debug
parameter.
d.start_activity(action="android.intent.action.MAIN", category="android.intent.category.LAUNCHER", component="com.android.settings/.Settings", debug=True)
The following call will directly take you to the certificate settings page.
d.start_activity(action="com.android.settings.TRUSTED_CREDENTIALS")
List Application Permissions¶
This interface can list all declared permission names of an application.
app.permissions()
>>> app.permissions( )
['android.permission.REQUEST_NETWORK_SCORES', 'android.permission.WRITE_MEDIA_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE', 'android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_SETTINGS', 'android.permission.WRITE_SECURE_SETTINGS', 'android.permission.DEVICE_POWER', 'android.permission.CHANGE_CONFIGURATION', 'android.permission.MOUNT_UNMOUNT_FILESYSTEMS', 'android.permission.VIBRATE', 'android.permission.BLUETOOTH', 'android.permission.BLUETOOTH_ADMIN', 'android.permission.BLUETOOTH_PRIVILEGED', 'android.permission.ACCESS_COARSE_LOCATION', 'android.permission.NFC', 'android.permission.HARDWARE_TEST', 'android.permission.CALL_PHONE', 'android.permission.MODIFY_AUDIO_SETTINGS', 'android.permission.MASTER_CLEAR', 'com.google.android.googleapps.permission.GOOGLE_AUTH', 'android.permission.ACCESS_DOWNLOAD_MANAGER', 'android.permission.READ_CONTACTS', 'android.permission.WRITE_CONTACTS', 'android.permission.ACCESS_NETWORK_STATE', 'android.permission.LOCAL_MAC_ADDRESS', 'android.permission.ACCESS_WIMAX_STATE', 'android.permission.CHANGE_WIMAX_STATE', 'android.permission.ACCESS_WIFI_STATE', 'com.android.certinstaller.INSTALL_AS_USER', 'android.permission.CHANGE_WIFI_STATE', 'android.permission.TETHER_PRIVILEGED', 'android.permission.FOREGROUND_SERVICE', 'android.permission.INTERNET', 'android.permission.CLEAR_APP_USER_DATA', 'android.permission.READ_PHONE_STATE', 'android.permission.MODIFY_PHONE_STATE', 'android.permission.ACCESS_FINE_LOCATION', 'android.permission.WRITE_APN_SETTINGS', 'android.permission.ACCESS_CHECKIN_PROPERTIES', 'android.permission.READ_USER_DICTIONARY', 'android.permission.WRITE_USER_DICTIONARY', 'android.permission.FORCE_STOP_PACKAGES', 'android.permission.PACKAGE_USAGE_STATS', 'android.permission.BATTERY_STATS', 'com.android.launcher.permission.READ_SETTINGS', 'com.android.launcher.permission.WRITE_SETTINGS', 'android.permission.MOVE_PACKAGE', 'android.permission.USE_CREDENTIALS', 'android.permission.BACKUP', 'android.permission.READ_SYNC_STATS', 'android.permission.READ_SYNC_SETTINGS', 'android.permission.WRITE_SYNC_SETTINGS', 'android.permission.READ_DEVICE_CONFIG', 'android.permission.STATUS_BAR', 'android.permission.MANAGE_USB', 'android.permission.MANAGE_DEBUGGING', 'android.permission.SET_POINTER_SPEED', 'android.permission.SET_KEYBOARD_LAYOUT', 'android.permission.INTERACT_ACROSS_USERS_FULL', 'android.permission.COPY_PROTECTED_DATA', 'android.permission.MANAGE_USERS', 'android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS', 'android.permission.READ_PROFILE', 'android.permission.CONFIGURE_WIFI_DISPLAY', 'android.permission.CONFIGURE_DISPLAY_COLOR_MODE', 'android.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS', 'android.permission.SET_TIME', 'android.permission.ACCESS_NOTIFICATIONS', 'android.permission.REBOOT', 'android.permission.RECEIVE_BOOT_COMPLETED', 'android.permission.MANAGE_DEVICE_ADMINS', 'android.permission.READ_SEARCH_INDEXABLES', 'android.permission.BIND_SETTINGS_SUGGESTIONS_SERVICE', 'android.permission.OEM_UNLOCK_STATE', 'android.permission.MANAGE_USER_OEM_UNLOCK_STATE', 'android.permission.OVERRIDE_WIFI_CONFIG', 'android.permission.USE_FINGERPRINT', 'android.permission.MANAGE_FINGERPRINT', 'android.permission.USE_BIOMETRIC', 'android.permission.USE_BIOMETRIC_INTERNAL', 'android.permission.USER_ACTIVITY', 'android.permission.CHANGE_APP_IDLE_STATE', 'android.permission.PEERS_MAC_ADDRESS', 'android.permission.MANAGE_NOTIFICATIONS', 'android.permission.DELETE_PACKAGES', 'android.permission.REQUEST_DELETE_PACKAGES', 'android.permission.MANAGE_APP_OPS_RESTRICTIONS', 'android.permission.MANAGE_APP_OPS_MODES', 'android.permission.HIDE_NON_SYSTEM_OVERLAY_WINDOWS', 'android.permission.READ_PRINT_SERVICES', 'android.permission.NETWORK_SETTINGS', 'android.permission.TEST_BLACKLISTED_PASSWORD', 'android.permission.USE_RESERVED_DISK', 'android.permission.MANAGE_SCOPED_ACCESS_DIRECTORY_PERMISSIONS', 'android.permission.CAMERA', 'android.permission.MEDIA_CONTENT_CONTROL', 'com.wapi.permission.ACCESS_CERTIFICATE', 'com.oneplus.faceunlock.permission.FACE_SETTING', 'android.permission.STATUS_BAR_SERVICE', 'android.permission.MODIFY_AUDIO_ROUTING', 'com.oneplus.account.READ_ACCOUNT_INFO', 'com.android.settings.SEND_PERMISSION']
Grant Application Permissions¶
This interface is used to grant corresponding system permissions to the application. You should use this interface to set permissions when the APP is not started. Using it when the APP is running and requesting permissions will not produce the effect of clicking allow for you.
from lamda.const import *
app.grant(PERMISSION_READ_PHONE_STATE, mode=GrantType.GRANT_ALLOW)
The mode parameter of the grant method also supports GrantType.GRANT_DENY
, which means explicitly denying that permission. And GrantType.GRANT_IGNORE
, GRANT_IGNORE
has a more special meaning, which represents granting the application that permission, but the application actually cannot use this permission normally. For example, if the application requests the camera, using this parameter to deny it, the application’s camera may show a black screen.
Revoke Application Permissions¶
Calling this interface can revoke permissions granted to applications. You should also call it before the application starts.
from lamda.const import *
app.revoke(PERMISSION_READ_PHONE_STATE)
Check if Granted¶
This interface is used to check whether a certain permission has been properly granted to the application.
from lamda.const import *
app.is_permission_granted(PERMISSION_READ_PHONE_STATE)
Clear Application Cache¶
This interface is used to clear the application’s cache data, which usually does not affect the application.
app.clear_cache()
Clear Application Data¶
This interface is used to clear the application’s data. Note that this operation will clear all data of the application, and information such as accounts in the application will be lost.
app.reset()
Get Entry Activity¶
You can query the launch Activity (entry activity) of the application through this interface.
app.query_launch_activity()
>>> app.query_launch_activity()
{'action': 'android.intent.action.MAIN', 'component': 'com.android.settings/com.android.settings.Settings', 'categories': ['android.intent.category.LAUNCHER']}