Application Operations¶
In this chapter, you can learn how to start and stop apps, grant or revoke app permissions, disable or enable apps, and replay any activity (scheme, Activity) (including unexported activities), etc.
Listing Installed Applications¶
Retrieve information about all installed applications on the device.
d.enumerate_installed_apps()
>>> d.enumerate_installed_apps( )
[packageName: "com.android.uwb.resources"
label: "System UWB Resources"
uid: 10110
enabled: true
system: true
versionName: "T-initial"
, packageName: "com.android.adservices.api"
label: "Android System"
uid: 10105
enabled: true
system: true
versionName: "14"
...
Listing Running Applications¶
Retrieve information about currently running applications on the system.
d.enumerate_running_processes()
>>> d.enumerate_running_processes()
[packages: "com.android.launcher3"
processName: "com.android.launcher3"
uid: 10084
pid: 2360
label: "Quickstep"
, packages: "com.google.android.gms"
processName: "com.google.android.gms.persistent"
uid: 10123
pid: 2765
label: "Google Play services"
, packages: "com.instagram.android"
processName: "com.instagram.android"
uid: 10150
pid: 5529
label: "Instagram"
...
>>> result = d.enumerate_running_processes()
>>> print(result[0].processName)
com.android.launcher3
Getting Application by Name¶
Get an application instance using its common name (without knowing the Package ID).
app = d.get_application_by_name("WeChat")
Getting Application by Package Name¶
Get an application instance using its Package ID.
app = d.application("com.tencent.mm")
Getting Foreground Application¶
Get the currently running foreground application instance.
app = d.current_application()
Getting Multi-User Application¶
Get an instance of a multi-user application (typically distinguished by user, with uid of 999).
app = d.application("com.my.app", user=999)
Starting an Application¶
Start this app.
app.start()
Stopping an Application¶
Force-stop this app.
app.stop()
Checking Foreground State¶
Check whether the application is currently running in the foreground.
app.is_foreground()
Getting Application Information¶
Retrieve application information such as version, etc.
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'
Checking if Installed¶
Check whether this application is already installed on the device.
app.is_installed()
Uninstalling an Application¶
Uninstall the application from the device.
app.uninstall()
Launching Application Activities¶
You can replay system-level Activities to invoke any Activity of any application. The available parameters are as follows; note that extras only support boolean, int, short, long, double, float, and string types. For the definition of the flags parameter, 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 last activities interface 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 launch an Activity in a multi-user application, the following code will replay the Activity to the multi-user 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 app for you, which is almost equivalent to directly launching 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 app in debug mode. If you have seen "Waiting for debugger", this might be useful for you. Of course, your device or app must be debuggable. The only difference from the call above is the extra 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 navigate to the certificate settings page.
d.start_activity(action="com.android.settings.TRUSTED_CREDENTIALS")
Listing Application Permissions¶
This interface can list all 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',...]
Granting Application Permissions¶
This interface is used to grant the corresponding system permissions to the application. You should use this interface when the app is not running; using it while the app is requesting permissions at runtime will not automatically grant the permissions.
from lamda.const import *
app.grant(PERMISSION_READ_PHONE_STATE, mode=GrantType.GRANT_ALLOW)
It is also equivalent to providing the full string of the permission name:
app.grant("android.permission.READ_PHONE_STATE", mode=GrantType.GRANT_ALLOW)
The mode parameter of the grant method also supports GrantType.GRANT_DENY, meaning explicitly deny the permission. And GrantType.GRANT_IGNORE, whose meaning is more special: it grants the permission to the app, but the app cannot actually use this permission properly. For example, if the app requests the camera and is denied using this parameter, the app's camera may show a black screen.
Revoking Application Permissions¶
Calling this interface can revoke permissions granted to the application. You should also call it before the application starts.
from lamda.const import *
app.revoke(PERMISSION_READ_PHONE_STATE)
Checking if Permission is Granted¶
This interface checks whether a certain permission has been properly granted to the application.
from lamda.const import *
app.is_permission_granted(PERMISSION_READ_PHONE_STATE)
Clearing Application Cache¶
This interface clears the application's cache data, typically without affecting the app.
app.clear_cache()
Clearing Application Data¶
This interface clears the application's data. Please note that this operation will wipe all app data, and information such as accounts will be lost.
app.reset()
Getting Launch Activity¶
You can query the startup 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']}
Enabling an Application¶
This interface enables a disabled application. After enabling, you can use the app normally.
app.enable()
Disabling an Application¶
This interface disables an application. A disabled app will not be displayed in the app list and cannot be used unless re-enabled. This feature allows you to temporarily or permanently freeze an app; the app cannot auto-start. When many apps are installed on the device, you can appropriately disable apps not currently in use to reduce system resource consumption.
app.disable()