DEV Community

happyer
happyer

Posted on

Android Application Keep-Alive Strategies and Practices

Preface

In the Android ecosystem, keeping applications alive in the background has always been a challenge for developers. As the Android system places increasing emphasis on user privacy and device performance, the operation of background applications has faced more and more restrictions. However, for some applications that need to run continuously, such as real-time message push and location services, keep-alive strategies become particularly important. This article will explore some current keep-alive solutions on the Android platform and provide some practical suggestions. Here are 30 common Android keep-alive methods to help you overcome background restrictions:

1. Foreground Service

Foreground service is one of the most direct keep-alive methods. By setting the service to the foreground, the system will consider the service crucial to the user and will not easily reclaim it. Foreground services need to display a notification to inform the user that the service is running. You can set a service to the foreground by calling the startForeground(int id, Notification notification) method.

2. Dual Process Daemon

Create two Services running in different processes. When one process is reclaimed by the system, the other process can detect this through the onServiceDisconnected(ComponentName name) method and restart the killed process. This mutual guarding can improve the application's survival rate.

3. Third-Party Push Services

Use third-party push services like JPush, Xiaomi Push, etc. These push services often employ keep-alive techniques to ensure real-time message delivery.

4. Static Broadcast Receivers

Register static broadcast receivers in the AndroidManifest.xml to listen for system broadcasts such as battery status changes and screen unlocks. When a broadcast is received, check if the application process is alive, and if it has been killed, restart the application. Note that from Android 8.0 onwards, the use of static broadcasts has been restricted, and some implicit broadcasts cannot be received through static registration.

5. Using System Broadcasts to Revive

Listen to system broadcasts such as boot completed, network changes, and app installation/uninstallation. When a broadcast is received, check if the application process is alive, and if it has been killed, restart the application.

6. JobScheduler

Use JobScheduler to periodically start the application. JobScheduler, introduced in Android 5.0, is a task scheduling mechanism that can execute tasks under specific conditions, such as when the device is charging or connected to Wi-Fi. By creating a Job, setting the trigger conditions, and submitting the Job to the JobScheduler, the JobScheduler will start the application when the conditions are met.

7. Whitelist

Guide users to add the application to the system's whitelist, such as the power-saving whitelist or auto-start whitelist. Applications added to the whitelist are not subject to system restrictions and can continue running in the background.

8. Proper Use of Activity

When necessary, set the application's Activity to singleTask or singleInstance mode to ensure that only one instance of the application exists in the background. This can reduce system restrictions on the application and improve its survival rate in the background.

9. Using AlarmManager for Periodic Wake-Up

Use AlarmManager to periodically wake up the application. By setting a timed task, the application can be started using a PendingIntent when the specified time is reached. Note that from Android 6.0 onwards, AlarmManager's behavior has been restricted, and timed tasks may be delayed when the device is in low-power mode.

10. Properly Setting Process Priority

The Android system decides whether to reclaim a process based on its priority. By properly setting the process priority, you can reduce the likelihood of the system reclaiming the process. For example, you can set a Service as a foreground service or bind the process to an Activity that the user is interacting with.

11. Using Sticky Broadcasts

Using sticky broadcasts can, to some extent, increase the priority of the broadcast receiver. When a sticky broadcast is sent, the system stores the broadcast in memory, allowing the application to receive it even if it has been killed and restarted. However, note that from Android 5.0 onwards, the use of sticky broadcasts has been restricted, and some broadcasts cannot be sent in sticky mode.

12. Using WorkManager

WorkManager is part of the Android Architecture Components and provides a unified solution for background tasks. WorkManager can automatically choose the best execution method and ensure that tasks are completed even if the application exits or the device restarts. While WorkManager may not be as effective in keep-alive as other methods, it is a solution that aligns more closely with Android system guidelines and can help avoid system restrictions and user experience issues.

13. Proper Use of WakeLock

In certain scenarios, you can use WakeLock to prevent the CPU from entering sleep mode, ensuring that the application can continue running in the background. However, be cautious with WakeLock as it can lead to increased battery consumption. Always release the lock as soon as it is no longer needed.

14. Proper Use of SyncAdapter

SyncAdapter is an Android framework for handling data synchronization operations. SyncAdapter can automatically schedule sync tasks based on the device's network status, battery status, and other conditions. Although SyncAdapter is not specifically designed for keep-alive purposes, it can improve the application's survival rate in the background.

15. Using AccountManager

By adding an account to the application and associating it with SyncAdapter, you can improve the application's survival rate to some extent. When the system triggers a sync operation, it will start the application process associated with the account. However, be cautious with this method as it may cause inconvenience to users.

16. Using Multiple Processes

In the AndroidManifest.xml, set the android:process attribute for Services or Activities to run them in separate processes. This way, even if the main process is killed, other processes can still survive.

17. Using ContentProvider

Register a ContentProvider in the AndroidManifest.xml and access this ContentProvider from other applications through ContentResolver. This way, even if your application is in the background, as long as other applications access the ContentProvider, your application can remain alive.

18. Performance Optimization

Optimize your application's performance to reduce memory, CPU, and battery consumption. By doing so, the system is more likely to reclaim other resource-intensive applications first, thereby increasing your application's survival rate in the background.

19. Using NotificationListenerService

By implementing a NotificationListenerService and registering it in the AndroidManifest.xml, you can listen for changes in the system notification bar. When a new notification is received, check if the application process is alive, and if it has been killed, restart the application. Note that to use NotificationListenerService, users need to grant notification access permission to the application in the settings.

20. Adapting to Doze Mode and App Standby

Starting from Android 6.0 (API level 23), the system introduced Doze mode and App Standby to optimize battery usage. In these modes, the system restricts background applications' network access and CPU usage. To ensure your application runs normally in these modes, you need to adapt to these features, such as using high-priority Firebase Cloud Messaging (FCM) messages to wake up the application.

21. Using Firebase Cloud Messaging (FCM)

For applications that require real-time message push, Firebase Cloud Messaging (FCM) is a reliable solution. FCM is a cross-platform messaging service that ensures efficient and reliable message delivery. By using FCM, you can ensure that your application receives real-time messages even when it is in the background, without needing excessive keep-alive measures.

22. Timely Adaptation to New System Versions

As the Android system evolves, the restrictions on background applications may change. To ensure that your application runs smoothly on new system versions, you need to timely adapt to these changes and adjust your keep-alive strategies accordingly.

23. Using Binder Mechanism

Binder is an inter-process communication (IPC) mechanism in Android. By creating a Binder object in a Service and obtaining this Binder object in another process, you can establish a connection between the two processes, thereby improving the Service's survival rate.

24. Using Native Processes

Using JNI (Java Native Interface) technology, you can create a native process to guard the application process. When the application process is killed, the native process can detect this event and restart the application process. This method requires knowledge of C/C++ and may increase the complexity and maintenance cost of the application.

25. Using Reflection to Call Hidden APIs

There are some hidden APIs and system services in the Android system that can be used to improve the application's survival rate. For example, by using reflection to call the ActivityManager's addPersistentProcess method, you can set the application as a system process, thereby increasing its priority. However, this method carries significant risks and may cause the application to malfunction on certain devices or system versions.

26. Monitoring System UI

Monitor changes in the system UI, such as the status bar and navigation bar. When the system UI changes, check if the application process is alive, and if it has been killed, restart the application.

Conclusion

When designing keep-alive strategies for Android applications, developers need to balance system resources, user experience, and application requirements. By effectively utilizing the APIs and services provided by the system, adhering to Android best practices, and building trust with users, you can achieve background keep-alive without violating system guidelines. As the Android system continues to evolve, developers need to stay updated and adapt to new system features and restrictions to ensure stable operation and a good user experience.

Codia AI's products

Codia AI has rich experience in multimodal, image processing, development, and AI.
1.Codia AI Figma to code:HTML, CSS, React, Vue, iOS, Android, Flutter, Tailwind, Web, Native,...

Codia AI Figma to code

2.Codia AI DesignGen: Prompt to UI for Website, Landing Page, Blog

Codia AI DesignGen

3.Codia AI Design: Screenshot to Editable Figma Design

Codia AI Design

4.Codia AI VectorMagic: Image to Full-Color Vector/PNG to SVG

Codia AI VectorMagic

Top comments (0)