【问题标题】:How to know os kill my foreground service android如何知道操作系统杀死我的前台服务android
【发布时间】:2021-10-22 15:19:55
【问题描述】:

我做了一个锁屏应用。当操作系统终止小米红米 Note 10 Pro (MIUI 12) 中的服务时,我想重新启动我的服务。当服务被杀死时,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"/>

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    试试这个,如果你想在 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;
                    }
             }
       }
    

    【讨论】:

      【解决方案2】:

      根据the documentation,不能保证onDestroy会被调用。我找不到明确提及进程被终止时会发生什么,但似乎you are more likely to be called onStop。因此,您可以尝试以onStop 的意图启动您的服务。

      另外还有documented ways to prevent your process to be elected,比如:有一个相关的Activity在运行或者在BroadcastReceiver或者Service有正在进行的回调。

      请注意,您的进程可能会被用户杀死,而拒绝满足用户的杀死愿望是侵入性的。因此,最好的解决方案应该围绕用户希望您的进程保持活跃的实际原因来设计。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-05
        相关资源
        最近更新 更多