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:
@@ -1,3 +1,4 @@
|
||||
import application = require("application");
|
||||
|
||||
application.mainModule = "main-page";
|
||||
application.start();
|
||||
|
||||
@@ -1,16 +1,57 @@
|
||||
import connectivity = require("connectivity");
|
||||
import observable = require("data/observable");
|
||||
import pages = require("ui/page");
|
||||
import connectivity = require("connectivity");
|
||||
import labelModule = require("ui/label");
|
||||
import color = require("color");
|
||||
|
||||
export function onGetConnectionType(args) {
|
||||
var infoLabel: labelModule.Label;
|
||||
export function onPageLoaded(args: observable.EventData) {
|
||||
var page = <pages.Page>args.object;
|
||||
infoLabel = page.getViewById<labelModule.Label>("infoLabel");
|
||||
}
|
||||
|
||||
export function onGetConnectionType(args: observable.EventData) {
|
||||
var connectionType = connectivity.getConnectionType();
|
||||
updateInfoLabel(connectionType);
|
||||
}
|
||||
|
||||
export function onStartMonitoring(args: observable.EventData) {
|
||||
onGetConnectionType(null);
|
||||
connectivity.starMonitoring(onConnectionTypeChanged);
|
||||
}
|
||||
|
||||
export function onStopMonitoring(args: observable.EventData) {
|
||||
connectivity.stopMonitoring();
|
||||
}
|
||||
|
||||
function updateInfoLabel(connectionType: number) {
|
||||
switch (connectionType) {
|
||||
case connectivity.connectionType.none:
|
||||
args.object.text = "No connection";
|
||||
infoLabel.text = "None";
|
||||
infoLabel.backgroundColor = new color.Color("Red");
|
||||
break;
|
||||
case connectivity.connectionType.wifi:
|
||||
args.object.text = "WiFi connection";
|
||||
infoLabel.text = "WiFi";
|
||||
infoLabel.backgroundColor = new color.Color("Green");
|
||||
break;
|
||||
case connectivity.connectionType.mobile:
|
||||
args.object.text = "Mobile connection";
|
||||
infoLabel.text = "Mobile";
|
||||
infoLabel.backgroundColor = new color.Color("Yellow");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
updateInfoLabel(newConnectionType);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
<Page xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
|
||||
<StackLayout>
|
||||
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30"/>
|
||||
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
|
||||
<StackLayout id="stackLayout">
|
||||
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
|
||||
<Button text="Start Monitoring" tap="onStartMonitoring" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
|
||||
<Button text="Stop Monitoring" tap="onStopMonitoring" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
|
||||
<Label id="infoLabel" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center" width="200" height="200"/>
|
||||
</StackLayout>
|
||||
</Page>
|
||||
|
||||
14
apps/notifications-demo/app.ts
Normal file
14
apps/notifications-demo/app.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import application = require("application");
|
||||
|
||||
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();
|
||||
39
apps/notifications-demo/main-page.ts
Normal file
39
apps/notifications-demo/main-page.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import application = require("application");
|
||||
import observable = require("data/observable");
|
||||
import pages = require("ui/page");
|
||||
import labelModule = require("ui/label");
|
||||
|
||||
var batteryLabel: labelModule.Label;
|
||||
var registered = false;
|
||||
export function onPageLoaded(args: observable.EventData) {
|
||||
var page = <pages.Page>args.object;
|
||||
batteryLabel = page.getViewById<labelModule.Label>("batteryLabel");
|
||||
|
||||
if (registered) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (application.android) {
|
||||
application.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;
|
||||
var message = "Battery: " + percent + "%";
|
||||
console.log(message);
|
||||
batteryLabel.text = message;
|
||||
});
|
||||
}
|
||||
else {
|
||||
var onReceiveCallback = function onReceiveCallback(notification: NSNotification) {
|
||||
var percent = UIDevice.currentDevice().batteryLevel * 100;
|
||||
var message = "Battery: " + percent + "%";
|
||||
console.log(message);
|
||||
batteryLabel.text = message;
|
||||
}
|
||||
UIDevice.currentDevice().batteryMonitoringEnabled = true;
|
||||
onReceiveCallback(null);
|
||||
application.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification, onReceiveCallback);
|
||||
}
|
||||
registered = true;
|
||||
}
|
||||
5
apps/notifications-demo/main-page.xml
Normal file
5
apps/notifications-demo/main-page.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
|
||||
<StackLayout>
|
||||
<Label id="batteryLabel" text="Battery" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
|
||||
</StackLayout>
|
||||
</Page>
|
||||
2
apps/notifications-demo/package.json
Normal file
2
apps/notifications-demo/package.json
Normal file
@@ -0,0 +1,2 @@
|
||||
{ "name" : "notifications-demo",
|
||||
"main" : "app.js" }
|
||||
@@ -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