app


CloudControl Pro 9 Docs / app

app

The app module provides functions to use other applications, interact with other applications, and so on.

It also provides advanced functions startActivity and sendBroadcast, which can be used to complete the interaction with other applications.

To import this module, use the statement const app = require('app'); or import the part of functions and properties const { startActivity, packageName } = require('app');

Table of contents

Interfaces

Variables

Functions

Variables

packageName

Const packageName: any

The package name of the current application.

In CloudControl Pro, it is org.autojs.autojspro; in released app, it is the package name of the released app.

Example

"nodejs";
const { packageName } = require("app");
console.log(packageName);

Functions

editFile

editFile(file): void

Edit file with other apps. The case that file does not exist is handled by the third party app. If no app can edit the file, then throw ActivityNotException.

Example

"nodejs";
const app = require("app");
app.editFile("/sdcard/1.txt/");

Parameters

Name Type
file string

Returns

void


getApkInfo

getApkInfo(file, options?): android.content.pm.PackageInfo | null

Parse an APK file and return the package info.

See

PackageManager.getPackageArchiveInfoopen in new window

Example

"nodejs";
const app = require("app");
const info = app.getApkInfo("/path/to/apk", {
   get: ['meta_data'],
});
console.log(info.packageName);

Parameters

Name Type Description
file string The file path.
options? PMOptions the filter options

Returns

android.content.pm.PackageInfo | null

A PackageInfo object containing information about the package archive. If the package could not be parsed, returns null.


getAppName

getAppName(packageName): string | null

Get the app name for the given package name. If the app of the given package name is not found, returns null.

Parameters

Name Type Description
packageName string package name

Returns

string | null

the app name of the given package name, or null if not found


getInstalledApps

getInstalledApps(options?): android.content.pm.ApplicationInfo[]

Get the list of installed applications.

Returns an array of ApplicationInfo objects. If there are no installed applications, an empty array is returned.

Options options.match specifies which applications to return. Options options.get specifies which information about each application to return.

Example

"nodejs";
const app = require("app");
const apps = app.getInstalledApps({
    get: ['meta_data'],
    match: ['system_only']
});
console.log(apps);

See

PackageManager.getInstalledApplicationopen in new window

Parameters

Name Type Description
options? PMOptions 过滤选项

Returns

android.content.pm.ApplicationInfo[]

为当前用户安装的所有应用程序包的列表。如果设置了match选项 uninstalled_packages,则包括被删除但保留了数据的应用程序。


getInstalledPackages

getInstalledPackages(options?): android.content.pm.PackageInfo[]

Get the list of installed applications.

See

PackageManager.getInstalledPackagesopen in new window

Parameters

Name Type Description
options? PMOptions options.match specifies which applications to return. Options options.get specifies which information about each application to return.

Returns

android.content.pm.PackageInfo[]

An array of PackageInfo objects. If there are no installed applications, an empty array is returned.


getPackageName

getPackageName(targetAppName): string | null

Get the package name corresponding to the application name. If the application does not exist, then return null; if the application name corresponds to multiple applications, then only return the package name of one of them.

Parameters

Name Type Description
targetAppName string Application name

Returns

string | null

Package name, or null


getUriForFile

getUriForFile(pathOrUri): android.net.Uri

Create a uri object from a file path or uri string. In higher version Android, the uri returned will be a content uri.

Parameters

Name Type Description
pathOrUri string pathOrUri file path or file uri, for example "/sdcard/1.txt" or "file:///sdcard/1.txt"

Returns

android.net.Uri

file uri, can be used as Intent's data property


intentToShell

intentToShell(options): string

Construct an Intent according to the options, and convert it to the corresponding shell intent command parameters.

See

adb amopen in new window

Example

"nodejs";
const { intentToShell } = require("app");
const { exec } = require("shell");
exec("am start " + intentToShell({
    packageName: "org.autojs.autojs",
    className: "org.autojs.autojs.ui.settings.SettingsActivity_"
}), { root: true});

Parameters

Name Type Description
options IntentOptions Intent options

Returns

string

The corresponding shell intent command parameters


launch

launch(packageName): boolean

Start the application by package name. If the application does not exist, then return false; otherwise return true.

Note! On some Android versions, the application may be limited to start in the background, even if the return value is true, and it may not actually start the application.

See

Restrictions on starting activities from the backgroundopen in new window

Parameters

Name Type Description
packageName string Package name of the application to be launched.

Returns

boolean

Whether the application exists, instead of whether it is actually launched successfully.


launchApp

launchApp(targetAppName): boolean

Start the application by application name. If the application does not exist, then return false; otherwise return true. If the application name corresponds to multiple applications, then randomly start one of them.

Note! On some Android versions, the application may be limited to start in the background, even if the return value is true, and it may not actually start the application.

See

Restrictions on starting activities from the backgroundopen in new window

Parameters

Name Type Description
targetAppName string Name of the application to start

Returns

boolean

Whether the application exists, instead of whether it is actually launched successfully.


makeIntent

makeIntent(options): any

Build a new intent from options.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and Context.startService(Intent) or Context.bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Example

"nodejs"; 
const app = require("app");
const i = app.makeIntent({
    action: "VIEW",
    type: "image/png",
    data: "file:///sdcard/1.png"
});
$autojs.androidContext.startActivity(i);

Parameters

Name Type Description
options IntentOptions intent options to build intent

Returns

any

The new intent object


openAppSettings

openAppSettings(packageName): boolean

Open the details page of the app. If the app is not found, return false; otherwise return true.

Parameters

Name Type Description
packageName string package name

Returns

boolean

whether the app is found


openUrl

openUrl(url): void

Open website with browser. If no browser app installed, then throw ActivityNotException.

Parameters

Name Type Description
url string The url of the website, must start with "http://" or "https://".

Returns

void


parseUri

parseUri(uri): android.net.Uri

Parse uri string or uri object and return the corresponding Uri object. If the uri string is invalid, the function will return a Uri object, but the values of scheme, path, etc. may be null.

On higher version Android, since the system restricts the absolute path of a file, the Uri returned by this function may be a content uri like content://... if the uri string is file://...

Parameters

Name Type Description
uri any uri string or uri object, the latter directly return the uri itself

Returns

android.net.Uri

parsed Uri object


registerBroadcastReceiver

registerBroadcastReceiver(filter): BroadcastReceiver

Parameters

Name Type
filter any

Returns

BroadcastReceiver


sendBroadcast

sendBroadcast(target): Promise<void>

Build a new intent from options and send the broadcast.

See

makeIntent

Parameters

Name Type Description
target IntentOptionsWithRoot broadcast name or broadcast intent to send. If target is a string, then: * inspect_layout_hierarchy inspect layout hierarchy * inspect_layout_bounds inspect layout bounds But these broadcasts are not available in release apps.

Returns

Promise<void>

Promise, if using root permission, will wait for shell command to finish; if not using root permission, will return immediately.


sendEmail

sendEmail(options): void

Start the email application. If there is no email application, then throw ActivityNotException.

Parameters

Name Type Description
options EmailOptions Email sending options

Returns

void


startActivity

startActivity(target): Promise<void>

Build a new intent from options and start the activity.

See

makeIntent

Example

"nodejs";
const { startActivity } = require("app");
startActivity({
    action: "SEND",
    type: "text/plain",
    data: "file:///sdcard/1.txt"
});

Parameters

Name Type Description
target "console" | "settings" | IntentOptionsWithRoot activity name or activity intent to start. If target is a string, then: * console: start LogActivity * settings: start SettingsActivity

Returns

Promise<void>

Promise, if using root permission, will wait for shell command to finish; if not using root permission, will return immediately. Never wait for activity to start before returning.


startService

startService(target): Promise<void>

Build a new intent from options and start the service.

See

makeIntent

Parameters

Name Type Description
target IntentOptionsWithRoot service options to build service intent

Returns

Promise<void>

Promise, if using root permission, will wait for shell command to finish; if not using root permission, will return immediately. Never wait for service to start before returning.


uninstall

uninstall(packageName): void

Uninstall application. Will pop up uninstall application dialog. If the package name of the application is not installed, the application uninstaller will handle it, which may pop up a "not found" prompt.

Parameters

Name Type Description
packageName string the package name to uninstall

Returns

void


unregisterBroadcastReceiver

unregisterBroadcastReceiver(receiver): void

Parameters

Name Type
receiver BroadcastReceiver

Returns

void


viewFile

viewFile(file): void

Open file with other apps. The case of file not exist will be handled by the app that open the file. If can't find the app to open the file, it will throw ActivityNotException.

Parameters

Name Type
file string

Returns

void