简体   繁体   English

Android SDK版本

[英]Android SDK version

I'm quite confused how application behaves when launched on different Android versions. 我很困惑应用程序在不同的Android版本上启动时的行为方式。

Let's say I'm compiling it using SDK level 4. Is it compiled in the code and when running it on phone with SDK 10 will still use the old SDK 4, or it will use SDK 10? 假设我正在使用SDK级别4进行编译。它是否在代码中编译,并且在使用SDK 10的手机上运行时仍将使用旧的SDK 4,否则它将使用SDK 10?

A simple example is Service's setForeground() -method, which was deprecated. 一个简单的例子是Service的setForeground() -method,它已被弃用。 If I run such code on phone with SDK 10, will the function have no effect or it will use the version from SDK 4? 如果我在带有SDK 10的手机上运行此类代码,该功能是否会起作用,还是会使用SDK 4中的版本?

The same goes for onStart() being deprecated. onStart()被弃用也是如此。 Is it going to be ignored on SDK 10 and it will try to call onStartCommand() ? 是否会在SDK 10上被忽略,它会尝试调用onStartCommand()

I'd be thankful if somebody could explain me what's gonna happen here and why. 如果有人能解释我将会发生什么以及为什么会这样,我会感激不尽。

Is it compiled in the code and when running it on phone with SDK 10 will still use the old SDK 4, or it will use SDK 10? 它是在代码中编译的,当在手机上运行时,SDK 10仍然会使用旧的SDK 4,还是会使用SDK 10?

SDK means Software Development Kit, thus devices do not run anything using the SDK. SDK意味着软件开发工具包,因此设备不使用SDK运行任何东西。 On the other hand, devices have different kind of OS which belongs to a specific API. 另一方面,设备具有属于特定API的不同种类的OS。

If you run a build compiled using SDK 4, and run it in a device which runs API 10, it will run it using the new API (that's called, backwards compatibility). 如果运行使用SDK 4编译的构建,并在运行API 10的设备中运行它,它将使用新API(称为向后兼容性)运行它。

Usually, deprecated functions are supported on newer versions of the SDK. 通常,较新版本的SDK支持不推荐使用的功能。 Most of them will work, but you must try to not use them. 他们中的大多数都可以使用,但你必须尝试不使用它们。 Of course, there are cases where you have no choice, for instance, when the method you are using is deprecated but there is no replacement in the old SDK that you are using. 当然,在某些情况下,您无法选择,例如,当您使用的方法已弃用但在您使用的旧SDK中没有替换时。

Your service is just compiled bytecode - with own classes and references to other classes and methods. 您的服务只是编译的字节码 - 具有自己的类和对其他类和方法的引用。 When it is deployed on newer android version, it (android, OS) will do whatever it sees fit - you have no influence there. 当它部署在较新的Android版本上时,它(android,OS)会做任何它认为合适的事情 - 你没有影响力。 As other java based systems, it will rely on proper class names / packages / interfaces, and try to resolve methods with certain signatures ( parameters and return values). 与其他基于java的系统一样,它将依赖于正确的类名/包/接口,并尝试使用某些签名(参数和返回值)来解析方法。 If it behaves socially, it would first check if there is method from newer interface ( ie onStartCommand() ), and if it is available it will be called. 如果它在社交方面表现,它将首先检查是否有来自较新接口的方法(即onStartCommand()),如果它是可用的,它将被调用。 Then it may (or not) fallback to older method. 然后它可能(或不)回退到旧方法。 But usually you can rely that reverse compatibility is maintained pretty long time. 但通常你可以依赖反向兼容性保持相当长的时间。

Most deprecated api calls on most phones will work... However, some, like your given example of setForeground() have been removed due to the fact that developers have misused it (setting a service into the foreground without notifying users). 在大多数手机上大多数已弃用的api调用都可以正常工作......但是,由于开发人员误用了它(在不通知用户的情况下将服务设置到前台),因此已删除某些内容,例如您给定的setForeground()示例。 So they WILL NOT work. 所以他们不会工作。 Unfortunately, there is no documentation to which ones are supported and which ones aren't so it's a trial and error thing... 不幸的是,没有哪些文档可以支持哪些,哪些不支持,这是一个反复试验的事情......

Also, there are differences between different implementations (phone manufacturers / models) for some reason. 此外,由于某种原因,不同的实现(电话制造商/型号)之间存在差异。 A good example is the old Contacts APi (pre V2.0) which works on some newer phones with Android 2.2/2.3 and doesn't work on others with the same OS versions... The best bet would be NOT to use deprecated methods, and to adjust according to the current popularity of the Android OS version (currently 2.1, 2.2, 2.3.2, 2.3.3 are the popular versions: http://developer.android.com/resources/dashboard/platform-versions.html ). 一个很好的例子是旧的Contacts APi(pre V2.0),它适用于一些使用Android 2.2 / 2.3的新手机,并且不适用于具有相同操作系统版本的其他手机......最好的选择是不要使用弃用的方法,并根据Android OS版本的当前流行度进行调整(目前2.1,2.2,2.3.2,2.3.3是流行版本: http//developer.android.com/resources/dashboard/platform-versions。 HTML )。

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

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