Last active
January 30, 2016 20:14
-
-
Save naderio/af8c190346fb705c3309 to your computer and use it in GitHub Desktop.
NativeScript Android battery service
This file contains 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
'use strict'; | |
const application = require('application'); | |
const observable = require('data/observable'); | |
const events = require('~/common/events'); | |
const debug = require('nativescript-debug')(__filename); | |
var service = new observable.Observable(); | |
service.value = null; | |
application.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED, function onReceiveCallback(context, intent) { | |
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1); | |
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1); | |
var value = (level / scale) * 100.0; | |
this.value = value; | |
this.notify({ | |
eventName: 'update', | |
object: value, | |
}); | |
}.bind(service)); | |
module.exports = service; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment