假設targetSdkVersion
是x
,和minSdkVersion
是y
(x > y
),且是一種方法,其在x
過時,但在y
替代一個不被支持。如何解決這個問題呢?如何在面向Android的多API級別中使用棄用的方法?
例如,new SoundPool(int, int, int)
不贊成在API級別21
,而另一種方法是使用SoundPool.Builder
,但SoundPool.Builder
不能在API級0使用。
在這種情況下該怎麼辦?
假設targetSdkVersion
是x
,和minSdkVersion
是y
(x > y
),且是一種方法,其在x
過時,但在y
替代一個不被支持。如何解決這個問題呢?如何在面向Android的多API級別中使用棄用的方法?
例如,new SoundPool(int, int, int)
不贊成在API級別21
,而另一種方法是使用SoundPool.Builder
,但SoundPool.Builder
不能在API級0使用。
在這種情況下該怎麼辦?
你可以做的OS版本運行時檢查,然後使用方法已過時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
}
寫一個函數,即acccepts necassary參數並根據SDK版本輸出SoundPool
對象:
public SoundPool build(int, int, int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new SoundPool.Builder()
//setParameters
.build();
} else {
return new SoundPool(int, int, int);
}
}
使用不推薦使用的方法,至少在較早的設備版本上。 – CommonsWare