Created
April 12, 2017 12:44
-
-
Save bsorrentino/ac2b3ee2cecb96a867f6d922faa90617 to your computer and use it in GitHub Desktop.
Battery Mode using Cordova Broadcaster Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var exec = require('cordova/exec'); | |
| exports.startMonitoringPowerState = function(success, error) { | |
| exec(success, error, "broadcasterTest", "startMonitoringPowerState", []); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /********* broadcasterTest.m Cordova Plugin Implementation *******/ | |
| #import <Cordova/CDV.h> | |
| @interface broadcasterTest : CDVPlugin { | |
| // Member variables go here. | |
| } | |
| - (void)startMonitoringPowerState:(CDVInvokedUrlCommand*)command; | |
| @end | |
| @implementation broadcasterTest | |
| -(NSDictionary *)isLowPowerModeEnabled { | |
| BOOL state = ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) ; | |
| return @{ @"lowPower":[NSNumber numberWithBool:state] }; | |
| } | |
| - (void)startMonitoringPowerState:(CDVInvokedUrlCommand*)command | |
| { | |
| CDVPluginResult* pluginResult = nil; | |
| [[NSNotificationCenter defaultCenter] addObserver:self | |
| selector: @selector(onPowerStateDidChange:) | |
| name: NSProcessInfoPowerStateDidChangeNotification | |
| object: nil]; | |
| pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self isLowPowerModeEnabled]]; | |
| [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; | |
| } | |
| - (void)onPowerStateDidChange:(NSNotification *)notification { | |
| [[NSNotificationCenter defaultCenter] postNotificationName:@"powerStateDidChange" | |
| object:nil | |
| userInfo:[self isLowPowerModeEnabled]]; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;(function() { | |
| console.log( "INIT TEST!!"); | |
| function onReady() { | |
| window.broadcaster.addEventListener( "powerStateDidChange", function( e ) { | |
| console.log( "powerStateDidChange", e ); | |
| }); | |
| cordova.plugins.broadcasterTest.startMonitoringPowerState( | |
| function(initState) { | |
| console.log( "START MONITOR POWER STATE!", JSON.stringify(initState) ); | |
| }, | |
| function(e) { | |
| console.log( "ERROR STARTING MONITOR POWER STATE ", e); | |
| } | |
| ) | |
| } | |
| document.addEventListener('deviceready', onReady, false); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment