Interface: Java


CloudControl Pro 9 Docs / globals / Java

Interface: Java

globals.Java

Table of contents

Methods

Methods

boxBoolean

boxBoolean(value): any

Wrap value as a wrapper of java.lang.Boolean, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type boolean or java.lang.Boolean can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Boolean value)
values.put("key", $java.boxBoolean(true));

Parameters

Name Type
value number

Returns

any


boxByte

boxByte(value): any

Wrap value as a wrapper of java.lang.Byte, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type byte or java.lang.Byte can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Byte value)
values.put("key", $java.boxByte(1));

Parameters

Name Type
value number

Returns

any


boxChar

boxChar(value): any

Wrap value as a wrapper of java.lang.Char, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type char or java.lang.Char can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Char value)
values.put("key", $java.boxChar(1));

Parameters

Name Type
value number

Returns

any


boxDouble

boxDouble(value): any

Wrap value as a wrapper of java.lang.Double, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type double or java.lang.Double can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Double value)
values.put("key", $java.boxDouble(1));

Parameters

Name Type
value number

Returns

any


boxFloat

boxFloat(value): any

Wrap value as a wrapper of java.lang.Float, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type float or java.lang.Float can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Float value)
values.put("key", $java.boxFloat(1));

Parameters

Name Type
value number

Returns

any


boxInt

boxInt(value): any

Wrap value as a wrapper of java.lang.Integer, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type int or java.lang.Integer can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Integer value)
values.put("key", $java.boxInt(1));

Parameters

Name Type
value number

Returns

any


boxLong

boxLong(value): any

Wrap value as a wrapper of java.lang.Long, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type long or java.lang.Long can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Long value)
values.put("key", $java.boxLong(1));

Parameters

Name Type
value number

Returns

any


boxShort

boxShort(value): any

Wrap value as a wrapper of java.lang.Short, which can avoid ambiguous when calling overloaded Java methods, so that method of argument type short or java.lang.Short can be called certainty.

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

const values = new android.content.ContentValues();
// public void put(String key, Short value)
values.put("key", $java.boxShort(1));

Parameters

Name Type
value number

Returns

any


create

create(constructor, args, 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

Name Type Default value Description
constructor any undefined Java class constructor, if constructor is not a Java class constructor, throw TypeError.
args any[] undefined Constructor arguments
threadMode ThreadMode 'current' Thread mode

Returns

Promise<any>


createSync

createSync(constructor, args, 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

Name Type Default value Description
constructor any undefined Java class constructor, if constructor is not a Java class constructor, throw TypeError.
args any[] undefined Constructor arguments
threadMode ThreadMode 'current' Thread mode

Returns

any


defineClass

defineClass(jsClass, options?): Promise<C>

Dynamically generate a Java class from the given JavaScript class. The methods of the parent class can be overridden in the JavaScript class, and implement methods of the Java interface. Usually used to inherit abstract classes or override methods of some classes.

For example, you can inherit android.webkit.MyWebViewClient and override the shouldOverrideUrlLoading method to intercept requests.

This method is implemented by dynamically generating dex, which takes a certain amount of time and requires asynchronous waiting. The dex generated last time can be reused in the second call. The dex file generated by default is located in the .codecache directory of the current directory. You can be specified through cacheDexFile option.

Note that if you just want to implement Java interfaces, you don't need to use this method, you can directly use something like new View.OnClickListener({onClick: () => {}}).

Example

"nodejs";
require('rhino').install();
const $java = $autojs.java;

async function main() {
  const MyWebViewClient = await $java.defineClass(
     class MyWebViewClient extends android.webkit.WebViewClient {
         
         shouldOverrideUrlLoading(webview, url) {
              if (typeof(url) === 'string') {
                  console.log(url);
              }
              return false;
         }
    }
  );
  const client = new MyWebViewClient();
  // ...
}
main().catch(console.error);

给定一个JavaScript类,让它继承自某个Java类,并实现给定的Java接口。将生成一个相应的类,从而可在给定的JavaScript类中覆写Java类的相应方法。

Parameters

Name Type Description
jsClass C JavaScript类
options? DefineClassOptions 选项,用于指定生成类的包名、要实现的Java接口等。

Returns

Promise<C>


findClass

findClass(name): 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 Type Description
name string The full Java class name

Returns

any

Java class


findClassOrNull

findClassOrNull(name): any

Similar to findClass, but return null instead of throwing exception when the class is not found.

See

findClass

Parameters

Name Type Description
name string full java class name

Returns

any

Java class or null


loadDex

loadDex(dexFile): Promise<void>

Load the Dex file asynchronously. After loading, the classes in the Dex will be available in JavaScript.

Example

"nodejs";
const $java = $autojs.java;
require('rhino').install();

async function main() {
   await $java.loadDex('/sdcard/mydex.dex');
   console.log(new com.example.MyClass());
}
main().catch(console.error);

Parameters

Name Type Description
dexFile string the path of dex file

Returns

Promise<void>


loadJar

loadJar(jarFile): Promise<void>

Load the jar file asynchronously. After loading, the classes in the jar will be available in JavaScript.

Example

"nodejs";
const $java = $autojs.java;
require('rhino').install();

async function main() {
   await $java.loadJar('/sdcard/myjar.jar');
   console.log(new com.example.MyClass());
}
main().catch(console.error);

Parameters

Name Type Description
jarFile string the path of dex file

Returns

Promise<void>


setDefaultThreadMode

setDefaultThreadMode(clazz, 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

Name Type Description
clazz any Java class
threadMode ThreadMode Thread mode

Returns

any


setThreadMode

setThreadMode(obj, 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

Name Type Description
obj any Java object
threadMode ThreadMode Thread mode

Returns

any


wrap

wrap<T>(obj, sync?): 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

Name
T

Parameters

Name Type Default value Description
obj T undefined JavaScript object
sync? Boolean false Whether to block the call

Returns

T