Urgent.News

What's breaking now, across thousands of outlets.

Tech

Как не сажать батарею телефона своим приложением

Представь: ты едешь в метро, листаешь приложение, а телефон садится за 20 минут. Ты злишься и удаляешь приложение. Твоя задача как разработчика — чтобы приложение было полезным, но не «съедало» батарею. ────────────────────────────────────────────────── Почему вообще важно думать про заряд • Пользователь не хочет остаться без связи. Если приложение быстро сажает батарею — его просто удалят. •…

Original Russian Read in English

When using your app on a phone, you might find that the phone's battery drains quickly, often within 20 minutes. This can be frustrating and may lead you to uninstall the app. As a developer, your goal is to create a useful app that doesn't drain the battery excessively.

Understanding why battery conservation is important is crucial. Users don't want to lose their connection, and a quickly draining battery is undesirable. Additionally, a heated phone is harmful, as the processor working at maximum capacity causes the phone to heat up and the battery to wear out faster. Furthermore, conserving energy reduces the need for frequent phone charging, extending the lifespan of the phone and decreasing electronic waste. The key is to provide necessary functionality while consuming minimal energy.

The main difference between web and mobile apps lies in their functionality. Web apps (websites, PWAs) operate in a browser, where the browser decides when to "freeze" a tab to save battery. However, developers have little control over this. For instance, a timer that performs an action every minute may not function in the background. On the other hand, native apps (React Native, Flutter) have direct access to the system and can:

- Check battery level

- Perform tasks in the background (e.g., download updates overnight when the phone is charging)

- Respond to events (e.g., notification received, Wi-Fi connected)

Mobile apps can be more energy-efficient but also have a higher chance of errors.

Mobile apps primarily consume energy through three main factors: the screen, the processor (CPU), and the radio module (network). The screen is the biggest energy consumer, and the brighter it is, the more animations and complex effects it has, the quicker the battery drains. The processor (CPU) also consumes significant energy when constantly calculating, redrawing, or checking.

Lastly, the radio module (network) uses much more energy than Wi-Fi, especially with frequent short requests. The simple rule is to avoid unnecessary actions. If an animation isn't needed at the moment, don't show it. If data is already present on the phone, don't download it again.

To save energy, consider these strategies:

- Simplify the interface during low battery. Remove blurred backgrounds, complex shadows, and heavy animations, leaving only essential elements.

- Avoid unnecessary requests. If a user has already downloaded a list of products and nothing has changed, don't download it again.

- Group actions. It's better to download a batch of data once rather than 10 times with slight changes.

- Utilize caching. Store data on the device (in a database or storage) to avoid network requests.

- Perform heavy computations outside the main thread. Allow the interface to remain responsive while the complex tasks run separately.

- React to the app's lifecycle. When the app goes to the background (user minimizes it), stop timers, pauses, and heavy tasks. When the app returns to the foreground, you can re-enable everything.

Regarding background tasks, here's what mobile apps can and cannot do:

- Schedule tasks (once a day, once a week)

- Download files when the phone is charging and connected to Wi-Fi

- Process data while the app is in the background

PWAs (web apps) cannot:

- Operate extensively in the background

- Consistently receive data from sensors (GPS, accelerometer)

- Perform regular scheduled tasks

Keep in mind that different phones use battery differently. An older device may cause an app to drain the battery faster. It's important to test your app on various phones. Temperature also affects battery usage; if an app heavily heats the phone, the system may slow it down to prevent overheating. Lastly, app store rules and permissions play a role. Apple and Google closely monitor background activities; attempts to deceive the system can result in the app not being accepted to the store or being restricted.

The basic principle is to react to the battery level: with high battery, enable all features, animations, and auto-updates. With medium battery, keep only essential functions and disable heavy effects. With low battery, only provide the most critical features and minimize network usage. Inform the user when "energy-saving mode" is activated, so they understand why certain functions aren't working.

In summary, you shouldn't develop an app that's the fastest at any cost. Your goal is to create a decently fast and energy-efficient app. Think about the user: they care about not having their phone die at inconvenient times. If you consider battery level, lifecycle, network, and screen, your app will not only be user-friendly but also environmentally friendly.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How I score a small business website, check by check

Disclosure: I built this tool with help from an AI coding assistant (Claude), and an AI helped draft this article. I reviewed the code and the text. Every number below comes from the project's README.

  • Each area has its own weight and scoring system, with overall score as a weighted mean of 100%.
  • The tool presents top three actionable fixes to business owners with easy-to-understand reports.

More from Thursday 19 November →