Using cordovaNetwork plugin, we can get network information such as online or offline. Furthermore, we can get the connection type using getNetwork() function.
Connection Type Description
Connection.UNKNOWN Unknown connection
Connection.ETHERNET Ethernet connection
Connection.WIFI WiFi connection
Connection.CELL_2G Cell 2G connection
Connection.CELL_3G Cell 3G connection
Connection.CELL_4G Cell 4G connection
Connection.CELL Cell generic connection
Connection.NONE No network connection
Start Ionic project using.
ionic start network blank
Then go to network folder.
cd network
Then add cordovaNetwork plugin.
cordova plugin add cordova-plugin-network-information
Download ngCordova using
https://github.com/driftyco/ng-cordova/archive/master.zip
Include ngCordova js file in index.html.
<script type="text/javascript" src="js/ng-cordova.min.js" ></script>
Add controller
<body ng-app="starter" ng-controller="netctrl" >
Add ngCordova to app.js.
angular.module('starter', ['ionic','ngCordova'])
Create an netctrl controller to monitor the network status.
controller('netctrl',function($scope,$ionicPlatform,$rootScope, $cordovaNetwork){
document.addEventListener("deviceready", function () {
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetwork.isOffline()
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
alert("online");
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
var offlineState = networkState;
alert("offline.Please enable data");
})
}, false);
})
That’s all. To generate apk.
ionic platform add android
ionic build android
Tested Device
Samsung J7 Android Lolipop