nav-title | title | description | position |
---|---|---|---|
Accessing APIs |
Accessing APIs |
NativeScript Android Runtime Metadata API Access |
2 |
The Android packages are available in the JavaScript's global context and are the entry point for accessing Android APIs. For example:
- java.lang
- android
- android.view
- etc.
The above is accessed in JavaScript like:
var javaLangPkg = java.lang;
var androidPkg = android;
var androidViewPkg = android.view;
Note: You may not use APIs that are not present in the metadata. By default the Android Runtime comes with prebuilt metadata for Android API level 17, which is also the minimum required SDK. If needed, you may use the generator tool to build metadata against higher API level.
The JavaScript objects that lie behind the Android APIs are called Proxies. There are two types of proxies:
Provides access to the classes, interfaces, constants and enumerations within each package.
Represents a thin wrapper over a class or an interface and provides access to its methods and fields. From a JavaScript perspective this type of proxy may be considered as a constructor function. For example android.view.View
is a class proxy.
Class proxies may be instantiated much like in Android:
var file = new java.io.File("/path/to/myfile.txt");
The result of the call will create a native java.io.File instance on the Android side and a special hollow Object on the JavaScript side. This special object knows how to invoke methods and access fields on the corresponding native instance. For example we may retrieve the path value of the above created File
like:
var path = file.getPath();
Using a Class Proxy you may access the static methods/fields exposed by a type:
var filePathDelimiter = java.io.File.separator;
var pathSeparator = java.io.File.pathSeparator;