2016-10-10 35 views
2

假設targetSdkVersionx,和minSdkVersionyx > y),且是一種方法,其在x過時,但在y替代一個不被支持。如何解決這個問題呢?如何在面向Android的多API級別中使用棄用的方法?

例如,new SoundPool(int, int, int)不贊成在API級別21,而另一種方法是使用SoundPool.Builder,但SoundPool.Builder不能在API級0​​使用。

在這種情況下該怎麼辦?

+0

使用不推薦使用的方法,至少在較早的設備版本上。 – CommonsWare

回答

3

你可以做的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 
} 
3

寫一個函數,即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); 
    } 

} 
相關問題