Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 2.39 KB

accessing-packages.md

File metadata and controls

58 lines (43 loc) · 2.39 KB
nav-title title description position
Accessing APIs
Accessing APIs
NativeScript Android Runtime Metadata API Access
2

Packages

The Android packages are available in the JavaScript's global context and are the entry point for accessing Android APIs. For example:

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.

Proxies

The JavaScript objects that lie behind the Android APIs are called Proxies. There are two types of proxies:

Package Proxy

Provides access to the classes, interfaces, constants and enumerations within each package.

Class Proxy

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.

Instance Methods/Fields

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();

Static Methods/Fields

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;

See Also