premium, Создай класс, унаследованный от Service или LifecycleService : public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1; @Override public void onCreate() { super.onCreate(); startForeground(NOTIFICATION_ID, createNotification()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Здесь выполняй фоновую работу new Thread(() -> { while (true) { Log.d("MyForegroundService", "Сервис работает..."); try { Thread.sleep(5000); // Каждые 5 секунд лог } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; // Не используем биндинг } @Override public void onDestroy() { super.onDestroy(); Log.d("MyForegroundService", "Сервис уничтожен"); } private Notification createNotification() { String channelId = "foreground_service_channel"; NotificationChannel channel = new NotificationChannel( channelId, "Foreground Service", NotificationManager.IMPORTANCE_LOW ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); return new NotificationCompat.Builder(this, channelId) .setContentTitle("Фоновый сервис") .setContentText("Сервис выполняет работу в фоне") .setSmallIcon(R.drawable.ic_launcher_foreground) .build(); } } JAVA public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1; @Override public void onCreate() { super.onCreate(); startForeground(NOTIFICATION_ID, createNotification()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Здесь выполняй фоновую работу new Thread(() -> { while (true) { Log.d("MyForegroundService", "Сервис работает..."); try { Thread.sleep(5000); // Каждые 5 секунд лог } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; // Не используем биндинг } @Override public void onDestroy() { super.onDestroy(); Log.d("MyForegroundService", "Сервис уничтожен"); } private Notification createNotification() { String channelId = "foreground_service_channel"; NotificationChannel channel = new NotificationChannel( channelId, "Foreground Service", NotificationManager.IMPORTANCE_LOW ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); return new NotificationCompat.Builder(this, channelId) .setContentTitle("Фоновый сервис") .setContentText("Сервис выполняет работу в фоне") .setSmallIcon(R.drawable.ic_launcher_foreground) .build(); } } Добавь в AndroidManifest.xml : <service android:name=".MyForegroundService" android:foregroundServiceType="mediaPlayback" android:exported="false" /> XML <service android:name=".MyForegroundService" android:foregroundServiceType="mediaPlayback" android:exported="false" /> Заменяй mediaPlayback на то, что подходит для твоего сервиса, например, dataSync , location , phoneCall и т. д. Запуск сервиса: Intent serviceIntent = new Intent(context, MyForegroundService.class); ContextCompat.startForegroundService(context, serviceIntent); JAVA Intent serviceIntent = new Intent(context, MyForegroundService.class); ContextCompat.startForegroundService(context, serviceIntent); Остановка сервиса: Intent serviceIntent = new Intent(context, MyForegroundService.class); context.stopService(serviceIntent); JAVA Intent serviceIntent = new Intent(context, MyForegroundService.class); context.stopService(serviceIntent); Если Android убьёт сервис, то он перезапустится сам, так как мы используем START_STICKY в onStartCommand() .
MALWARE, почему то так кажется, что это не поможет. Так как сама система андроида через некоторое время убирает приложение в неактивные.