START_STICKY_COMPATIBILITY in Services

Dif*_*ffy 7 android

什么是Android服务方面的START_STICKY_COMPATIBILITY标志.文档提到了它

START_STICKY的兼容版本,不保证在被杀死后再次调用onStartCommand(Intent,int,int).

什么是兼容版本?如果它是一个版本START_STICKY,那么为什么onStartCommand()不能保证调用呢?为什么有人会在它不能保证onStartCommand()在服务被杀之后被调用时使用它?

cse*_*nga 11

默认实现onStartCommand:

  public @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) {
        onStart(intent, startId);
        return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
    }
Run Code Online (Sandbox Code Playgroud)

mStartCompatibility是这样确定的:

 mStartCompatibility = getApplicationInfo().targetSdkVersion
                < Build.VERSION_CODES.ECLAIR;
Run Code Online (Sandbox Code Playgroud)

在1.6版本的Service不存在的执行onStartCommandonStart.在2.1版本中,他们onStart不推荐使用.请注意参数的不同之处flags.

通过这样做,他们将保持与旧系统(PRE Eclair)的兼容性,旧系统期望旧值,并且它们也支持新系统中的新行为.