Cordova runs Android APP in the background

Cordova runs Android APP in the background



Project root directory installation

cordova plugin add cordova-plugin-background-mode


For example: Set the return button on the home page (Home.vue) of the Vue project, and the APP will automatically run in the background after clicking Return

  1. Set the back button listener event in Home.vue created() ( )
  created () {
    document.addEventListener("backbutton", this.onBackKeyDown, false)
    document.addEventListener('deviceready', function () {
     //cordova.plugins.backgroundMode is now available
    }, false)
  1. Set onBackKeyDown()function, point the return key twice to exit the APP, in exitApp()setting the background APP function
  methods: {
    onBackKeyDown () {
      document.removeEventListener("backbutton", this.onBackKeyDown, false)
      document.addEventListener("backbutton", this.exitApp, false)
    exitApp () {
      console.log('exit app')
     // APP
      window.cordova.plugins.backgroundMode.moveToBackground()// APP


cordova package.json

"cordova-android": "8.1.0",
"cordova-plugin-background-mode": "^0.7.3",
"cordova-plugin-device": "^2.0.3",

vue package.json

"vue": "^2.6.10",
"@vue/cli-plugin-babel": "^3.11.0",
"@vue/cli-plugin-eslint": "^3.11.0",
"@vue/cli-plugin-pwa": "^3.11.0",
"@vue/cli-service": "^3.11.0",