簡體   English   中英

如何知道 os 殺死我的前台服務 android

[英]How to know os kill my foreground service android

我制作了鎖屏應用程序,我想在 xiaomi redmi note 10 pro (miui 12) 中的 os 殺死服務時重新啟動我的服務,當服務被殺死 onDestroy 不調用時。

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;
}

在 onDestroy() 函數上,我重新啟動了我的服務

清單

<service android:name=".LockScreenService"
        android:process=":ServiceProcess"
        android:enabled="true"
        android:exported="false"/>

試試這個,如果你想在 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;
                }
         }
   }

根據文檔,不能保證onDestroy會被調用。 我找不到對進程被終止時會發生什么的明確提及,但似乎您更有可能被稱為onStop 因此,您可以嘗試使用來自onStop的意圖啟動您的服務。

此外,有記錄的方法可以防止您的進程被選中,例如:運行相關的Activity或在BroadcastReceiverService中進行持續回調。

請注意,您的進程可能會被用戶殺死,而拒絕滿足用戶殺死的願望是侵入性的。 因此,最好的解決方案應該圍繞用戶希望您的流程保持活動的實際原因進行設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM