Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
How to know os kill my foreground service android

I made a lock screen app. I want to restart my service when the OS kills the service in Xiaomi Redmi Note 10 Pro (MIUI 12). When the service is killed, onDestroy is not call.

public class LockScreenService extends Service {
    SharedPreferences prefs;
    private  BroadcastReceiver screenStateReceiver;
    public static boolean isScreenReceiverRegistered=false;
    public IBinder onBind(Intent paramIntent) {
        return null;
    }
    public void onCreate() {
        super.onCreate();

        prefs = getSharedPreferences("SettingPreference", Context.MODE_PRIVATE);
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.setPriority(999);
        screenStateReceiver = new ScreenStateReceiver();
        registerReceiver(screenStateReceiver, filter);
        isScreenReceiverRegistered = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String channelId = createNotificationChannel(notificationManager);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
            Notification notification = notificationBuilder.setOngoing(true)
                    .setSmallIcon(R.drawable.icon_notification)
                    .setPriority(NotificationCompat.PRIORITY_MIN)
                    .setCategory(NotificationCompat.CATEGORY_SERVICE)
                    .build();

            startForeground(127, notification);
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private String createNotificationChannel(NotificationManager notificationManager){
        String channelId = "my_service_channelid";
        String channelName = "Lock Screen Running";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        // omitted the LED color
        channel.setImportance(NotificationManager.IMPORTANCE_NONE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
        return channelId;
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags,
                              final int startId) {
        return START_STICKY;
    }

and on onDestroy() function I restart my service.

Manifests

<service android:name=".LockScreenService"
        android:process=":ServiceProcess"
        android:enabled="true"
        android:exported="false"/>
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

try this, if you want to get it in onResume()

    @Override
        protected void onResume() {
            super.onResume();
    
            Log.d(TAG, "onResume: GamePreferences.getPid()-------->   " + GamePreferences.getPid());
            Log.d(TAG, "onResume: android.os.Process.myPid()-------->   " + android.os.Process.myPid());
            if (GamePreferences.getPid() != 0) {
                if (GamePreferences.getPid() != android.os.Process.myPid()) {
                    Log.d(TAG, "GamePreferences.getPid() != android.os.Process.myPid(): -------->   " + android.os.Process.myPid());
    
                    //restart your service in foreground
                    return;
                }
         }
   }
over 4 years ago · Santiago Trujillo Relatório

0

According to the documentation, there is no guarantee onDestroy will be called. I could not find an explicit mention to what happens when the process is killed, but it seems that you are more likely to be called onStop. So you can try to start your service with an intent from onStop.

Also, there are documented ways to prevent your process to be elected, such as: having a related Activity running or having ongoing callbacks in BroadcastReceiver or Service.

Note well that your process might get killed by the user, and refusing to comply to the user's desire to kill is invasive. Therefore the best solution should be designed around the actual reason why a user would want your process to stay alive.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda