Set Toggle
The Set Toggle action enables or disables a toggle or switch using its displayed label. This action allows administrators to control switch-based settings within an application without manually interacting with the device. Unlike Set Checkbox, which is intended for checkbox controls, Set Toggle is used for UI elements represented as switches or toggles.
When to Use
Use this action when you want to:
- Enable or disable Wi-Fi from Android Settings.
- Turn Bluetooth on or off.
- Enable or disable application settings represented by switches.
- Automate the configuration of device or application preferences.
Syntax
var task = suremdmjs.automate().toggle(
"<Package Name>",
"<Label Text>",
<Enabled>
);
suremdmjs.automate().performAction(
task,
"onSuccess",
"onFailure"
);
Parameters
| Parameter | Required | Description |
|---|---|---|
| Package Name | Yes | Package name of the target application. Leave empty ("") to search all applications. |
| Label Text | Yes | Visible text displayed next to the target toggle or switch. |
| Enabled | Yes | Pass true to enable the toggle, or false to disable it. |
Parameter Details
Enabled
| Value | Description |
|---|---|
| true | Enables the specified toggle or switch. |
| false | Disables the specified toggle or switch. |
Example
The following example enables the Wi-Fi toggle from Android Settings.
!#suremdmjs
enableWifi();
function enableWifi() {
var task = suremdmjs.automate().toggle(
"com.android.settings",
"Wi-Fi",
true
);
suremdmjs.automate().performAction(
task,
"wifiEnabled",
"toggleFailed"
);
}
function wifiEnabled() {
suremdmjs.log("Wi-Fi enabled successfully.");
}
function toggleFailed() {
suremdmjs.log("Wi-Fi toggle could not be located or enabled.");
}
How This Example Works
- Searches for the Wi-Fi toggle within the Android Settings application.
- Identifies the toggle using its displayed label.
- Uses the Enabled parameter set to true to enable Wi-Fi.
- Executes the success callback if the toggle action succeeds.
- Executes the failure callback if the toggle cannot be located or the action fails.
Expected Result
The Wi-Fi toggle is enabled automatically. If the action cannot be completed, the failure callback is executed.
The target toggle must be accessible through the device's Accessibility Service. The displayed label must match the intended toggle or switch. Some system settings may restrict automation depending on the Android version and device configuration.