Capdi
Capdi

Reputation: 257

Ionic app: Keeping the screen unlock and open when the ionic app is up and running

I'm developing a navigation app with Ionic framework. Is there a way to keep the screen of the device open while the app is up and running ?

Upvotes: 1

Views: 1927

Answers (2)

Jay Pagnis
Jay Pagnis

Reputation: 1110

The answer by Najam Us Saqib is correct but for some minor corrections. Corrections to the above answer: The IONIC Native package doesnt exist anymore. We need to use Awesome-Cordova-plugins package.

npm install @awesome-cordova-plugins/insomnia

A slightly modified version of the code would be

import { Insomnia } from '@awesome-cordova-plugins/insomnia/ngx';

export class MyComponent implements OnInit, OnDestroy {

    constructor(private insomnia: Insomnia) {
        this.insomnia.keepAwake()
            .then(
                () => console.log('success'),
                () => console.log('error')
            );
    }

    ngOnDestroy() {
        ...
        this.insomnia.allowSleepAgain()
          .then(
            () => console.log('success'),
            () => console.log('error')
          );
        ...
    }

}

The above code will keep the app awake as long as this component is in the stack. Once the component is destroyed, the app will allow the device to sleep again.

Upvotes: 1

Najam Us Saqib
Najam Us Saqib

Reputation: 3402

Yes you can Keep screen Active: There is a cordova plugin for this:

ionic cordova plugin add cordova-plugin-insomnia

npm install @ionic-native/insomnia

import { Insomnia } from '@ionic-native/insomnia/ngx';

constructor(private insomnia: Insomnia) { }

...

this.insomnia.keepAwake()
  .then(
    () => console.log('success'),
    () => console.log('error')
  );

this.insomnia.allowSleepAgain()
  .then(
    () => console.log('success'),
    () => console.log('error')
  )

Check Plugin Docs here Cordova Insomnia Docs

Upvotes: 4

Related Questions