假设targetSdkVersion
为x
,minSdkVersion
为y
(x > y
),还有一种方法,在 x
中已弃用,但在 y
中不支持替代方法。如何解决这个问题?
例如,new SoundPool(int, int, int)
在 API 级别 21
中已弃用,替代方法是使用 SoundPool.Builder
,但 SoundPool.Builder
不能在 API 级别 14
中使用。
这种情况怎么办?
最佳答案
您可以对操作系统版本进行运行时检查,然后在旧版本中使用已弃用的方法 new SoundPool(int, int, int)
,而在新版本中您可以使用 SoundPool.Builder
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.<YOUR_VERSION_X>) {
//use old method
} else {
//use new method
}
关于android - 如何在面向 Android 的多 API 级别中使用已弃用的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960097/