Hice una aplicación de pantalla de bloqueo. Quiero reiniciar mi servicio cuando el sistema operativo elimine el servicio en Xiaomi Redmi Note 10 Pro (MIUI 12). Cuando se elimina el servicio, onDestroy no se llama.
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; } y en la función onDestroy() reinicio mi servicio.
Manifiestos
<service android:name=".LockScreenService" android:process=":ServiceProcess" android:enabled="true" android:exported="false"/>prueba esto, si quieres obtenerlo en 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; } } }De acuerdo con la documentación , no hay garantía de que se onDestroy a Destroy. No pude encontrar una mención explícita de lo que sucede cuando se cancela el proceso, pero parece que es más probable que lo llamen onStop . Entonces puede intentar iniciar su servicio con una intención de onStop .
Además, hay formas documentadas de evitar que se elija su proceso , como: tener una Activity relacionada en ejecución o tener devoluciones de llamadas en curso en BroadcastReceiver o Service .
Tenga en cuenta que su proceso puede ser asesinado por el usuario, y negarse a cumplir con el deseo de matar del usuario es invasivo. Por lo tanto, la mejor solución debe diseñarse en torno a la razón real por la que un usuario querría que su proceso se mantuviera vivo.