简体   繁体   English

Android:运行时获取TargetSDKVersion

[英]Android: Get TargetSDKVersion in runtime

Can I get the used TargetSDKVersion in runtime?我可以在运行时获取使用的 TargetSDKVersion 吗?

This is due to the fact that the WebView in Android API 19> handles pixels differently than pre 19.这是因为 Android API 19> 中的 WebView 处理像素的方式与 pre 19 不同。

This is for a library and so I would not like to have the developer enter it manually.这是一个库,所以我不想让开发人员手动输入它。

My comment: I am using my Nexus 5 API 21 Lollipop.我的评论:我正在使用我的 Nexus 5 API 21 Lollipop。 Changing TargetSDKVersion changes the way javascript of the html reads the widths by a multiple of the screen density.更改 TargetSDKVersion 会更改 html 的 javascript 以屏幕密度的倍数读取宽度的方式。 I have just changed it to 14 then to 19, and I confirm this.我刚刚将其更改为 14,然后更改为 19,我确认了这一点。

在我的情况下,我做到了这一点

int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion;

About target SDK version, look to the ApplicationInfo class (get it from here ) 关于目标SDK版本,请查看ApplicationInfo类 (从此处获取)

int version = 0;
IPackageManager pm = AppGlobals.getPackageManager();
try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo(yourAppName, 0);
    if (applicationInfo != null) {
      version = applicationInfo.targetSdkVersion;
    }
}

OR 要么

If we talk about device OS version 如果我们谈论设备操作系统版本

Build class contain information about version 构建类包含有关版本的信息

android.os.Build.VERSION.SDK_INT

This is another way to get the targetSdkVersion of my application in runtime, using Android Studio : 这是使用Android Studio在运行时获取应用程序的targetSdkVersion的另一种方法:

try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;

        }
        catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }

the value of targetSDKVersion is defined into my build.gradle file targetSDKVersion的值被定义到我的build.gradle文件中

   defaultConfig {
        applicationId "com.tuna.hello.androidstudioapplication"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 12
        versionName "1.0"
    }

Having the following config in an Android Marshmallow (API 23) phone: 在Android Marshmallow(API 23)手机中使用以下配置:

defaultConfig {
      applicationId "com.mytestapp"
      minSdkVersion 9
      targetSdkVersion 22
      versionCode 1
      versionName "1.0.0"
}

Another direct approach to get the actual running app targetSdkVersion is: 获取实际运行的app targetSdkVersion的另一种直接方法是:

Context mContext = getApplicationContext();
int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; // Outputs 22
int mobileSdkVersion = Build.VERSION.SDK_INT; // Outputs 23

You can even set it in runtime:您甚至可以在运行时设置它:

        applicationInfo.targetSdkVersion = 32

This may work for example if some external api (WebView in my case) works differently depending of your app's target api and you need to restore previous behaviour on earlier platforms (in that case dont forget to wrap this with if (Build.VERSION.SDK_INT <= xxx)这可能会起作用,例如,如果某些外部 api(在我的例子中是 WebView)根据您的应用程序的目标 api 工作不同并且您需要在早期平台上恢复以前的行为(在这种情况下不要忘记用 if (Build.VERSION.SDK_INT 包装它<= xxx)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM