Android App Keep‑Alive Techniques: Boot Receiver, Account Sync, Foreground Service, and JobScheduler
This guide explains Android keep‑alive strategies—including boot‑completed receivers, account‑sync adapters, JobScheduler jobs, and foreground services—while also covering device‑specific settings for enabling or disabling background execution on manufacturers such as Huawei.
Android application keep-alive is a joint effort of the app, the system and the user. Most apps want to stay alive permanently to support push notifications and data reporting, while manufacturers try to limit background execution to save battery, and users want apps to run when needed but stop otherwise.
The article explains several Android keep‑alive strategies, including boot‑completed broadcast receivers, account‑sync adapters, JobScheduler jobs, and foreground services.
Boot Receiver
Implement a BroadcastReceiver subclass that listens for BOOT_COMPLETED (and LOCKED_BOOT_COMPLETED ) and starts a foreground service.
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// check and start foreground service etc.
}
}Register it in AndroidManifest.xml with high priority:
<receiver android:name=".receiver.BootReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<!-- priority -->
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
</intent-filter>
</receiver>Account Sync Mechanism
Create an account type, a sync‑adapter XML, an authenticator service, and a SyncAdapter that runs when the system performs account synchronization.
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="{accountType}"
android:allowParallelSyncs="false"
android:contentAuthority="{contentAuthority}"
android:isAlwaysSyncable="true"
android:supportsUploading="true"
android:userVisible="true"/>Implement AccountSyncProvider , AuthenticationService , and register them in the manifest.
JobScheduler
Define a JobService (e.g., LiveJobService ) and schedule it with JobScheduler . Use setPersisted(true) for reboot persistence and setPeriodic() or setMinimumLatency() for timing.
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class LiveJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
// task logic
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}Foreground Service
Implement a Service (e.g., NotificationService ) that creates a low‑importance notification channel and calls startForeground() to keep the process alive under strict background limits.
public void setForegroundService() {
String channelName = "slient_name";
String CHANNEL_ID = "slient_id";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
channel.setDescription("test");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("通知")
.setContentText("前台服务")
.setAutoCancel(true)
.setOngoing(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
startForeground(111, builder.build());
}Device‑Specific Settings
On devices such as Huawei/Honor, users can enable “Auto‑start”, “Associated start”, and “Background activity” in the phone manager, or disable them to prevent keep‑alive. Account sync can also be toggled in the system settings.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.