首页 > 代码库 > $cordovaNetwork 使用

$cordovaNetwork 使用

1 .安装插件

  直接安装:

  cordova plugin add cordova-plugin-network-information

 下载到本地安装:

 https://github.com/apache/cordova-plugin-network-information

 

2.代码使用

 

  .controller(‘ContentCtrl‘, function ($scope, $stateParams, $ionicLoading,ENV,$cordovaNetwork,$ionicPopup) {    $scope.playVideo= function (videosrc) {      var type = $cordovaNetwork.getNetwork();      if(type==Connection.WIFI){        if(videosrc!=""){          VideoPlayer.play(ENV.imgUrl+videosrc);        }      }      else{        $scope.showConfirm = function() {          var confirmPopup = $ionicPopup.confirm({            title: ‘播放提示‘,            template: ‘现在不在WiFi环境下,是否使用4G流量进行播放!‘          });          confirmPopup.then(function(res) {            if(res) {              if(videosrc!=""){                VideoPlayer.play(ENV.imgUrl+videosrc);              }            }          });        };      }    }  })

3.其它

// 监听手机网络在线事件
$rootScope.$on(‘$cordovaNetwork:online‘, function(event, networkState){
var onlineState = networkState;
})

// 监听手机网络离线事件
$rootScope.$on(‘$cordovaNetwork:offline‘, function(event, networkState){
var offlineState = networkState;
})

var type = $cordovaNetwork.getNetwork();

type 类型:

Connection.UNKNOWN

Connection.ETHERNET

Connection.WIFI WiFi

Connection.CELL_2G

Connection.CELL_3G

Connection.CELL_4G

Connection.CELL

Connection.NONE

以上方法一般在run 的$ionicPlatform.ready 里面处理

 

$cordovaNetwork 使用