work_manager - Scheduling & Tasks
This module manages scheduled tasks to automatically run scripts at certain times or when certain events occur. Like Auto.js Pro's built-in scheduled tasks feature, packaged scripts can also use these APIs to create scheduled tasks.
When adding scheduled tasks, it's recommended to request ignoring battery optimizations to prevent Android from restricting background execution. See Battery Management - PowerManager.
if (!$power_manager.isIgnoringBatteryOptimizations()) {
console.log("Battery optimization ignore is not enabled");
$power_manager.requestIgnoreBatteryOptimizations();
}Warning
Due to system restrictions, scheduled tasks may not run exactly on time. Add Auto.js Pro to vendor whitelists and allow auto-start whenever possible. See Don't kill my app.
$work_manager.addDailyTask(task)
task{Object} Task configuration:path{string} Absolute path to the script to runtime{number} | {string} | {Date} Time of day to run (timestamp/string/Date supported)delay{number} Delay before starting (ms), default 0. Not reliable for long delays; avoid using this for delays > 30sloopTimes{number} Number of loops, default 1interval{number} Interval between loops (ms), default 0. Not reliable for long intervals; avoid using this for intervals > 30s
Returns {TimedTask}
Add a scheduled task that runs once per day. Only the time-of-day part is used; the date (year/month/day) is ignored.
Example: create a task that runs every day at 13:14:
console.log($work_manager.addDailyTask({
path: "/sdcard/scripts/test.js",
time: new Date(0, 0, 0, 13, 14, 0),
delay: 0,
loopTimes: 1,
interval: 0,
}));$work_manager.addWeeklyTask(task)
task{Object}path{string} Absolute path to the script to runtime{number} | {string} | {Date} Time of day to run (timestamp/string/Date supported)daysOfWeek{string[]} Days of week. Allowed values:['sunday','monday','tuesday','wednesday','thursday','friday','saturday']or['Mon','Tue','Wed','Thu','Fri','Sat','Sun']delay{number} Delay before starting (ms), default 0. Not reliable for long delays; avoid using this for delays > 30sloopTimes{number} Number of loops, default 1interval{number} Interval between loops (ms), default 0. Not reliable for long intervals; avoid using this for intervals > 30s
Returns {TimedTask}
Add a scheduled task that runs on selected weekdays.
Example: run on Monday and Tuesday at 13:14, looping 5 times:
log($work_manager.addWeeklyTask({
path: "/sdcard/scripts/test.js",
// Timestamp is Mon Jun 21 2021 13:14:00 GMT+0800; only the time-of-day (13:14:00) is used
time: 1624252440000,
daysOfWeek: ['monday', 'tuesday'],
delay: 0,
loopTimes: 5,
interval: 10
}));$work_manager.addDisposableTask(task)
task{Object}path{string} Absolute path to the script to runtime{number} | {string} | {Date} Start time (timestamp/string/Date). On v9.3+ you can use this field instead ofdatedate{number} | {string} | {Date} Same astime. Due to a historical mistake, this field should have been namedtime(v9.2 and earlier requiredate)delay{number} Delay before starting (ms), default 0. Not reliable for long delays; avoid using this for delays > 30sloopTimes{number} Number of loops, default 1interval{number} Interval between loops (ms), default 0. Not reliable for long intervals; avoid using this for intervals > 30s
Returns {TimedTask}
Add a one-time scheduled task. After it runs once, it will be removed automatically.
Example: run once at 2021-05-21 13:14:
log($work_manager.addDisposableTask({
path: "/sdcard/scripts/test.js",
date: new Date(2021, 5, 21, 13, 14, 0),
}));$work_manager.addIntentTask(task)
task{Object}path{string} Absolute path to the script to runaction{string} Broadcast action name to listen fordelay{number} Delay before starting (ms), default 0. Not reliable for long delays; avoid using this for delays > 30sloopTimes{number} Number of loops, default 1interval{number} Interval between loops (ms), default 0. Not reliable for long intervals; avoid using this for intervals > 30s
Returns {TimedTask}
Add a broadcast-triggered task. It runs when a specific event (broadcast) occurs.
The key parameter is the broadcast action. When certain events occur (e.g. battery level changes), the system broadcasts a specific action. See Broadcast Actions.
Example: run a script when battery state changes:
log($work_manager.addIntentTask({
path: "/sdcard/scripts/test.js",
action: Intent.ACTION_BATTERY_CHANGED,
}));$work_manager.removeTimedTask(id)
id{number} Task id- Returns {boolean} Whether deletion succeeded
Remove a time-based scheduled task by id.
$work_manager.removeIntentTask(id)
id{number} Task id- Returns {boolean} Whether deletion succeeded
Remove an event-based (broadcast) task by id.
$work_manager.getTimedTask(id)
id{number} Task id- Returns {TimedTask}
Get a time-based scheduled task by id.
$work_manager.getIntentTask(id)
id{number} Task id- Returns {TimedTask}
Get an event-based (broadcast) task by id.
Example:
// Add a task triggered by battery changes
let id = $work_manager.addIntentTask({
path: "/sdcard/scripts/test.js",
action: Intent.ACTION_BATTERY_CHANGED,
delay: 0,
loopTimes: 1,
interval: 0
});
// Find it by id
let task = $work_manager.getIntentTask(id.id);
// Print the script path
log(task.scriptPath);$work_manager.queryTimedTasks([options])
options{Object} Query options (optional). Omit to query all time-based tasks.path{string} Script path
Returns {TimedTask[]} Array of matched tasks
Query time-based tasks by script path, or query all time-based tasks.
Example:
let jsPath = "/sdcard/scripts/test.js";
// Add a weekly task (Sunday 13:13), loop 5 times, 5000ms interval
let task = $work_manager.addWeeklyTask({
path: jsPath,
time: 1624252440000,
daysOfWeek: ['sunday'],
delay: 0,
loopTimes: 5,
interval: 5000
});
// Query by script path
let tasks = $work_manager.queryTimedTasks({
path: jsPath
});
// Remove all matched tasks
tasks.forEach(t => {
console.log("Remove:", t);
log($work_manager.removeTimedTask(t.id));
});$work_manager.queryIntentTasks([options])
options{Object} Query options (optional). Omit to query all event-based tasks.path{string} Script pathaction{string} Broadcast action name
- Returns {TimedTask[]} Array of matched tasks
Query broadcast-triggered tasks by script path or by action, or query all broadcast-triggered tasks.
Example:
// Add a task triggered by battery changes
let task = $work_manager.addIntentTask({
path: "/sdcard/scripts/test.js",
action: Intent.ACTION_BATTERY_CHANGED,
delay: 0,
loopTimes: 1,
interval: 0
});
// Query all event-based tasks
let tasks = $work_manager.queryIntentTasks();
// Print the action of each matched task
tasks.forEach(t => {
console.log(t.action);
});TimedTask
Represents a scheduled task object containing basic task information.
TimedTask.id
- {number}
Unique task id, used to query tasks.
TimedTask.scriptPath
- {string}
Path of the script executed by this task.
TimedTask.millis
- {number}
Execution time as a timestamp in milliseconds.
TimedTask.delay
- {number}
Delay configured for this task.
TimedTask.interval
- {number}
Interval between loops for this task.
TimedTask.loopTimes
- {number}
Number of loops for this task.
TimedTask.action
- {string}
Trigger action for this task (only present for broadcast-triggered tasks).
Broadcast Actions
You can find most built-in Android intent actions in the Android docs Intent: Action. Some system components define their own actions. For example, the action for network connectivity changes is ConnectivityManager.CONNECTIVITY_ACTION (import ConnectivityManager before use). These broadcasts often carry extra data; you can read it via $engines.myEngine().execArgv.intent. See Broadcast Parameters.
Common broadcast actions:
| Action | Description |
|---|---|
| org.autojs.autojs.action.startup | Auto.js startup |
| Intent.ACTION_BOOT_COMPLETED | Boot completed |
| Intent.ACTION_SCREEN_OFF | Screen off |
| Intent.ACTION_SCREEN_ON | Screen on |
| Intent.ACTION_USER_PRESENT | Device unlocked |
| Intent.ACTION_BATTERY_CHANGED | Battery state changed |
| Intent.ACTION_POWER_CONNECTED | Power connected |
| Intent.ACTION_POWER_DISCONNECTED | Power disconnected |
| ConnectivityManager.CONNECTIVITY_ACTION | Network connectivity changed |
| Intent.ACTION_PACKAGE_ADDED | App installed |
| Intent.ACTION_PACKAGE_REMOVED | App uninstalled |
| Intent.ACTION_PACKAGE_REPLACED | App updated |
| Intent.ACTION_HEADSET_PLUG | Headset plug/unplug |
| Intent.ACTION_CONFIGURATION_CHANGED | Configuration changed (etc.) |
| Intent.ACTION_TIME_TICK | Every minute |
Examples:
// Add a task triggered when Auto.js starts (available in packaged apps too)
console.log($work_manager.addIntentTask({
path: "/sdcard/scripts/test.js",
action: "org.autojs.autojs.action.startup",
}));
// Add a task triggered when network state changes
let ConnectivityManager = android.net.ConnectivityManager;
console.log($work_manager.addIntentTask({
path: "/sdcard/scripts/conn_test.js",
action: ConnectivityManager.CONNECTIVITY_ACTION,
}));Broadcast Parameters
When a broadcast-triggered task runs, you can access the intent object via $engines.myEngine().execArgv.intent, then consult Android docs to read extras from the intent.
For example, for battery change broadcasts, the docs BatteryManager show you can read the battery level via EXTRA_LEVEL.
// Script executed due to battery change
let intent = $engines.myEngine().execArgv.intent;
if (!intent) {
console.log("Please configure a scheduled task to run this script on battery changes");
exit();
}
let BatteryManager = android.os.BatteryManager;
let level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
console.log("Battery level:", level);Bug
As of v8.7.1, Auto.js Pro may run a scheduled task twice when it is triggered. This will be fixed in a later v8.7 release.
