Android TargetApi 与 SuppressLint

作者: 我是吸血鬼 | 来源:发表于2016-07-31 10:20 被阅读1236次

    在android.annotation包中有两个抽象接口:

    TargetApi 与SupperessLint

    TargetApi :
    原文注释如下:
    Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.

    大概的意思是该注释告诉Lint工具把该类型当作指定API下运行,无论该该工程的target api是多少。

    在Android中不同版本会有一些差异,特别是在新版本中添加的函数在低版本中不存在,如果项目中minSdkVersion 指定版本为11,而targetVersion 版本指定为23。那么在@Override onProvideAssistContent( ) API 23方法就会出现如下警告:
    Call requires API level 23 (current min is 14):
    android.app.assist.AssistContent#setStructuredData less...

    在方法中添加@TargetApi(Build.VERSION_CODES.M)就不会出现这种错误。

    然而这样也会存在隐患,虽然在IDE中不会报错,编译也可以通过。在低版本的手机中会出现crash。
    如何解决这种情况呢?
    1、如果在低版本中无法另外实现该方法,那么最好还是放弃使用。
    2、如果在低版本中可以另外实现,针对版本进行判断,在高版本中实现系统方案在低版本中实现自定义的方案。

    SupperessLint
    原文注释如下:
    Indicates that Lint should ignore the specified warnings for the annotated element.

    大概的意思是告诉Lint工具忽略指定的警告针对注释元素。
    上面的问题也可以通过@SupperessLint("NewApi")来解决,但是该注解并没有说明NewApi的具体值,这个模糊的标识不建议使用。

    相关文章

      网友评论

        本文标题:Android TargetApi 与 SuppressLint

        本文链接:https://www.haomeiwen.com/subject/jeitsttx.html