简体   繁体   English

如何知道 os 杀死我的前台服务 android

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

I made lock screen app I want to restart my service when os kill the service in xiaomi redmi note 10 pro (miui 12) when service is killed onDestroy not call.我制作了锁屏应用程序,我想在 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;
}

and on onDestroy() function i restart my service在 onDestroy() 函数上,我重新启动了我的服务

Manifests清单

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

try this, if you want to get it in onResume()试试这个,如果你想在 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;
                }
         }
   }

According to the documentation , there is no guarantee onDestroy will be called.根据文档,不能保证onDestroy会被调用。 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 .我找不到对进程被终止时会发生什么的明确提及,但似乎您更有可能被称为onStop So you can try to start your service with an intent from onStop .因此,您可以尝试使用来自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 .此外,有记录的方法可以防止您的进程被选中,例如:运行相关的Activity或在BroadcastReceiverService中进行持续回调。

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.因此,最好的解决方案应该围绕用户希望您的流程保持活动的实际原因进行设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM