Cordova runs Android APP in the background

Cordova runs Android APP in the background

Github: github.com/katzer/cord...

Installation

Project root directory installation

cordova plugin add cordova-plugin-background-mode

Usage

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() ( cordova.apache.org/docs/en/9.x... )
  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 () {
      console.log('backkeydown')
      document.removeEventListener("backbutton", this.onBackKeyDown, false)
      document.addEventListener("backbutton", this.exitApp, false)
    },
    exitApp () {
      console.log('exit app')
     //navigator.app.exitApp()// APP
      window.cordova.plugins.backgroundMode.moveToBackground()// APP
    }    
  }
 

Version

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",