The Application module provides abstraction over the platform-specific Application implementations. It is the main BCL module and is required for other BCL modules to work properly. The default bootstrap.js implementation for each platform loads and initializes this module.

import * as app from "tns-core-modules/application";
import * as platform from "tns-core-modules/platform";

The pre-required app module is used throughout the following code snippets.

Checking the target platform

Use the following code in case you need to check somewhere in your code the platform you are running against:

if ( {
    console.log("We are running on Android device!");
} else if (app.ios) {
    console.log("We are running on iOS device");

Using the Android-specific implementation

Accessing the Android-specific object instance (will be undefined if running on iOS)

var androidApp =;

Using the Android Application context

var context =;
//// get the Files (Documents) folder (directory)
var dir = context.getFilesDir();

Tracking the current Activity

if (androidApp.foregroundActivity === androidApp.startActivity) {
    ////console.log("We are currently in the main (start) activity of the application");

Registering a Broadcast Receiver (Android)

//// Register the broadcast receiver
if ( {,
        function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
            var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
            var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
            var percent = (level / scale) * 100.0;
            ////console.log("Battery: " + percent + "%");
//// When no longer needed, unregister the broadcast receiver
if ( {;

Adding a Notification Observer (iOS)

//// Add the notification observer
if (app.ios) {
    var observer = app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
        function onReceiveCallback(notification: NSNotification) {
            var percent = getter(UIDevice, UIDevice.currentDevice).batteryLevel * 100;
            var message = "Battery: " + percent + "%";
//// When no longer needed, remove the notification observer
if (app.ios) {
    app.ios.removeNotificationObserver(observer, UIDeviceBatteryLevelDidChangeNotification);

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.