Is there any way to check whether the app is running in foreground or background in ionic/cordova/phonegap, I need to use it on android and ios, thanks a lot
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
A small service for Ionic based on Sithys answer:
For Ionic 2 and Ionic 3 the solution is:
"Is there any way to check whether the app is running in foreground or background?"
Yes.
1) When an app becomes inactive (runs in the background) Cordova fires the
pause
event, and when an app becomes active again (brought to the foreground) Cordova fires theresume
event.2) From these events, one can use a variable to store the state as "foreground" or "background".
Use the two Events "
Pause
" and "Resume
". You will find all Events here in the Apache Cordova Events Documentation.Event - Pause:
Event - Resume
You can add an Eventlistener for that into your code. For those two Events that would be:
Pause - Quick Example
Or Full Example like this:
Resume - Quick Example
Or Full Example like this
Try that out and let me know, if you need further help!
Using an angular abstraction of
ionic.Platform
Refer to ionic v1 documentation for $ionicPlatform