mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Added connectivity module.
This commit is contained in:
31
connectivity/connectivity.android.ts
Normal file
31
connectivity/connectivity.android.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import appModule = require("application");
|
||||
import common = require("connectivity/connectivity-common");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
var WIFI = "WIFI";
|
||||
var MOBILE = "MOBILE";
|
||||
|
||||
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
||||
if (!appModule.android || !appModule.android.context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
var activeNetworkInfo = getActiveNetworkInfo();
|
||||
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var connectionType = activeNetworkInfo.getTypeName();
|
||||
switch (connectionType) {
|
||||
case WIFI:
|
||||
return common.connectionType.wifi;
|
||||
case MOBILE:
|
||||
return common.connectionType.mobile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user