Skip to content

Instantly share code, notes, and snippets.

@naderio
Last active January 30, 2016 20:14
Show Gist options
  • Save naderio/af8c190346fb705c3309 to your computer and use it in GitHub Desktop.
Save naderio/af8c190346fb705c3309 to your computer and use it in GitHub Desktop.
NativeScript Android battery service
'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