Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { BackgroundMode } from '@ionic-native/background-mode';
- import { Platform } from 'ionic-angular';
- import { PowerManagement } from '@ionic-native/power-management';
- @Injectable()
- export class BackgroundProvider {
- private onPauseSubscription = undefined;
- constructor(private backgroundMode: BackgroundMode,
- private platform: Platform,
- private powerManagement: PowerManagement) { }
- public paused() {
- // push us to the background in this case so that we're not driving the screen...
- this.backgroundMode.moveToBackground();
- }
- public enableBackground(forceBackgroundOnPause) {
- // https://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012
- this.backgroundMode.setDefaults({
- title: "Radios App",
- hidden: false,
- silent: true,
- resume: false,
- });
- if (this.platform.is('android')) {
- this.backgroundMode.enable();
- this.backgroundMode.on('enable').subscribe(() => {
- this.backgroundMode.disableWebViewOptimizations();
- })
- if (forceBackgroundOnPause) {
- this.onPauseSubscription = this.platform.pause.subscribe(() => {
- console.log('paused')
- this.paused(); // when we pause, some apps are better left in the background so as to not keep the screen lit..
- });
- } else {
- this.onPauseSubscription = undefined;
- }
- this.powerManagement.dim()
- .then(() => {
- console.log('enablebackground: Wakelock acquired');
- this.powerManagement.setReleaseOnPause(false)
- .then(() => console.log('enablebackground: setReleaseOnPause success'))
- .catch(() => console.log('enablebackground: setReleaseOnPause Failed to set'));
- })
- .catch(() => console.log('enablebackground: Failed to acquire wakelock'));
- } else {
- this.backgroundMode.enable();
- }
- }
- public disableBackground() {
- console.log("Disabling Background Mode");
- if (this.platform.is('android')) {
- if (this.onPauseSubscription != undefined) {
- this.onPauseSubscription.unsubscribe();
- }
- this.backgroundMode.disable();
- this.powerManagement.release()
- .then(() => console.log('disableBackground: Wakelock released'))
- .catch(() => console.log('disableBackground: Failed to release wakelock'));
- } else {
- this.backgroundMode.disable();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement