【问题标题】:Clear Shared Preferences on App upgrade清除应用升级时的共享偏好
【发布时间】:2012-01-30 13:31:00
【问题描述】:

在我的

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

DBHelper 函数(实现 SQLite 开放助手)我提供 sql 查询以根据我的新版本进行修改。

但我还想在升级时清除我的一些共享首选项详细信息。 但是打开 sharedPreferences 进行编辑需要 Context 对象,我这里怎么提供呢..?

【问题讨论】:

  • DatabaseOpenHelper 在构造函数中需要一个上下文,那你为什么不把上下文保存为你的DatabaseOpenHelper 的一个属性以便以后使用呢?

标签: android sharedpreferences


【解决方案1】:
protected class DatabaseOpenHelper extends SQLiteOpenHelper {
    private Context mContext;

        public DatabaseOpenHelper(Context context) {
        mContext = context;
            super(context, databaseName, null, 1);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // do what ever you want with mContext...

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多