为什么应用程序不会在 6.0 (API23) 平台上崩溃,因为不推荐使用支持库的 getResources().getColor()?
Why app is not crashing on 6.0 (API23) platform for deprecated getResources().getColor() without support library?
我决定检查如果 运行 应用不推荐使用支持库的方法会发生什么。我预计它会崩溃。我对设备的 Android 6.0 (API23) 平台使用了已弃用的 getResources().getColor()
方法。
为什么它不崩溃? (就好像后面的平台支持库支持前面的一样)
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getResources().getColor(R.color.colorAccent);
}
}
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "scanandbuy.com"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//compile 'com.android.support:appcompat-v7:25.3.1'
//compile 'com.android.support:design:25.3.1'
}
Why doesn't it crash?
为什么会这样?
"Deprecated" 并不代表 "will crash"。 "Deprecated" 表示 "there is a replacement that we would like you to strongly consider using, on the API levels where the replacement exists".
有时,已弃用的方法也会有行为更改。通常,这是出于隐私或安全原因,而且通常情况下,更改的行为不是崩溃,而是更良性的事情(例如,返回一个空结果集,而不是您之前会得到的结果)。检索颜色资源值没有我能想到的任何隐私或安全问题。
我决定检查如果 运行 应用不推荐使用支持库的方法会发生什么。我预计它会崩溃。我对设备的 Android 6.0 (API23) 平台使用了已弃用的 getResources().getColor()
方法。
为什么它不崩溃? (就好像后面的平台支持库支持前面的一样)
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getResources().getColor(R.color.colorAccent);
}
}
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "scanandbuy.com"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//compile 'com.android.support:appcompat-v7:25.3.1'
//compile 'com.android.support:design:25.3.1'
}
Why doesn't it crash?
为什么会这样?
"Deprecated" 并不代表 "will crash"。 "Deprecated" 表示 "there is a replacement that we would like you to strongly consider using, on the API levels where the replacement exists".
有时,已弃用的方法也会有行为更改。通常,这是出于隐私或安全原因,而且通常情况下,更改的行为不是崩溃,而是更良性的事情(例如,返回一个空结果集,而不是您之前会得到的结果)。检索颜色资源值没有我能想到的任何隐私或安全问题。