DateTime type, could be a number which represents millisecond timestamp; or be a string which represents standard time format; or a Date object.
Add a task to run when an activity intent is handled.
For example, when an activity intent is handled, such as opening a file in file manager, the task will be run.
The first parameter is action, which decides what action will trigger the task. For example:
android.intent.action.VIEW
: When an activity intent is handled, such as opening a file in file manager, the task will be run.android.intent.action.SEND
: When an activity intent is handled, such as sharing a file, the task will be run.The second parameter is dataType, which decides what type of file will trigger the task. For example:
The following code will add a task to run when an activity intent is handled to open a text file:
const { addActivityIntentTask } = require("work_manager");
addActivityIntentTask({
path: "/sdcard/Scripts/handle_text.js",
action: 'android.intent.action.VIEW',
dataType: "text/plain"
}).then(task => console.log(`Task ${task} added`));
The following code is the content of handle_text.js, which will read the file content and print it:
// handle_text.js
"nodejs";
const { myEngine } = require('engines');
const { getPathFromUri } = require('app');
const { readFileSync } = require('fs');
const intent = $engines.myEngine().execArgv.intent;
if (!intent) {
process.exit();
}
const uri = intent.getUri();
const file = getPathFromUri(uri);
console.log(file);
console.log(readFileSync(file, 'utf8'));
Add a broadcast intent task, will run when a specific event (broadcast) happens.
The most important parameter is the broadcast event's Action. System will send out a specific Action broadcast when a specific event (such as battery changed) happens.
For example, create a task that runs a script when battery changed:
"nodejs";
const { android } = require('rhino').Packages;
const Intent = android.content.Intent;
const { addBroadcastIntentTask } = require("work_manager");
addBroadcastIntentTask({
path: "/path/to/script.js",
action: Intent.ACTION_BATTERY_CHANGED,
}).then(task => console.log(`Task ${task} added`));
You can find most of Android system's built-in Intent Action in Intent: Action in Android document.
Some system components also define their own Actions, for example, ConnectivityManager.CONNECTIVITY_ACTION (use require('ConnectivityManager')
to import it).
Here are some common broadcast Actions:
When the task runs, you can get the Intent object by require('engines').myEngine().execArgv.intent
.
The following is an example of getting battery level when battery changed task is run:
"nodejs";
const { myEngine } = require('engines');
const { android } = require('rhino').Packages;
const intent = myEngine().execArgv.intent;
if (!intent) {
process.exit();
}
const BatteryManager = android.os.BatteryManager;
const level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
console.log("Battery:", level);
Broadcast intent task configuration
Promise of added task
Add a timed task which runs once a day. The time parameter will only keep the time of the day, ignore the year, month and day.
For example, create a timed task which runs at 1:14 PM every day.
The configuration of the timed task.
Add an intent task. There are two types:
It is recommended to use addBroadcastIntentTask and addActivityIntentTask instead of using this function to add tasks.
Add a timed task which runs once.
For example, create a timed task which runs on May 21, 2021 at 1:14 PM.
Add a timed task which runs every week.
For example, create a timed task which runs every Monday and Tuesday at 1:14 PM.
The configuration of the timed task.
Get the intent task by task ID.
Task ID
The Promise of the task, if not found, resolve null
Get the timed task by task ID.
Task ID
The Promise of the task, if not found, resolve null
Query intent tasks by script path or Intent Action.
intent task query conditions, empty to query all tasks
Query timed tasks by script path.
timed task query conditions, empty to query all tasks
Delete an intent task by id.
The id of the task.
Promise that indicates whether the task is deleted successfully. If the task does not exist, resolve false, otherwise resolve true.
Delete a timed task by id.
The id of the task.
Promise that indicates whether the task is deleted successfully. If the task does not exist, resolve false, otherwise resolve true.
This module is used to manage tasks, which can be run automatically at certain time or event, or when a file is changed.
When adding a task, it is recommended to add a request to ignore battery optimizations.