Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Use the following code in case you need to check somewhere in your code the platform you are running against:

Basic usage of the `application` module in a component:
Basic usage of the `application` module in a component to apply different code on Android and iOS
<snippet id='app-check-target-code'/>
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ export class AppUsingAndroidExampleComponent {
console.log("We are running on Android device!");
this.isItemVisible = true;

// >> app-class-properties
// import { android as androidApp } from "tns-core-modules/application";
let isPaused = androidApp.paused; // e.g. false
let packageName = androidApp.packageName; // The package ID e.g. org.nativescript.nativescriptsdkexamplesng
let nativeApp = androidApp.nativeApp; // The native APplication reference
let foregroundActivity = androidApp.foregroundActivity; // The current Activity reference
let currentContext = androidApp.currentContext; // The current Android context
let context = androidApp.context; console.log(context); // The current Android context
// << app-class-properties

// >> app-android-dirs-code
this.fileList = [];
this.appContext = androidApp.context;

// https://developer.android.com/reference/android/content/Context.html#getFilesDir()
this.filesDir = this.appContext.getFilesDir();

// https://developer.android.com/reference/android/content/Context.html#getCacheDir()
this.cacheDir = this.appContext.getCacheDir();

let files = this.appContext.fileList();
Expand All @@ -37,8 +51,9 @@ export class AppUsingAndroidExampleComponent {
this.batteryLife = "0";
let that = this;

// Broadcast Receiver https://developer.android.com/reference/android/content/BroadcastReceiver
androidApp.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
function onReceiveCallback(androidContext: android.content.Context, intent: android.content.Intent) {
let level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
let scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
let percent = (level / scale) * 100.0;
Expand All @@ -51,6 +66,12 @@ export class AppUsingAndroidExampleComponent {
this.isItemVisible = false;
}
}

unregister() {
// >> app-android-broadcast-unregister-code
androidApp.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
// << app-android-broadcast-unregister-code
}
}

class File {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
Using the Android Application context to get the absolute path to Files directory:
### Application Module Android Specific Properties

The application module provides a number of Android specific properties to access the Android app, context and activities.
<snippet id='app-class-properties'/>

### Using the Android Application Context

In the extended example below, the Android context is used to get the absolute path to Files directory.
We are accessing methods from the Android SDK (like `getCacheDir` and `getFilesDir`)
<snippet id='app-android-dirs-code'/>

Registering a Broadcast Receiver (Android)
<snippet id='app-android-broadcast-code'/>
### Registering a Broadcast Receiver (Android)

NativeScript can send/receive messages and system information though broadcast receivers.
More on broadcast receivers in Android [here](https://developer.android.com/guide/components/broadcasts).
<snippet id='app-android-broadcast-code'/>

### Unregistering a Broadcast Receiver (Android)
<snippet id='app-android-broadcast-unregister-code'/>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export class AppUsingIosExampleComponent {
constructor() {

if (iosApp) {
// >> app-class-properties-ios
// import { ios as iosApp } from "tns-core-modules/application";

// https://developer.apple.com/documentation/uikit/uiapplicationdelegate?language=objc
let delegate = iosApp.delegate; // the iOS application delegate

let nativeApp = iosApp.nativeApp; // The native iOS app

// https://developer.apple.com/documentation/uikit/uiwindow/1621581-rootviewcontroller?language=objc
let rootController = iosApp.rootController; // the iOS rootViewController

let window = iosApp.window; // UIWindow
// << app-class-properties-ios

this.isItemVisible = true;
this.batteryLife = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Adding a Notification Observer (iOS)
### Application Module iOS Specific Properties

The application module provides a number of iOS specific properties to access the iOS app, delegate and root view controller, etc..
<snippet id='app-class-properties-ios'/>

### Adding a Notification Observer (iOS)
<snippet id='app-ios-observer-code'/>

Removing a notification observer
### Removing a notification observer
<snippet id='app-ios-observer-remove-code'/>