Pause an Android App with Phonegap

2019-01-23 16:59发布

问题:

Is there any way to programmatically pause an Android app in Phonegap? I would like to mimic the behavior that occurs when you hit the HOME button. I've already had to overwrite the back button handler using this, and while in most cases I want it to do my action, when in a particular state the user would expect the app to minimize, and I want to replicate this behavior.

Keep in mind, on Android this is not the same as closing the app. That is quite easy to do with device.exitApp(); but I would like it to remember its state and keep running in the background. Especially if there's still an asynchronous job being done in the background.

Is there a feature in Phonegap to achieve this?

Possible duplicate of Manually pause an application in Android Phonegap, but I couldn't find some of the tools the OP mentioned there such as navigator, so I was nervious to totally edit and rewrite their post

回答1:

The simple answer appears to be: no.

However, for anyone else that comes down this path, its not impossible. It's just that there isn't a feature of Phonegap to do it for you.

The Android equivalent of "sleeping an app" is actually just opening another intent. Specifically, opening the "Home" intent would sleep the running app and bring you back to the home screen. But as far as I can tell from asking around and scoping the docs, Phonegap doesn't have a direct way of opening intents.

What you (supposedly) can do is one of two things:

  • This plugin is supposed to be promising

  • Call the Java code that does it yourself using the means described here

Mind you, as of right now I've decided to not go any further with this, so I make no promises about either of those means, having not attempted them myself.

I invite anyone else who decides to pursue this further to update their experience here.