npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ufuk-react-native-battery-optimization-check

v1.0.2

Published

A fork from react-native-battery-optimization-check with the declarations for typescript applications

Downloads

72

Readme

react-native-battery-optimization-check

A react-native solution to check if your app runs under battery optimization on Android devices and makes it easy for the user to disable it.

Base on @rasheedk/react-native-disable-battery-optimizations-android which is no longer supported in react-native >= 0.63.0

Why?

Android devices with API level greater than 23 (Android 6 — Marshmallow) comes with a battery optimization feature in an attempt to reduce battery consumption.

This feature implements a Doze mode which prevents the continuous use of resources by applications while the device is on Standby.

Doze mode and App Standby

If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps’ access to network and CPU-intensive services.

Photo from Android developer website

At the conclusion of each maintenance window, the system again enters Doze, suspending network access and deferring jobs, syncs, and alarms. Over time, the system schedules maintenance windows less and less frequently, helping to reduce battery consumption in cases of longer-term inactivity when the device is not connected to a charger.

As soon as the user wakes the device by moving it, turning on the screen, or connecting a charger, the system exits Doze and all apps return to normal activity.

Here is the list of main restrictions that apply to your application in doze mode:

  • The system ignores wake locks;
  • A network connection is not available;
  • The system doesn’t perform WiFi, WiFi RTT, and BLE scans;
  • Standard AlarmManager alarms will be rescheduled;
  • The system doesn’t allow JobScheduler and WorkManager.

Read more about Optimize for Doze and App Standby.

Battery optimization

Battery optimization allows you to put your app into the whitelist. By default, it is turned off. Apps available in the whitelist are partially exempt from Doze and App Standby optimizations. This doesn’t mean they have full access to and could perform tasks during the doze mode. An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions like jobs being differed, standard alarm triggers are still imposed.

Installation

$ yarn add @saserinn/react-native-battery-optimization-check

Link is automatic on react native >= 0.60

For manual linking :

$ react-native link @saserinn/react-native-battery-optimization-check

Usage

There are 3 functions on this library:

BatteryOptEnabled()

  • Returns true if battery optimization is enabled for your app.
  • Returns false if battery optimization is disabled for your app.
  • Returns false if battery optimization is not available on the device or if the app is not running on Android.

Example:

import { BatteryOptEnabled } from "@saserinn/react-native-battery-optimization-check";

BatteryOptEnabled().then((isEnabled)=>{
	// returns promise<boolean>
	const result = isEnabled ? "enabled" : "disabled";
	// ...
});

OpenOptimizationSettings()

  • This function is only available on Android devices with API level greater than 23 (Android 6 — Marshmallow).
  • Opens the battery optimization settings page with a list of all apps that are available in the whitelist. You can also add or remove apps from the whitelist.
  • This method uses an intent with ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS which is allowed to publish your app in Playstore without restrictions in this regard.

Example:

import { OpenOptimizationSettings, BatteryOptEnabled } from "@saserinn/react-native-battery-optimization-check";

BatteryOptEnabled().then((isEnabled)=>{
	// returns promise<boolean>
	if (isEnabled) {
		// if battery optimization is enabled, request to disable it.
		OpenOptimizationSettings();
		// ...
	}
});

RequestDisableOptimization()

CAUTION! Google Play policies prohibit apps from requesting direct exemption from Power Management features in Android 6.0+ (Doze and App Standby) unless the core function of the app is adversely affected.

Read more about Acceptable use cases for exemption.

  • This function is only available on Android devices with API level greater than 23 (Android 6 — Marshmallow).
  • This function will request the user to disable battery optimization for your app.
  • A system dialog will be shown to the user to disable battery optimization directly from the app.

Example:

import { RequestDisableOptimization, BatteryOptEnabled } from "@saserinn/react-native-battery-optimization-check";

BatteryOptEnabled().then((isEnabled)=>{
	// returns promise<boolean>
	if (isEnabled) {
		// if battery optimization is enabled, request to disable it.
		RequestDisableOptimization();
		// ...
	}
});

Example App

In order to run the example app, you need to install the library and then go to the /example folder and run the following commands:

  1. instal and link node_modules

    $ yarn
  2. run the app

    $ react-native run-android

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library