【问题标题】:Backwards compatibility and methods in AndroidAndroid中的向后兼容性和方法
【发布时间】:2017-12-26 04:37:01
【问题描述】:

我目前正在更新我维护的一个库,我想提供一个在方法签名中使用MediaDataSource 的方法,但这仅在 API 23+ 中可用。我知道 Android 文档声明您应该通过以下检查确保向后兼容性:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // some API specific code

我也知道可以根据文件夹命名自定义资源,例如layout-v13。我的问题是,是否可以添加此类检查或类似的检查,以使我的代码仍可在

@Version Build.VERSION_CODES.HONEYCOMB // not real code, just what I'm thinking
public void setData(MediaDataSource mediaDataSource) {
    // some code
}

【问题讨论】:

    标签: java android backwards-compatibility


    【解决方案1】:

    是的,通常当您遇到 API 兼容性问题时,当您在警告上按 alt+enter 时,Android Studio 会为您提供多种解决方案。

    以 Android 中的 NotificationChannel 为例,它仅适用于 Oreo 用户 (API 26)。您可以针对他们进行以下操作。

    选项 1:If else 语句 您已经在问题中提到了这一点

    选项 2:@TargetAPI 注释

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotification() {
    NotificationChannel notificationChannel = new NotificationChannel("123", "newNotification", NotificationManager.IMPORTANCE_DEFAULT);}
    

    选项 3:@RequiresAPI 注释

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotification() {
    NotificationChannel notificationChannel = new NotificationChannel("123", "newNotification", NotificationManager.IMPORTANCE_DEFAULT);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多