mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Resolved Issue #451: Improve the Network Stack
Resolved Issue #473: Add support for Notification Observers (iOS) and Broadcast Receivers (Android)
This commit is contained in:
@@ -26,7 +26,26 @@ var dir = context.getFilesDir();
|
||||
// ### Tracking the current Activity
|
||||
// ``` JavaScript
|
||||
if (androidApp.foregroundActivity === androidApp.startActivity) {
|
||||
console.log("We are currently in the main (start) activity of the application");
|
||||
////console.log("We are currently in the main (start) activity of the application");
|
||||
}
|
||||
// ```
|
||||
// </snippet>
|
||||
// <snippet module="application" title="application">
|
||||
// ### Registering a Broadcast Receiver (Android)
|
||||
// ``` JavaScript
|
||||
//// Register the broadcast receiver
|
||||
if (app.android) {
|
||||
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
|
||||
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 (app.android) {
|
||||
app.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
|
||||
}
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
@@ -1 +1,27 @@
|
||||
|
||||
/* tslint:disable:no-unused-variable */
|
||||
import app = require("application");
|
||||
import TKUnit = require("./TKUnit");
|
||||
import commonTests = require("./application-tests-common");
|
||||
|
||||
// merge the exports of the application_common file with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(commonTests, exports);
|
||||
|
||||
// <snippet module="application" title="application">
|
||||
// ### Adding a Notification Observer (iOS)
|
||||
// ``` JavaScript
|
||||
//// Add the notification observer
|
||||
if (app.ios) {
|
||||
app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
|
||||
function onReceiveCallback(notification: NSNotification) {
|
||||
var percent = UIDevice.currentDevice().batteryLevel * 100;
|
||||
var message = "Battery: " + percent + "%";
|
||||
////console.log(message);
|
||||
});
|
||||
}
|
||||
//// When no longer needed, remove the notification observer
|
||||
if (app.ios) {
|
||||
app.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
|
||||
}
|
||||
// ```
|
||||
// </snippet>
|
||||
@@ -24,4 +24,27 @@ export var test_DummyTestForSnippetOnly0 = function () {
|
||||
}
|
||||
// ```
|
||||
// </snippet>
|
||||
}
|
||||
|
||||
export var test_DummyTestForSnippetOnly1 = function () {
|
||||
// <snippet module="connectivity" title="connectivity">
|
||||
// ### Monitoring connection type.
|
||||
// ``` JavaScript
|
||||
connectivity.starMonitoring(function onConnectionTypeChanged(newConnectionType: number) {
|
||||
switch (newConnectionType) {
|
||||
case connectivity.connectionType.none:
|
||||
////console.log("Connection type changed to none.");
|
||||
break;
|
||||
case connectivity.connectionType.wifi:
|
||||
////console.log("Connection type changed to WiFi.");
|
||||
break;
|
||||
case connectivity.connectionType.mobile:
|
||||
////console.log("Connection type changed to mobile.");
|
||||
break;
|
||||
}
|
||||
});
|
||||
////...
|
||||
connectivity.stopMonitoring();
|
||||
// ```
|
||||
// </snippet>
|
||||
}
|
||||
Reference in New Issue
Block a user