How xuningjack/servicekeep works
1. What this repo is
- This is an Android “service keep-alive” module, not a general app.
- Its core goal is to keep a target Service process alive across Android versions by using two different strategies:
- Android 5.0 and below: native process forking via JNI/C
- Android 5.0 and above: JobScheduler-based wakeups
2. Main runtime boundary
- The architecture is split between:
- Java/Kotlin-side Android components in app/src/jack/com/servicekeep
- Native C code in app/jni
- The Java side owns service orchestration and Android lifecycle integration.
- The native side exists only to support the pre-Lollipop process-fork keep-alive mechanism.
3. Primary entry points
- MainActivity is the sample/demo UI entry point.
- The real integration point for consumers is KeepAliveManager.INSTANCE.startKeepAliveService(context) and stopKeepAliveSerice(context).
- The README makes it clear the library is meant to be embedded, with the host app putting its actual business logic into WorkService.onStartCommand().
4. Core orchestration layer
- KeepAliveManager is the central facade.
- It likely decides which keep-alive strategy to use based on OS version and coordinates starting/stopping the relevant components.
- Supporting managers:
- ServiceManager for service lifecycle operations
- PreferencesManager for persisted flags/configuration
- This is the main control plane of the module.
5. Business service boundary
- WorkService is the service that the host application is expected to keep alive.
- The README explicitly says this is where the actual business behavior should be executed, specifically in onStartCommand.
- From an architecture perspective, this is the payload service; the rest of the repo exists to make sure it gets restarted or reawakened.
6. Scheduler-based keep-alive path for Android 5.0+
- KeepAliveJobSchedulerService is the platform job component used on newer Android versions.
- It runs in its own process, as indicated by the manifest example.
- Its role is to schedule periodic or deferred work that wakes or restarts WorkService.
- This is the main recovery path on modern Android, replacing the older native forking approach.
7. Native keep-alive path for Android 5.0 and below
- NativeRuntime bridges Java to native code.
- app/jni/helper.c contains the C-side logic, which the README describes as using fork to create a process that helps keep the service alive.
- Android.mk and Application.mk show this is built with the NDK and packaged into the app.
- The native path is a version-specific compatibility subsystem, not part of the main business flow.
8. Persistence and state
- PreferencesManager likely stores enable/disable state or runtime settings needed to remember whether keep-alive should be active.
- This is the only obvious persistence boundary in the repo besides Android’s own service/job state.
- It supports the orchestration layer rather than holding business data.
9. Utilities and support code
- FileUtils and LogUtils are supporting utilities.
- They are not core architectural boundaries, but they indicate the module includes file/system interactions and centralized logging.
- Constant.java likely holds process names, service names, or scheduling constants that glue the components together.
10. Process model
- The README and manifest examples show multiple isolated Android processes:
- one for WorkService
- one for the job scheduler service
- one or more native/helper processes in the older strategy
- This multi-process design is the key architectural feature of the module.
- The keep-alive strategy depends on process separation so one component can revive another.
11. Android platform dependencies
- The repo depends on Android system services and permissions, especially:
- RECEIVE_BOOT_COMPLETED for boot-time reactivation
- BIND_JOB_SERVICE for the scheduler service
- The manifest also marks services as persistent and uses explicit processes, which is essential to the keep-alive design.
12. Native binary packaging
- app/libs/ contains prebuilt native libraries for multiple ABIs:
- armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64
- This matters because the keep-alive mechanism must run across device architectures, and the module is intended to be broadly compatible.
13. How data/control flows
- Host app calls KeepAliveManager.startKeepAliveService(context).
- Manager starts/configures the appropriate keep-alive mechanism.
- On modern Android, KeepAliveJobSchedulerService periodically re-triggers or monitors WorkService.
- On older Android, the native forked process helps preserve or restart the service.
- WorkService.onStartCommand() is where the host app’s persistent logic actually executes.
14. How to draw the diagram
- Center the diagram on KeepAliveManager.
- Draw two branches:
- Newer Android: KeepAliveManager → KeepAliveJobSchedulerService → WorkService
- Older Android: KeepAliveManager → NativeRuntime → helper.c forked process → WorkService
- Add PreferencesManager and ServiceManager as supporting components around the manager.
- Place MainActivity as a demo/consumer entry point, not part of the core keep-alive mechanism.
Main entry points: KeepAliveManager.java KeepAliveManager · WorkService.java WorkService · KeepAliveJobSchedulerService.java KeepAliveJobSchedulerService · NativeRuntime.java NativeRuntime
Components 15
Java side
Android-facing orchestration, services, and sample entry point.
- MainActivity · Sample UI used to trigger keep-alive behavior.
app/src/jack/com/servicekeep/MainActivity.java - KeepAliveManager · Central control plane that starts and stops the keep-alive subsystem.
app/src/jack/com/servicekeep/manager/KeepAliveManager.java - ServiceManager · Coordinates Android service lifecycle operations.
app/src/jack/com/servicekeep/manager/ServiceManager.java - PreferencesManager · Persists enablement and runtime flags for keep-alive.
app/src/jack/com/servicekeep/manager/PreferencesManager.java - Constant · Shared names, flags, and scheduling constants.
app/src/jack/com/servicekeep/Constant.java - KeepAliveJobSchedulerService · Modern keep-alive service used on Android 5.0+.
app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java - WorkService · Host application's persistent service; business logic runs here.
app/src/jack/com/servicekeep/service/WorkService.java - FileUtils · Support code for filesystem and path handling.
app/src/jack/com/servicekeep/utils/FileUtils.java - LogUtils · Centralized logging helper used across the module.
app/src/jack/com/servicekeep/utils/LogUtils.java
Native side
Pre-Lollipop compatibility layer and NDK-backed fork logic.
- NativeRuntime · Java-to-native bridge for the pre-Lollipop strategy.
app/src/jack/com/servicekeep/fork/NativeRuntime.java - helper.c · C-side fork-based process helper used to keep the service alive.
app/jni/helper.c - NDK build · Build scripts that compile and package the native runtime.
app/jni/Android.mk - Native libs · Prebuilt native binaries for multiple CPU architectures.
app/libs
Android platform
OS services and processes the module depends on.
- JobScheduler · Platform scheduling API used to wake or relaunch work on newer Android versions.
- App processes · Separate Android processes used to isolate the worker, scheduler, and helper paths.
Connections 16
- MainActivity → KeepAliveManager: starts · Demo UI invokes the keep-alive facade.
- KeepAliveManager → PreferencesManager: reads/writes · Facade consults persisted state before starting or stopping the subsystem.
- KeepAliveManager → ServiceManager: coordinates · Facade delegates service lifecycle operations.
- KeepAliveManager → Constant: uses · Shared constants guide component names and behavior.
- KeepAliveManager → KeepAliveJobSchedulerService: newer Android · Selects the JobScheduler-based keep-alive path on Android 5.0+.
- KeepAliveManager → NativeRuntime: older Android · Selects the JNI/native path on Android 5.0 and below.
- KeepAliveJobSchedulerService → JobScheduler: scheduled by · Runs through the platform job scheduling API.
- KeepAliveJobSchedulerService → WorkService: revives · Wakeups or deferred work target the persistent service.
- NativeRuntime → helper.c: bridges to · Java layer enters native fork logic through JNI.
- helper.c → App processes: forks · Native helper creates a process used to sustain keep-alive behavior.
- App processes → WorkService: isolates · Separate process boundaries let one component restart another.
- NDK build → helper.c: builds · NDK scripts compile the native helper into packaged binaries.
- Native libs → NativeRuntime: packages · ABI-specific binaries support the JNI/native path across architectures.
- WorkService → FileUtils: uses · Payload service can rely on filesystem helpers.
- WorkService → LogUtils: logs with · Service-side execution is centralized through logging helpers.
- KeepAliveManager → App processes: depends on · Overall design relies on process separation for recovery.