当前位置:首页 > Windows程序 > 正文

(转)在低版本的SDK里使用高版本函数@SuppressLint(NewApi) or @TargetApi?

2021-05-25 Windows程序

@SuppressLint 和 @TargetApi达到的效果是一样的,相对于SuppressLint ,,TargetApi会根据函数里使用的API,严格匹配SDK版本,给出编译错误,但是SuppressLint 则忽略了SDK版本。

例如:

[java] view plaincopy

 

     @TargetApi(Build.VERSION_CODES.FROYO)  

    public static File getExternalCacheDir(Context context) {  

        File dir;  

//        if (!VersionUtils.hasDonut()) {  

        if (!VersionUtils.hasFroyo()) {  

            dir = new File(Environment.getExternalStorageDirectory().getPath()  

                    + "/Android/data/" + context.getPackageName() + "/cache/");  

            if (!dir.exists() && !dir.mkdirs())  

                dir = null;  

        } else {  

            dir = context.getExternalCacheDir();  

        }  

        return dir;  

    }  

如果把VersionUtils.hasFroyo()改成VersionUtils.hasDonut()则会报编译错误,如果是@SuppressLint("NewApi") 则不会提示错误。比较严谨的角度讲,更加推荐TargetApi

原文地址:

(转)在低版本的SDK里使用高版本函数@SuppressLint("NewApi") or @TargetApi?

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70613.html