> For the complete documentation index, see [llms.txt](https://docs.sentiance.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sentiance.com/sdk/appendix/battery-optimization.md).

# Battery Optimization

### Battery usage

Battery consumption greatly depends on enabled features: the more features an app requires, the more data is processed and the more battery power is used. Users who make many trips or very long trips will naturally experience higher battery consumption.

Processing sensor and location data is the primary source of battery consumption. This occurs when the SDK requests data from the device’s sensors and the location services.

A high frequency of sensor and location requests increases the volume and rate of data processing by the SDK, which significantly improves the accuracy of detections performed by the SDK, but also results in more battery usage

Sensor and location data is requested and processed only when the SDK is in an active state. \
Learn more about this in the next section.

### Optimization features

To minimize battery impact from sensor and location processing, multiple optimization techniques are applied that reduce background processing and battery consumption by the SDK.<br>

* When the user is stationary, the SDK enters an idle/sleep state. In this state the SDK is not detecting or processing any data.<br>
* The SDK only wakes up when:
  * A geofence is broken: meaning the user is in significant movement
  * The state of the user transitions from Stationary to in-transport by or automatic trip start.<br>
* After approximately 3 minutes of being stationary, the SDK goes back to sleep and stops active detection until awakened again

### Battery saving by systems

Native battery saving features, such as Low Power Mode and Adaptive Battery on Android, can reduce SDK detection quality.\
\
Sentiance SDK built-in battery optimizations already improve energy efficiency, so we recommend disabling battery-saving features for any app running the Sentiance SDK to ensure optimal performance.

For consistent and reliable detection performance, it is recommended to:

* Inform end users about the restrictions of native battery-saving features of the device
* Explain why disabling certain battery-saving features for the app improves SDK accuracy

See below for the best practices regarding battery optimization

<details>

<summary>iOS</summary>

On iOS, you can’t fully prevent your app from being terminated in the background especially under low battery or system pressure. The OS strictly controls background execution to preserve battery and performance.

* Apps cannot disable Low Power Mode or system battery optimizations
* The system may suspend or terminate your app at any time in the background
* There is no equivalent to Android’s “disable battery optimization”

The Sentiance SDK already has excellent built-in battery optimazation features. and detections are being processed in the most efficient way possible.&#x20;

</details>

<details>

<summary>Android</summary>

Certain Android device manufacturers aggressively restrict battery usage, often prioritizing low battery consumption over background app quality.&#x20;

[Don't Kill My App](https://dontkillmyapp.com/) (no affiliation to Sentiance) keeps track of different manufacturers' practices on battery optimization techniques that severely restrict how your app can run in the background and the potential solutions.

Manufacturers listed below are notorious for their strict requirements for background process restrictions, and without **proper user-onboarding, and correct battery-related settings, Sentiance SDK cannot guarantee a consistent output on these devices.**

* [OnePlus](https://dontkillmyapp.com/oneplus)
* [Huawei](https://dontkillmyapp.com/huawei)
* [Samsung](https://dontkillmyapp.com/samsung)
* [Xiaomi](https://dontkillmyapp.com/xiaomi)
* [Others](https://dontkillmyapp.com/)<br>

### Programmatically Prompting for Battery Optimization Permission

When running the SDK on Android 6 and higher, it is recommended to ask the user to disable battery optimization for your app. This makes sure that SDK detections continue to work properly when the device is in Doze mode. This is particularly important with manufacturers like OnePlus and Nokia (HMD Global) who customize Android to do aggressive battery optimization, keeping the device in Doze mode longer than intended.

Additionally, on Android 9, disabling this will prevent Adaptive Battery from [bucketing](https://developer.android.com/about/versions/pie/power#buckets) your app based on usage and [restricting background processing](https://developer.android.com/reference/android/app/ActivityManager#isBackgroundRestricted\(\)), all of which that can impact the detection quality of the SDK.

After explaining to the user about the benefits of disabling battery optimization, call the [`disableBatteryOptimization()`](/sdk/api-reference/android/sentiance.md#disablebatteryoptimization) method of the [`Sentiance`](/sdk/api-reference/android/sentiance.md) class. This will trigger a system dialog asking the user to allow disabling battery optimization for your app.

```kotlin
Sentiance.getInstance(context).disableBatteryOptimization()
```

The Sentiance SDK does not define the `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission required for this feature. You must therefore explicitly add this permission to your app.

{% code title="AndroidManifest.xml" %}

```xml
<uses-permission 
	android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
```

{% endcode %}

{% hint style="danger" %}
Please note that Google Play policies prohibit apps from requesting this permission unless the app's core functionality is affected. For more information about the supported use cases, see [here](https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases).
{% endhint %}

{% hint style="success" %}
Please take advantage of [SDK Status](/sdk/api-reference/android/sdkstatus.md) updates to react to any incorrect configurations.
{% endhint %}

### Embeddable Projects

#### (These are not maintained by Sentiance)

{% embed url="<https://github.com/DoubleDotLabs/doki>" %}

{% embed url="<https://github.com/judemanutd/AutoStarter>" %}

</details>
