Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robinpowered/react-native-device-battery
Monitor device battery state changes
https://github.com/robinpowered/react-native-device-battery
Last synced: 2 months ago
JSON representation
Monitor device battery state changes
- Host: GitHub
- URL: https://github.com/robinpowered/react-native-device-battery
- Owner: robinpowered
- Created: 2015-12-02T02:42:00.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-18T11:46:51.000Z (about 2 years ago)
- Last Synced: 2024-04-14T07:57:17.067Z (10 months ago)
- Language: Java
- Size: 130 KB
- Stars: 43
- Watchers: 28
- Forks: 28
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-react-native - react-native-device-battery ★30 - Observe battery state changes in your react native application (Components / System)
- awesome-react-native - react-native-device-battery ★30 - Observe battery state changes in your react native application (Components / System)
- awesome-react-native - react-native-device-battery ★30 - Observe battery state changes in your react native application (Components / System)
- awesome-react-native - react-native-device-battery ★30 - Observe battery state changes in your react native application (Components / System)
- awesome-react-native-ui - react-native-device-battery ★11 - Observe battery state changes in your react native application (Components / System)
README
# react-native-device-battery
Get and observe the devices battery level and charging status
## Installation
Install the node module
```
npm install react-native-device-battery --save
```### iOS
TBD### Android
Add the following to `android/settings.grade`
```
include ':react-native-device-battery'
project(':react-native-device-battery').projectDir = new File(settingsDir, '../node_modules/react-native-device-battery/android')
```Add the following to `android/app/build.gradle`
```
compile project(':react-native-device-battery')
```Register the module in `MainActivity.java`
```java
import com.robinpowered.react.battery.DeviceBatteryPackage; // <--- importpublic class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
......@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new DeviceBatteryPackage()) // <------ add this line to yout MainActivity class
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();mReactRootView.startReactApplication(mReactInstanceManager, "AndroidRNSample", null);
setContentView(mReactRootView);
}......
}
```## Example Usage
```js
import DeviceBattery from 'react-native-device-battery';// get the battery level
DeviceBattery.getBatteryLevel().then(level => {
console.log(level); // between 0 and 1
});// check if the device is charging
DeviceBattery.isCharging().then(isCharging => {
console.log(isCharging) // true or false
});// as a listener
var onBatteryStateChanged = (state) => {
console.log(state) // {level: 0.95, charging: true}
};// to attach a listener
DeviceBattery.addListener(onBatteryStateChanged);// to remove a listener
DeviceBattery.removeListener(onBatteryStateChanged);
```