Skip to content

Commit cec3355

Browse files
authored
Merge pull request #355 from NativeScript/niliev/receiver
docs: fix broadcast receiver example + minor article improvements
2 parents 2d9e9dc + c0cf81b commit cec3355

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
Use the following code in case you need to check somewhere in your code the platform you are running against:
2-
3-
Basic usage of the `application` module in a component:
1+
Basic usage of the `application` module in a component to apply different code on Android and iOS
42
<snippet id='app-check-target-code'/>

app/ng-framework-modules-category/application/app-using-android-specifics/app-using-android-specifics.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,24 @@ export class AppUsingAndroidExampleComponent {
2020
console.log("We are running on Android device!");
2121
this.isItemVisible = true;
2222

23+
// >> app-class-properties
24+
// import { android as androidApp } from "tns-core-modules/application";
25+
let isPaused = androidApp.paused; // e.g. false
26+
let packageName = androidApp.packageName; // The package ID e.g. org.nativescript.nativescriptsdkexamplesng
27+
let nativeApp = androidApp.nativeApp; // The native APplication reference
28+
let foregroundActivity = androidApp.foregroundActivity; // The current Activity reference
29+
let currentContext = androidApp.currentContext; // The current Android context
30+
let context = androidApp.context; console.log(context); // The current Android context
31+
// << app-class-properties
32+
2333
// >> app-android-dirs-code
2434
this.fileList = [];
2535
this.appContext = androidApp.context;
36+
37+
// https://developer.android.com/reference/android/content/Context.html#getFilesDir()
2638
this.filesDir = this.appContext.getFilesDir();
39+
40+
// https://developer.android.com/reference/android/content/Context.html#getCacheDir()
2741
this.cacheDir = this.appContext.getCacheDir();
2842

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

54+
// Broadcast Receiver https://developer.android.com/reference/android/content/BroadcastReceiver
4055
androidApp.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
41-
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
56+
function onReceiveCallback(androidContext: android.content.Context, intent: android.content.Intent) {
4257
let level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
4358
let scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
4459
let percent = (level / scale) * 100.0;
@@ -51,6 +66,12 @@ export class AppUsingAndroidExampleComponent {
5166
this.isItemVisible = false;
5267
}
5368
}
69+
70+
unregister() {
71+
// >> app-android-broadcast-unregister-code
72+
androidApp.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
73+
// << app-android-broadcast-unregister-code
74+
}
5475
}
5576

5677
class File {
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
Using the Android Application context to get the absolute path to Files directory:
1+
### Application Module Android Specific Properties
2+
3+
The application module provides a number of Android specific properties to access the Android app, context and activities.
4+
<snippet id='app-class-properties'/>
5+
6+
### Using the Android Application Context
7+
8+
In the extended example below, the Android context is used to get the absolute path to Files directory.
9+
We are accessing methods from the Android SDK (like `getCacheDir` and `getFilesDir`)
210
<snippet id='app-android-dirs-code'/>
311

4-
Registering a Broadcast Receiver (Android)
5-
<snippet id='app-android-broadcast-code'/>
12+
### Registering a Broadcast Receiver (Android)
13+
14+
NativeScript can send/receive messages and system information though broadcast receivers.
15+
More on broadcast receivers in Android [here](https://developer.android.com/guide/components/broadcasts).
16+
<snippet id='app-android-broadcast-code'/>
17+
18+
### Unregistering a Broadcast Receiver (Android)
19+
<snippet id='app-android-broadcast-unregister-code'/>

app/ng-framework-modules-category/application/app-using-ios-specifics/app-using-ios-specifics.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export class AppUsingIosExampleComponent {
1515
constructor() {
1616

1717
if (iosApp) {
18+
// >> app-class-properties-ios
19+
// import { ios as iosApp } from "tns-core-modules/application";
20+
21+
// https://developer.apple.com/documentation/uikit/uiapplicationdelegate?language=objc
22+
let delegate = iosApp.delegate; // the iOS application delegate
23+
24+
let nativeApp = iosApp.nativeApp; // The native iOS app
25+
26+
// https://developer.apple.com/documentation/uikit/uiwindow/1621581-rootviewcontroller?language=objc
27+
let rootController = iosApp.rootController; // the iOS rootViewController
28+
29+
let window = iosApp.window; // UIWindow
30+
// << app-class-properties-ios
1831

1932
this.isItemVisible = true;
2033
this.batteryLife = 0;
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
Adding a Notification Observer (iOS)
1+
### Application Module iOS Specific Properties
2+
3+
The application module provides a number of iOS specific properties to access the iOS app, delegate and root view controller, etc..
4+
<snippet id='app-class-properties-ios'/>
5+
6+
### Adding a Notification Observer (iOS)
27
<snippet id='app-ios-observer-code'/>
38

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

0 commit comments

Comments
 (0)