我可以在运行时获取使用过的TargetSDKVersion吗?
这是因为Android API 19中的WebView>处理像素的方式与19之前不同。
这是一个库,所以我不想让开发人员手动输入它。
我的评论: 我正在使用我的Nexus 5 API 21 Lollipop。更改TargetSDKVersion会改变html的javascript读取宽度的方式,屏幕密度的倍数。我刚刚将其更改为14然后更改为19,我确认了这一点。
答案 0 :(得分:11)
就我而言,我已经完成了这个
int targetSdkVersion = getApplicationContext().getApplicationInfo().targetSdkVersion;
答案 1 :(得分:7)
About target SDK version, look to the ApplicationInfo class(从here获取)
int version = 0;
IPackageManager pm = AppGlobals.getPackageManager();
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(yourAppName, 0);
if (applicationInfo != null) {
version = applicationInfo.targetSdkVersion;
}
}
或强>
如果我们谈论设备操作系统版本
Build class contain information about version
android.os.Build.VERSION.SDK_INT
答案 2 :(得分:7)
这是使用targetSdkVersion
在运行时获取应用程序Android Studio
的另一种方法:
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;
}
catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
targetSDKVersion
的值已定义到我的build.gradle
文件
defaultConfig {
applicationId "com.tuna.hello.androidstudioapplication"
minSdkVersion 9
targetSdkVersion 22
versionCode 12
versionName "1.0"
}
答案 3 :(得分:2)
在Android Marshmallow(API 23)手机中使用以下配置:
defaultConfig {
applicationId "com.mytestapp"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0.0"
}
获取实际正在运行的应用 targetSdkVersion的另一种直接方法是:
Context mContext = getApplicationContext();
int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; // Outputs 22
int mobileSdkVersion = Build.VERSION.SDK_INT; // Outputs 23