Options
All
  • Public
  • Public/Protected
  • All
Menu

Module 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');

Index

Variables

Const packageName

packageName: string = ...

The package name of the current application.

In Auto.js 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: string): 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

    • file: string

    Returns void

getApkInfo

  • getApkInfo(file: string, options?: PMOptions): android.content.pm.PackageInfo | null
  • Parse an APK file and return the package info.

    see

    PackageManager.getPackageArchiveInfo

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

    Parameters

    • file: string

      The file path.

    • Optional 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): 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

    • packageName: string

      package name

    Returns string | null

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

getInstalledApps

  • getInstalledApps(options?: PMOptions): 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.getInstalledApplication

    Parameters

    Returns android.content.pm.ApplicationInfo[]

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

getInstalledPackages

  • getInstalledPackages(options?: PMOptions): android.content.pm.PackageInfo[]
  • Get the list of installed applications.

    see

    PackageManager.getInstalledPackages

    Parameters

    • Optional 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): 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

    • targetAppName: string

      Application name

    Returns string | null

    Package name, or null

getUriForFile

  • getUriForFile(pathOrUri: string): 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

    • 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

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

    see

    adb am

    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

    Returns string

    The corresponding shell intent command parameters

launch

  • launch(packageName: string): 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 background

    Parameters

    • 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: string): 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 background

    Parameters

    • targetAppName: string

      Name of the application to start

    Returns boolean

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

makeIntent

  • 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

    Returns any

    The new intent object

openAppSettings

  • openAppSettings(packageName: string): boolean
  • Open the details page of the app. If the app is not found, return false; otherwise return true.

    Parameters

    • packageName: string

      package name

    Returns boolean

    whether the app is found

openUrl

  • openUrl(url: string): void
  • Open website with browser. If no browser app installed, then throw ActivityNotException.

    Parameters

    • url: string

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

    Returns void

parseUri

  • parseUri(uri: string | android.net.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

    • uri: string | android.net.Uri

      uri string or uri object, the latter directly return the uri itself

    Returns android.net.Uri

    parsed Uri object

sendBroadcast

  • Build a new intent from options and send the broadcast.

    see

    makeIntent

    Parameters

    • 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

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

    Parameters

    Returns void

startActivity

  • 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

    • 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

  • Build a new intent from options and start the service.

    see

    makeIntent

    Parameters

    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: string): 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

    • packageName: string

      the package name to uninstall

    Returns void

viewFile

  • viewFile(file: string): 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

    • file: string

    Returns void