[ionic]How does interval work in background

2019-09-08 03:59发布

问题:

I am making interval that run function get wifi info and send to database it's work fine when open app . plugin-background-mode working and show text "Myapp1 running in background" but function doesn't work.

app.js

 .run(function($ionicPlatform, $ionicHistory){
$ionicPlatform.ready(function () { ...

    cordova.plugins.backgroundMode.enable(); //enable  background mode as default 
        cordova.plugins.backgroundMode.setDefaults({ //Modify the display information
        title:  'Myapp1',
        text:   'Myapp1 running in background'
    })
})

controller.js

.controller('DashCtrl',function(....) {

     $scope.$on('$ionicView.enter', function() {


      $scope.getWifiInfo  = function(){...}

         .
         .
         .

         $interval($scope.getWifiInfo, 20000);

         .
         .
         .

  });


});

I tried to put function into

cordova.plugins.backgroundMode.onactivate = function () {}

but still didn't work

回答1:

make the function call at least one time:

.controller('DashCtrl',function($interval,$ionicView) {
 $scope.$on('$ionicView.enter', function() {
 $interval($scope.getWifiInfo, 20000);
 });
 $scope.getWifiInfo  = function(){
 /* Your Code */
}
});


回答2:

ok, finally I found a solution.It's because I installed app that like antivirus and it has battery saving function.after I uninstalled that app,so now myapp work on background.