How to keep application running when the screen is off


#1

Working on a countdown like application, everything works fine when I test it on my android devices until the screen is turned off.

I started using cordova-plugin-background-mode plugin, and it solved the problem when I switch to another application or put my application in the background while the screen is still on, but it didn’t help with the problem when the screen is off, as soon as the screen is off, my application stops counting down, where I have a setInterval function in progress.

Is there any way to keep application running in the background even if the screen is off?

Or I can change my approach and have the application wake up every 1 minute and update its countdown state, is this possible with ionic plugins or cordova plugins?

Thanks
Deniz KALFA


#2

You might want to use the Ionic platform events to help with this. For example, you can detect when the app is paused. Save that time to a service. Then, detect when the resume event occurs. You can find the elapsed time and then update your timer.

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

  $ionicPlatform.on('resume', function() {
    // do something update your interval
  }

  $ionicPlatform.on('pause', function() {
    // do something here to store the timestamp
  }

});

#3

Great answer, thanks.


#4

Follow up question:

What if countdown time ends while the app is paused? Ideally, At the end of countdown time, user is notified by sound or vibration, but if user does not turn the screen on, user won’t be notified since app is not resumed yet.

Any suggestion for this scenario? Is there a mechanism to wake up the device in certain time intervals, e.g. every 2-3 minutes?


#5

Hi all,

Any thoughts on my previous question?

We kind of stuck with this problem and our spiking period is ending soon that will let us determine which technology to use for our future mobile apps.

Any help will be much appreciated.

Thanks


#6

Take a look at this:

http://forum.ionicframework.com/t/how-to-show-up-a-modal-on-phone-when-the-user-is-not-viewing-the-app/14809


#7

@Calendee I had a small doubt with regards to your suggestion. You said, “Save that time to a service”. My question is, how do i access the current time from the onPause method?


#8

In the onPause event, you can just do something like this:

var pausedTime;

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

  $ionicPlatform.on('resume', function() {
    console.log('Paused interval = ', Date.now() - pausedTime);
    // Perhaps fetched the paused time from the service.
  }

  $ionicPlatform.on('pause', function() {
    pausedTime = Date.now();
    // Perhaps save this paused time to a service
  }

});

#9

@Calendee I realised that my timer was still running even when the on pause was triggered. The only thing i then need to handle was when the app was forcefully closed by the OS. So I did that by using localStorage and restoring the data when the app launched. Thanx for the reply! :smiley:


#10

Hi all,

I’m working on a youtube app. But I have a problem on pause or stop when pressed screen lock button.

In my app, youtube videos continue playing in the background when pressed screen lock button.

But playing videos paused when I press home button.

Any help would be appreciated.

Thanks,