[ionic]How does interval work in background

2019-09-08 03:49发布

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

2条回答
何必那么认真
2楼-- · 2019-09-08 04:22

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.

查看更多
够拽才男人
3楼-- · 2019-09-08 04:27

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 */
}
});
查看更多
登录 后发表回答