Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { BackgroundMode } from '@ionic-native/background-mode';
  3. import { Platform } from 'ionic-angular';
  4. import { PowerManagement } from '@ionic-native/power-management';
  5.  
  6. @Injectable()
  7. export class BackgroundProvider {
  8.  
  9.  
  10.   private onPauseSubscription = undefined;
  11.   constructor(private backgroundMode: BackgroundMode,
  12.               private platform: Platform,
  13.               private powerManagement: PowerManagement) { }
  14.  
  15.   public paused() {
  16.     // push us to the background in this case so that we're not driving the screen...
  17.     this.backgroundMode.moveToBackground();
  18.   }
  19.  
  20.   public enableBackground(forceBackgroundOnPause) {
  21.     // https://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012
  22.  
  23.     this.backgroundMode.setDefaults({
  24.       title: "Radios App",
  25.       hidden: false,
  26.       silent: true,
  27.       resume: false,
  28.     });
  29.     if (this.platform.is('android')) {
  30.       this.backgroundMode.enable();
  31.       this.backgroundMode.on('enable').subscribe(() => {
  32.         this.backgroundMode.disableWebViewOptimizations();
  33.       })
  34.       if (forceBackgroundOnPause) {
  35.         this.onPauseSubscription = this.platform.pause.subscribe(() => {
  36.           console.log('paused')
  37.           this.paused(); // when we pause, some apps are better left in the background so as to not keep the screen lit..
  38.         });
  39.       } else {
  40.         this.onPauseSubscription = undefined;
  41.       }
  42.  
  43.       this.powerManagement.dim()
  44.         .then(() => {
  45.           console.log('enablebackground: Wakelock acquired');
  46.           this.powerManagement.setReleaseOnPause(false)
  47.             .then(() => console.log('enablebackground: setReleaseOnPause success'))
  48.             .catch(() => console.log('enablebackground: setReleaseOnPause Failed to set'));
  49.         })
  50.         .catch(() => console.log('enablebackground: Failed to acquire wakelock'));
  51.     } else {
  52.       this.backgroundMode.enable();
  53.     }
  54.   }
  55.  
  56.   public disableBackground() {
  57.     console.log("Disabling Background Mode");
  58.     if (this.platform.is('android')) {
  59.       if (this.onPauseSubscription != undefined) {
  60.         this.onPauseSubscription.unsubscribe();
  61.       }
  62.       this.backgroundMode.disable();
  63.       this.powerManagement.release()
  64.         .then(() => console.log('disableBackground: Wakelock released'))
  65.         .catch(() => console.log('disableBackground: Failed to release wakelock'));
  66.     } else {
  67.       this.backgroundMode.disable();
  68.     }
  69.   }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement