Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • Java

Index

Methods

create

  • create(constructor: any, args: any[], threadMode?: ThreadMode): Promise<any>
  • Create a Java object on the specified thread and return a Promise. For example, some Android UI related objects need to be created on UI thread.

    example
    "nodejs";
    const $java = $autojs.java;
    const View = $java.findClass('android.view.View');
    const context = $autojs.androidContext;
    
    async function main() {
        const view = await $java.create(View, [context], 'ui');
        console.log(view);
    }
    main();
    

    Parameters

    • constructor: any

      Java class constructor, if constructor is not a Java class constructor, throw TypeError.

    • args: any[]

      Constructor arguments

    • threadMode: ThreadMode = 'current'

      Thread mode

    Returns Promise<any>

createSync

  • createSync(constructor: any, args: any[], threadMode?: ThreadMode): any
  • Similar to create, but wait for the creation blocked and return the Java object instead of returning a Promise. Therefore, this function is usually not recommended to use, unless you know what you are doing.

    Parameters

    • constructor: any

      Java class constructor, if constructor is not a Java class constructor, throw TypeError.

    • args: any[]

      Constructor arguments

    • threadMode: ThreadMode = 'current'

      Thread mode

    Returns any

findClass

  • findClass(name: string): any
  • Find the Java class with the specified name and load it. Return the Java class.

    The returned Java class is actually a javascript constructor function, which can be used to construct a Java object, or access Java class's static methods.

    If you want to search inner classes, you need to use '$' symbol, like findClass('android.app.AlertDialog$Builder').

    If the class is not found, throw ClassNotFoundError.

    example
    "nodejs";
    const $java = $autojs.java;
    const Integer = $java.findClass('java.lang.Integer');
    const int = new Integer(255);
    console.log(Integer.toHexString(int));
    

    Parameters

    • name: string

      The full Java class name

    Returns any

    Java class

findClassOrNull

  • findClassOrNull(name: string): any
  • Similar to findClass, but return null instead of throwing exception when the class is not found.

    see

    findClass

    Parameters

    • name: string

      full java class name

    Returns any

    Java class or null

setDefaultThreadMode

  • setDefaultThreadMode(clazz: any, threadMode: ThreadMode): any
  • Set the default thread mode of the Java class. After creating a new Java object of the Java class, the default thread mode will be used. No effect on the existing Java objects.

    example
    "nodejs";
    const $java = $autojs.java;
    const View = $java.findClass('android.view.View');
    $java.setDefaultThreadMode(View, 'ui');
    

    Parameters

    • clazz: any

      Java class

    • threadMode: ThreadMode

      Thread mode

    Returns any

setThreadMode

  • setThreadMode(obj: any, threadMode: ThreadMode): any
  • Set the thread mode of the Java object. After setting, all the function calls of the Java object will be executed on the thread, and if not on the current thread, the function calls will return a Promise.

    example
    "nodejs";
    const $java = $autojs.java;
    const TextView = $java.findClass('android.widget.TextView');
    (async () => {
        const textView = await $java.create(TextView, [$autojs.androidContext], 'ui');
        $java.setThreadMode(textView, 'ui');
        await textView.setText('Hello World');
    })();
    

    Parameters

    • obj: any

      Java object

    • threadMode: ThreadMode

      Thread mode

    Returns any

wrap

  • wrap<T>(obj: T, sync??: Boolean): T
  • Wrap the specified JavaScript object, so that the returned new object's function will be called by Java when it is called. If sync is true, the functions will be blocked when they are called, and the return value will be returned to Java.

    Usually used to set some Java callbacks that are not called on JavaScript thread.

    example
    "nodejs";
    const $java = $autojs.java;
    const Button = $java.findClass('android.widget.Button');
    (async () => {
        const button = await $java.create(Button, [$autojs.androidContext], 'ui');
        $java.setThreadMode(button, 'ui');
        button.setOnClickListener($java.wrap(() => {
           console.log('click');
        }));
        button.setOnLongClickListener($java.wrap({
             onLongClick: () => {
                console.log('long click');
                return true;
             },
        }, true));
    })();
    

    Type parameters

    • T

    Parameters

    • obj: T

      JavaScript object

    • Optional sync: Boolean = false

      Whether to block the call

    Returns T