Android App Keep-Alive Strategies: Boot Broadcast, Account Sync, Foreground Service, and JobScheduler
This article explains various Android keep‑alive techniques—including boot‑time auto‑start, account‑sync mechanisms, foreground services, and scheduled jobs—providing code examples and configuration steps to help apps stay alive while also showing how to disable these features on specific devices.
Android applications often need to stay alive for functions such as push notifications and data reporting, while manufacturers try to limit background execution to save battery and improve performance.
This article explains several Android keep‑alive techniques, including auto‑start via boot broadcast, account‑sync mechanisms, foreground services, and JobScheduler periodic jobs.
Boot broadcast – Implement a BroadcastReceiver subclass (BootReceiver) and register it in AndroidManifest.xml with the BOOT_COMPLETED and LOCKED_BOOT_COMPLETED actions.
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// check and start foreground service for keep‑alive
}
}Register the receiver:
<receiver android:name=".receiver.BootReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<!-- priority can be set -->
<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 – Define an account type and sync‑adapter XML, then create a ContentProvider subclass (AccountSyncProvider) and register it.
<?xml version="1.0" encoding="utf-8"?>
<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 the authentication service and sync service, then add the account and enable automatic syncing via ContentResolver .
AccountManager am = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account account = new Account(accountName, {accountType});
am.addAccountExplicitly(account, accountPwd, new Bundle());
ContentResolver.setIsSyncable(account, {contentAuthority}, 1);
ContentResolver.setSyncAutomatically(account, {contentAuthority}, true);
ContentResolver.addPeriodicSync(account, {contentAuthority}, new Bundle(), 1);JobScheduler – Create a JobService subclass (LiveJobService) and schedule periodic jobs that survive device reboots.
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class LiveJobService extends JobService {
@Override public boolean onStartJob(JobParameters params) { return false; }
@Override public boolean onStopJob(JobParameters params) { return false; }
} JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(jobId,
new ComponentName(context.getPackageName(), LiveJobService.class.getName()))
.setPersisted(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
builder.setPeriodic(50000);
} else {
builder.setMinimumLatency(50000);
}
jobScheduler.schedule(builder.build());Foreground service – Implement a Service (NotificationService) that starts a persistent notification to keep the process in the foreground.
public class NotificationService extends Service {
@Override public IBinder onBind(Intent intent) { return null; }
@Override public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setForegroundService();
}
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
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 nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.createNotificationChannel(channel);
startForeground(111, builder.build());
}
}Finally, the article shows how to disable these keep‑alive features on specific devices (e.g., Huawei Honor 20) by adjusting system settings such as auto‑start, background activity, and account sync permissions.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
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.