work_manager

October 22, 2022

work_manager

This module is used to manage timed tasks and is used to automatically run scripts at certain times or when certain events are triggered. Just like the built-in timing task functions of CloudControl Pro, packaged scripts can also use these functions to create timing tasks.

When adding timed tasks, it is recommended to add code to apply for ignoring battery optimization to prevent Android from being restricted from running in the background. See Battery Management-PowerManageropen in new window.

if (!$power_manager.isIgnoringBatteryOptimizations()) {
    console.log("Ignore battery optimization is not turned on");
    $power_manager.requestIgnoreBatteryOptimizations();
}

Due to the limitations of various systems, timing tasks cannot be guaranteed to run on time. Please try to add CloudControl Pro to various whitelists and allow self-starting permissions.

$work_manager.addDailyTask(task)

  • task {Object} is used to describe the configuration of this scheduled task, including:
    • path {string} The absolute path of the script to be run
    • time {number} | {string} | {Date} The time when this scheduled task runs every day. It supports timestamp, string and Date objects
    • delay {number} The delay before the task starts, in milliseconds, the default is 0; if the delay is long, this parameter is not reliable, it is recommended not to use this parameter to control a delay greater than 30s
    • loopTimes {number} The number of task loops, the default is 1
    • interval {number} Task cycle interval, in milliseconds, the default is 0; if the interval is long, this parameter is not reliable, it is recommended not to use this parameter to control the interval greater than 30s
  • Return {TimedTask}

Added a timed task that runs once a day. The time parameter will only retain the time of day, ignoring the year, month, and day.

For example, create a timed task that runs at 1:14 pm every day:

console.log($work_manager.addDailyTask({
    path: "/sdcard/cloud/script/test.js",
    time: new Date(2024, 0, 1, 13, 14, 0),
    delay: 0,
    loopTimes: 1,
    interval: 0,
}));

$work_manager.addWeeklyTask(task)

  • task {Object}
    • path {string} The absolute path of the script to be run
    • time {number} | {string} | {Date} The time when this scheduled task runs every day. It supports timestamp, string and Date objects
    • daysOfWeek {Array<string>} runs on the days of the week, the parameters include: ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']
    • delay {number} The delay before the task starts, in milliseconds, the default is 0; if the delay is long, this parameter is not reliable, it is recommended not to use this parameter to control a delay greater than 30s
    • loopTimes {number} The number of task loops, the default is 1
    • interval {number} Task cycle interval, in milliseconds, the default is 0; if the interval is long, this parameter is not reliable, it is recommended not to use this parameter to control the interval greater than 30s
  • Return {TimedTask}

Add a timed task that runs on a weekly basis.

For example, create a timed task that runs 5 times every Monday and Tuesday at 1:14 pm

log($work_manager.addWeeklyTask({
    path: "/sdcard/cloud/script/test.js",
    // The timestamp is 2024-1-1 13:14:00 GMT+0800 (China Standard Time), in fact only the 13:14:00 parameter works
    time: new Date(2024, 0, 1, 13, 14, 0).getTime(),
    daysOfWeek: ['monday','tuesday'],
    delay: 0,
    loopTimes: 5,
    interval: 10
}));

$work_manager.addDisposableTask(task)

  • task {Object}
    • path {string} The absolute path of the script to be run
    • time {number} | {string} | {Date} The start time of this timing task, supports timestamp, string and Date object
    • delay {number} The delay before the task starts, in milliseconds, the default is 0; if the delay is long, this parameter is not reliable, it is recommended not to use this parameter to control a delay greater than 30s
    • loopTimes {number} The number of task cycles, the default is 1
    • interval {number} Task cycle interval, in milliseconds, the default is 0; if the interval is long, this parameter is not reliable, it is recommended not to use this parameter to control the interval greater than 30s
  • Return {TimedTask}

This function will add a one-time timed task, and the task will be automatically deleted in the timed task after it is executed once.

For example, create a timed task that runs at 13:14 on June 21, 2024.

log($work_manager.addDisposableTask({
    path: "/sdcard/cloud/script/test.js",
    date: new Date(2024, 5, 21, 13, 14, 0),
}));

$work_manager.addIntentTask(task)

  • task {Object}
    • path {string} The absolute path of the script to be run
    • action {string} Action name of the broadcast of the event that needs to be monitored
    • delay {number} The delay before the task starts, in milliseconds, the default is 0; if the delay is long, this parameter is not reliable, it is recommended not to use this parameter to control a delay greater than 30s
    • loopTimes {number} The number of task cycles, the default is 1
    • interval {number} Task cycle interval, in milliseconds, the default is 0; if the interval is long, this parameter is not reliable, it is recommended not to use this parameter to control the interval greater than 30s
  • Return {TimedTask}

A new broadcast timer task is added, which will run when a specific event (broadcast) occurs.

The most critical parameter is the action of the broadcast event. The system will broadcast a specific Action when a specific event (such as a battery change) occurs, see Broadcast Action.

For example, create a timed task that runs a script when the battery level changes:

log($work_manager.addIntentTask({
    path: "/sdcard/cloud/script/test.js",
    action: Intent.ACTION_BATTERY_CHANGED,
}));

$work_manager.removeTimedTask(id)

  • id {number} the id of the scheduled task
  • Return whether {boolean} is deleted successfully

Use id to delete timed tasks that run by time.

$work_manager.removeIntentTask(id)

  • id {number} the id of the scheduled task
  • Return whether {boolean} is deleted successfully

Use id to delete timed tasks that run by events.

$work_manager.getTimedTask(id)

  • id {number} the id of the scheduled task
  • Return {TimedTask}

Obtain timed tasks running on time by id.

$work_manager.getIntentTask(id)

  • id {number} the id of the scheduled task
  • Return {TimedTask}

Obtain timed tasks that run by events by id.

E.g:

// Add a timed task triggered by a change in the battery level of the phone
let tasks = $work_manager.addIntentTask({
    path: "/sdcard/cloud/script/test.js",
    action: Intent.ACTION_BATTERY_CHANGED,
    delay: 0,
    loopTimes: 1,
    interval: 0
});
// Find the timing task by id
let task = $work_manager.getIntentTask(tasks.id);
// Print the path of the script run by the timing task
log(task.scriptPath);

$work_manager.queryTimedTasks([options])

  • options {Object} Query parameters, optional. If it is not filled, all the timed tasks that run on time will be found
    • path {string} The path of the scheduled task script
  • Return {Array<[TimedTask](#timedtask)>} the array of timed tasks found

Search for time-running tasks through the script path, or query all time-running tasks.

E.g:

let jsPath = "/sdcard/cloud/script/test.js";
// Add a timed task that runs at 1:13 pm every Sunday, and run the script 5 times in a loop, with an interval of 5000 milliseconds
let task = $work_manager.addWeeklyTask({
    path: jsPath,
    time: new Date(2024, 0, 1, 13, 13, 0),
    daysOfWeek: ['sunday'],
    delay: 0,
    loopTimes: 5,
    interval: 5000
});
// Find timed tasks by script path
let tasks = $work_manager.queryTimedTasks({
    path: jsPath
});
// Delete all timed tasks found
tasks.forEach(t => {
    console.log("Delete: ", t);
    log($work_manager.removeTimedTask(t.id));
});

$work_manager.queryIntentTasks([options])

  • options {Object} Query parameters, optional; if not filled, all timed tasks that run by events are queried
    • path {string} The path of the scheduled task script
    • action {string} broadcast name
  • Return {Array<[TimedTask](#timedtask)>} the array of timed tasks found

Search for timed tasks that run by broadcast through script paths or monitor broadcasts, or query all timed tasks that run by broadcast.

E.g:

//Add a timed task triggered by a change in the battery level of the phone
let task = $work_manager.addIntentTask({
    path: "/sdcard/cloud/script/test.js",
    action: Intent.ACTION_BATTERY_CHANGED,
    delay: 0,
    loopTimes: 1,
    interval: 0
});
// Find all trigger event timing tasks
let tasks = $work_manager.queryIntentTasks();
// Print the trigger events of all the timed tasks found
tasks.forEach(t => {console.log(t.action);
});

TimedTask

Represents a timed task object, which contains the basic information of this timed task.

TimedTask.id

  • {number}

The unique id of this timed task, used to find the timed task.

TimedTask.scriptPath

  • {string}

The path of the executed script of this timed task.

TimedTask.millis

  • {number}

The time stamp of the execution time of this timing task, in milliseconds.

TimedTask.delay

  • {number}

The delay set by this scheduled task,

TimedTask.interval

  • {number}

The interval of this timing task cycle.

TimedTask.loopTimes

  • {number}

The number of cycles of this timing task.

TimedTask.action

  • {string}

The trigger event of this timed task (this parameter is only available for broadcast timed tasks).

Broadcast Action

You can find most of the Intent Actions that come with the Android system in the Android document Intent: Actionopen in new window; some system components will also automatically Define your own Action. For example, the Action triggered when the network connection changes is ConnectivityManager.CONNECTIVITY_ACTION (ConnectivityManager needs to be imported when using it). These Actions usually come with some additional parameters. You can get the broadcast parameters through $engines.myEngine().execArgv.intent, see Broadcast Parameters.

Some common broadcast Actions are listed below:

| Broadcast name | Description | | :-------------------------------------: | :-------- ------------------------: | | org.autojs.autojs.action.startup | When CloudControl starts | | Intent.ACTION_BOOT_COMPLETED | At boot time | | Intent.ACTION_SCREEN_OFF | When the screen is off | | Intent.ACTION_SCREEN_ON | When the screen is on | | Intent.ACTION_USER_PRESENT | When the screen is unlocked | | Intent.ACTION_BATTERY_CHANGED | When the battery level changes | | Intent.ACTION_POWER_CONNECTED | When the power is connected | | Intent.ACTION_POWER_DISCONNECTED | When the power is disconnected | | ConnectivityManager.CONNECTIVITY_ACTION | When the network connection changes | | Intent.ACTION_PACKAGE_ADDED | When a new app is installed | | Intent.ACTION_PACKAGE_REMOVED | When the app is uninstalled | | Intent.ACTION_PACKAGE_REPLACED | When the app is updated | | Intent.ACTION_HEADSET_PLUG | When the headset is unplugged and plugged in | | Intent.ACTION_CONFIGURATION_CHANGED | When some settings (screen orientation, region, etc.) are changed | | Intent.ACTION_TIME_TICK | Once every minute |

Example:

// Add a timed task triggered when CloudControl is started (software can also be used in the package)
console.log($work_manager.addIntentTask({
    path: "/sdcard/cloud/script/test.js",
    action: "org.autojs.autojs.action.startup",
}));

// Add a timing task triggered when the network status changes
let ConnectivityManager = android.net.ConnectivityManager;
console.log($work_manager.addIntentTask({
    path: "/sdcard/cloud/script/conn_test.js",
    action: ConnectivityManager.CONNECTIVITY_ACTION,
}));

Broadcast parameters

When the timed task executed by the broadcast is executed, we can get the intent object through $engines.myEngine().execArgv.intent, and then check the parameters of the intent object through the Android documentation to obtain these parameters.

For example, in the broadcast of battery changes, query the document BatteryManageropen in new window to know that the specific battery can be obtained through the EXTRA_LEVEL parameter.

// Timed task script executed due to power changes
let intent = $engines.myEngine().execArgv.intent;
if (!intent) {
    console.log("Please set up a scheduled task-run this script when the battery level changes");
    exit();
}
let BatteryManager = android.os.BatteryManager;
let level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
console.log("The power level is", level);

Bug

As of version 8.7.1, CloudControl Pro may be executed twice when a timed task is triggered, and it will be fixed in subsequent versions of 8.7.

Last update:
Contributors: Bruce