ngcordova 监控网络制式改变

ngcordova 监控网络制式改变

keywords

cordova,phonegap,ionic,network,网络制式

API参考

http://ngcordova.com/docs/plugins/network/

安装

cordova plugin add cordova-plugin-network-information

使用场景

适用于但网络制式发生变化,需要动态改变APP运行参数,例如提醒网络切换,图片的加载,图片质量或者服务器API地址的切换。

例子

使用

module.controller('MyCtrl', function($rootScope, $cordovaNetwork,$ionicLoading) {
  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 type = $cordovaNetwork.getNetwork()
      var onlineState = networkState;
      if (type == Connection.WIFI) {
        //TODO something
      }else if (type == Connection.CELL_4G || type == Connection.CELL_3G){
        //TODO something
      }
    })

    // listen for Offline event
    $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
      var offlineState = networkState;
      $ionicLoading.show({
          template: '网络异常,不能连接到服务器!'
      });
    })

  }, false);
});

Connection类型

Connection.UNKNOWN
Connection.ETHERNET
Connection.WIFI WiFi
Connection.CELL_2G
Connection.CELL_3G
Connection.CELL_4G
Connection.CELL
Connection.NONE

你可能感兴趣的:(ngcordova 监控网络制式改变)