mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
5
application/application.d.ts
vendored
5
application/application.d.ts
vendored
@ -512,14 +512,15 @@ declare module "application" {
|
|||||||
* @param notificationName A string containing the name of the notification.
|
* @param notificationName A string containing the name of the notification.
|
||||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||||
*/
|
*/
|
||||||
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): void;
|
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the observer for the specified notification from the default notification center.
|
* Removes the observer for the specified notification from the default notification center.
|
||||||
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
|
||||||
|
* @param observer The observer that was returned from the addNotificationObserver method.
|
||||||
* @param notificationName A string containing the name of the notification.
|
* @param notificationName A string containing the name of the notification.
|
||||||
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
|
||||||
*/
|
*/
|
||||||
removeNotificationObserver(notificationName: string): void;
|
removeNotificationObserver(observer: any, notificationName: string): void;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -37,14 +37,14 @@ class Window extends UIWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NotificationReceiver extends NSObject {
|
class NotificationObserver extends NSObject {
|
||||||
private _onReceiveCallback: (notification: NSNotification) => void;
|
private _onReceiveCallback: (notification: NSNotification) => void;
|
||||||
|
|
||||||
static new(): NotificationReceiver {
|
static new(): NotificationObserver {
|
||||||
return <NotificationReceiver>super.new();
|
return <NotificationObserver>super.new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationReceiver {
|
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
||||||
this._onReceiveCallback = onReceiveCallback;
|
this._onReceiveCallback = onReceiveCallback;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -62,11 +62,12 @@ class IOSApplication implements definition.iOSApplication {
|
|||||||
public rootController: any;
|
public rootController: any;
|
||||||
|
|
||||||
private _delegate: typeof UIApplicationDelegate;
|
private _delegate: typeof UIApplicationDelegate;
|
||||||
private _registeredObservers = {};
|
|
||||||
private _currentOrientation = UIDevice.currentDevice().orientation;
|
private _currentOrientation = UIDevice.currentDevice().orientation;
|
||||||
private _window: Window;
|
private _window: Window;
|
||||||
|
private _observers: Array<NotificationObserver>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this._observers = new Array<NotificationObserver>();
|
||||||
this.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, this.didFinishLaunchingWithOptions.bind(this));
|
this.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, this.didFinishLaunchingWithOptions.bind(this));
|
||||||
this.addNotificationObserver(UIApplicationDidBecomeActiveNotification, this.didBecomeActive.bind(this));
|
this.addNotificationObserver(UIApplicationDidBecomeActiveNotification, this.didBecomeActive.bind(this));
|
||||||
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
|
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
|
||||||
@ -88,15 +89,17 @@ class IOSApplication implements definition.iOSApplication {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void) {
|
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
||||||
var observer = NotificationReceiver.new().initWithCallback(onReceiveCallback);
|
var observer = NotificationObserver.new().initWithCallback(onReceiveCallback);
|
||||||
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
||||||
this._registeredObservers[notificationName] = observer;
|
this._observers.push(observer);
|
||||||
|
return observer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeNotificationObserver(notificationName: string) {
|
public removeNotificationObserver(observer: any, notificationName: string) {
|
||||||
var observer = this._registeredObservers[notificationName];
|
var index = this._observers.indexOf(observer);
|
||||||
if (observer) {
|
if (index >= 0) {
|
||||||
|
this._observers.splice(index, 1);
|
||||||
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,4 @@
|
|||||||
|
|
||||||
application.mainModule = "main-page";
|
application.mainModule = "main-page";
|
||||||
|
|
||||||
application.on(application.exitEvent, () => {
|
|
||||||
if (application.android) {
|
|
||||||
application.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
application.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
application.start();
|
application.start();
|
||||||
|
@ -5,6 +5,16 @@ import labelModule = require("ui/label");
|
|||||||
|
|
||||||
var batteryLabel: labelModule.Label;
|
var batteryLabel: labelModule.Label;
|
||||||
var registered = false;
|
var registered = false;
|
||||||
|
var batteryObserver: any;
|
||||||
|
|
||||||
|
application.on(application.exitEvent, () => {
|
||||||
|
if (application.android) {
|
||||||
|
application.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
application.ios.removeNotificationObserver(batteryObserver, UIDeviceBatteryLevelDidChangeNotification);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export function onPageLoaded(args: observable.EventData) {
|
export function onPageLoaded(args: observable.EventData) {
|
||||||
var page = <pages.Page>args.object;
|
var page = <pages.Page>args.object;
|
||||||
@ -34,7 +44,7 @@ export function onPageLoaded(args: observable.EventData) {
|
|||||||
}
|
}
|
||||||
UIDevice.currentDevice().batteryMonitoringEnabled = true;
|
UIDevice.currentDevice().batteryMonitoringEnabled = true;
|
||||||
onReceiveCallback(null);
|
onReceiveCallback(null);
|
||||||
application.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification, onReceiveCallback);
|
batteryObserver = application.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification, onReceiveCallback);
|
||||||
}
|
}
|
||||||
registered = true;
|
registered = true;
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,4 @@
|
|||||||
|
|
||||||
application.mainModule = "main-page";
|
application.mainModule = "main-page";
|
||||||
|
|
||||||
application.on(application.exitEvent, () => {
|
|
||||||
if (application.android) {
|
|
||||||
application.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
application.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
application.start();
|
application.start();
|
||||||
|
@ -10,7 +10,7 @@ global.moduleMerge(commonTests, exports);
|
|||||||
// ``` JavaScript
|
// ``` JavaScript
|
||||||
//// Add the notification observer
|
//// Add the notification observer
|
||||||
if (app.ios) {
|
if (app.ios) {
|
||||||
app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
|
var observer = app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
|
||||||
function onReceiveCallback(notification: NSNotification) {
|
function onReceiveCallback(notification: NSNotification) {
|
||||||
var percent = UIDevice.currentDevice().batteryLevel * 100;
|
var percent = UIDevice.currentDevice().batteryLevel * 100;
|
||||||
var message = "Battery: " + percent + "%";
|
var message = "Battery: " + percent + "%";
|
||||||
@ -19,7 +19,7 @@ if (app.ios) {
|
|||||||
}
|
}
|
||||||
//// When no longer needed, remove the notification observer
|
//// When no longer needed, remove the notification observer
|
||||||
if (app.ios) {
|
if (app.ios) {
|
||||||
app.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
|
app.ios.removeNotificationObserver(observer, UIDeviceBatteryLevelDidChangeNotification);
|
||||||
}
|
}
|
||||||
// ```
|
// ```
|
||||||
// </snippet>
|
// </snippet>
|
Reference in New Issue
Block a user