1.问题描述:
OTA升级从高版本升级到低版本成功,开机后桌面闹钟图标不见了,且桌面闹钟报错,无法打开应用。
参考log如下:
01-14 20:29:22.290: E/AndroidRuntime(2722): FATAL EXCEPTION: AsyncHandler
01-14 20:29:22.290: E/AndroidRuntime(2722): Process: com.android.deskclock, PID: 2722
01-14 20:29:22.290: E/AndroidRuntime(2722): android.database.sqlite.SQLiteException: Can't downgrade database from version 10 to 7
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:361)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:255)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
01-14 20:29:22.290: E/AndroidRuntime(2722): at com.android.deskclock.provider.ClockProvider.query(ClockProvider.java:96)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.content.ContentProvider.query(ContentProvider.java:922)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.content.ContentProvider$Transport.query(ContentProvider.java:213)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.content.ContentResolver.query(ContentResolver.java:462)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.content.ContentResolver.query(ContentResolver.java:405)
01-14 20:29:22.290: E/AndroidRuntime(2722): at com.android.deskclock.provider.AlarmInstance.getInstances(AlarmInstance.java:188)
01-14 20:29:22.290: E/AndroidRuntime(2722): at com.android.deskclock.alarms.AlarmStateManager.FixAlarmInstancesForReboot(AlarmStateManager.java:684)
01-14 20:29:22.290: E/AndroidRuntime(2722): at com.android.deskclock.AlarmInitReceiver$1.run(AlarmInitReceiver.java:102)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.os.Handler.handleCallback(Handler.java:808)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.os.Handler.dispatchMessage(Handler.java:103)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.os.Looper.loop(Looper.java:193)
01-14 20:29:22.290: E/AndroidRuntime(2722): at android.os.HandlerThread.run(HandlerThread.java:61)
2.分析结果:
从上面的log中可以看到,此类异常由数据库无法将降级导致,此时恢复出厂设置可以恢复正常。
3.解决方案
可通过在DatabaseHelper.java重写父类SQLiteOpenHelper.java的onDowngrade()方法来规避该问题,或者直接在SQLiteOpenHelper.java中对该方法就行修改。
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
throw new SQLiteException("Can't downgrade database from version " +
oldVersion + " to " + newVersion);
}
注:允许数据库降级,前提是对数据库版本号毫不关心。因此建议保持平台默认设计,不应该去规避该问题,因为:
You should care about this "Database Version thing" if ever your database schema is ever going to change