Basic Application Operations¶
The application operation-related APIs allow you to conveniently start and stop applications, grant or revoke application permissions, disable or enable applications, and replay any system activity (Activity), including any unexported activities.
List Installed Applications¶
Get the package ID 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 its common name (when the Package ID is unknown).
# Introduced in version 7.75
app = d.get_application_by_name("WeChat")
Get Application by Package Name¶
Get an application instance using its 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 Cloned Application¶
Get the instance of a cloned application (usually, cloned apps are distinguished by user and have a UID of 999).
app = d.application("com.my.app", user=999)
Start Application¶
You can call this method to start the app.
app.start()
Stop Application¶
You can call this method to force stop this app.
app.stop()
Check if Running in Foreground¶
You can call this method to check if the application is currently running in the foreground.
app.is_foreground()
Get Application Information¶
You can call this method to get information about the application, such as its 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 method to check if this application is installed on the device.
app.is_installed()
Uninstall Application¶
You can call this method to uninstall the application from the device.
app.uninstall()
Get Recent Activities¶
You can call the following method to get the last 5 activities in the system (up to 12). You can also directly replay any of these activities.
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 an Application Activity¶
You can replay system-level Activities and start any Activity of any application. The available parameters are as follows. Note that the extra 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 at 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 value of the Get Recent Activities method as an example, you can directly replay the last system Activity with the following code.
activity = d.get_last_activities(count=5)[-1]
d.start_activity(**activity)
If you want to start an Activity in a cloned application, the following code will replay the Activity in the cloned app.
d.start_activity(**activity, user=999)
Here are some examples for your reference. The following call will dial the customer service number 10000.
d.start_activity(action="android.intent.action.CALL", data="tel:10000")
The following call will start the Settings application for you, which is almost equivalent to starting the app directly.
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 app in debug mode. If you have ever seen Waiting for debugger, this might be useful for you. Of course, your device or app needs to be debuggable. The only difference from the call above is the addition of the 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 take you directly to the trusted credentials settings page.
d.start_activity(action="com.android.settings.TRUSTED_CREDENTIALS")
List Application Permissions¶
This method can list all the permission names declared by the 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 Permission¶
This method is used to grant system permissions to the application. You should use this method to set permissions when the app is not running. Using it while the app is running and requesting permissions will not have 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 to explicitly deny the permission, and GrantType.GRANT_IGNORE. The meaning of GRANT_IGNORE is special: it represents granting the permission to the application, but the application cannot actually use this permission properly. For example, if an application requests camera access and this parameter is used to deny it, the application's camera might show a black screen.
Revoke Application Permission¶
Calling this method can revoke a permission granted to the application. You should also call this before the application starts.
from lamda.const import *
app.revoke(PERMISSION_READ_PHONE_STATE)
Check if Granted¶
This method is used to check if a specific permission has been properly granted to the application.
from lamda.const import *
app.is_permission_granted(PERMISSION_READ_PHONE_STATE)
Clear Application Cache¶
This method is used to clear the application's cache data. This usually does not affect the application's functionality.
app.clear_cache()
Clear Application Data¶
This method is used to clear the application's data. Note that this operation will erase all application data, and information such as accounts will be lost.
app.reset()
Get Launch Activity¶
You can use this method to query the application's launch Activity (entry activity).
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']}
Enable Application¶
This method is used to enable a disabled application. After enabling, you can use the application normally again.
app.enable()
Disable Application¶
This method is used to disable an application. A disabled application will not appear in the app list, and you cannot use it unless it is enabled. This feature allows you to temporarily or permanently freeze an application. The app will not be able to auto-start. When many applications are installed on the device, you can disable apps that are not currently needed to reduce system resource usage.
app.disable()