@TargetApi注释仅适用于一个Api级别或更高级别吗? [英] Is @TargetApi annotation just for one Api level or above?

查看:72
本文介绍了@TargetApi注释仅适用于一个Api级别或更高级别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用@TargetApi(23).

I am using @TargetApi(23) in my app.

@TargetApi(23)
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        onAttachToContext(context);
    }


    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            onAttachToContext(activity);
        }
    }

    protected void onAttachToContext(Context context) {

    }

但是我听不懂:@TargetApi(23)注释的意思是仅适用于Api 23级"还是适用于Api 23级及以上"?例如,如果设备24的Api级别被调用了onAttach(Context context)方法?

But i can not understand something: @TargetApi(23) annotation's mean "just for Api level 23" or "for Api level 23 and above" ? For example if Api level of device 24, is onAttach(Context context) method called?

推荐答案

TargetApi 注释仅用于皮棉工具,在运行时没有结果.如果您使用的方法中只有23种可用的API方法并且未声明TargetApi,您将收到一些警告,表明您使用的API在最低SDK版本中不可用.您有责任在具有一致性的情况下调用此方法,并且要知道将要从中调用该API的级别.

TargetApi annotation is just for lint tool purposes and has no outcome in runtime. If you use any API methods just available on 23 within your method and don't declare the TargetApi, you will just get some warnings indicating you're using API's not available in your minimum SDK version. It's your responsibility to call this method with coherence being aware of the API level it will be called from.

这篇关于@TargetApi注释仅适用于一个Api级别或更高级别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆