是否可以在特定API级别访问方法?

时间:2014-09-08 21:39:39

标签: android

是否可以在特定API级别的类中标记方法? 例如,我有两种方法:foo1(int param1)foo2(int param1, int param2)。我希望foo2方法需要特定的API级别。设为19,如果我使用min sdk 8,我将收到有关API要求的错误。对于cource,我总是可以做 if(api_level == 19)foo2();别的foo1()。但我希望得到一些保护,以排除可能的错误。

1 个答案:

答案 0 :(得分:1)

我实际上正在做类似的事情我建议使用版本构建的switch case这是一个例子(我只是列出了所有版本级别,不推荐将所有版本级别添加到你想要的版本中)。 p>

switch (Build.VERSION.SDK_INT) {
    case Build.VERSION_CODES.BASE: // API level 1
        // Code or method goes here
        break;
    case Build.VERSION_CODES.BASE_1_1: // API level 2
        Toast.makeText(this, "Base update version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.CUPCAKE: // API level 3
        Toast.makeText(this, "Cupcake version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.CUR_DEVELOPMENT: // API level 4
        Toast.makeText(this, "Cur development version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.DONUT: // API level 5
        Toast.makeText(this, "Donut version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.ECLAIR: // API level 6
        Toast.makeText(this, "Eclair version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.ECLAIR_0_1: // API level 7
        Toast.makeText(this, "Eclair 0 1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.ECLAIR_MR1: // API level 8
        Toast.makeText(this, "Eclair MR1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.FROYO: // API level 9
        Toast.makeText(this, "Froyoooo version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.GINGERBREAD: // API level 10
        Toast.makeText(this, "Gingerbread version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.GINGERBREAD_MR1: // API level 11
        Toast.makeText(this, "Gingerbread MR1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.HONEYCOMB: // API level 12
        Toast.makeText(this, "Honeycomb version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.HONEYCOMB_MR1: // API level 13
        Toast.makeText(this, "Homecomb MR1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.HONEYCOMB_MR2:
        Toast.makeText(this, "Honeycomb MR2 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.ICE_CREAM_SANDWICH: // API level 14
        Toast.makeText(this, "Ice Cream Sandwich version",Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1: // API level 15
        Toast.makeText(this, "Ice Cream Sandwich MR1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.JELLY_BEAN: // API level 16
        Toast.makeText(this, "Jelly Bean version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.JELLY_BEAN_MR1: // API level 17
        Toast.makeText(this, "Jelly Bean MR1 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.JELLY_BEAN_MR2: // API level 18
        Toast.makeText(this, "Jelly Bean MR2 version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.KITKAT: // API level 19
        Toast.makeText(this, "Kitkat version", Toast.LENGTH_LONG).show();
        break;
    case Build.VERSION_CODES.KITKAT_WATCH:
        Toast.makeText(this, "Kitkat Watch version", Toast.LENGTH_LONG).show();
        break;
}