在我的应用程序中,我正在使用SharedPreferences来存储一些用户首选项。该应用程序没有被混淆(在proguard文件中为-dontobfuscate)。
现在在该应用程序的下一版本中,我要启用混淆功能。当我尝试此操作时,该应用程序在从以前版本的应用程序中读取SharedPreferences数据时返回NullPointerException。错误日志无济于事,因为代码已经被混淆并且它不提供有意义的信息。但是,在调试模式下尝试时,我发现崩溃可能是由于空上下文(这是代码中的静态变量)造成的!事实并非如此,因为如果SharedPreferences尚不存在,则应用程序会运行文件。
应用程序是否仍可以通过未混淆的版本读取SharedPreferences数据?
编写/读取SharedPreferences是相当标准的:写作:
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.putString("userUnitsOption", "C");
//apply the storage
prefsEditor.apply();
阅读:
final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
return mPrefs.getString("userUnitsOption", "A");
如果要在Android中使用共享的首选项,请按照以下说明使用它:
首先,您必须像这样存储您的首选项:
SharedPreferences.Editor editor = getSharedPreferences("mySharedPref", MODE_PRIVATE).edit();
editor.putString("myName", "abc");
editor.apply();
现在,要读取或获取那些存储的共享首选项,请编写如下所示的代码:
SharedPreferences prefs = MainActivity.this.getSharedPreferences("mySharedPref", MODE_PRIVATE); // Here MainActivity.this represent the context. So you can use your context in place of MainActivity.this
String strName = prefs.getString("myName","defaultName");
在科特林,
用法:私有val sharedPref = defaultPrefs(this)
要保存数据-> sharedPref[KEY] = *String data to save*
要获取数据-> val userDetails = sharedPref[KEY, ""]
创建如下所示的共享首选项帮助程序类。
object PreferenceHelper {
fun defaultPrefs(context: Context): SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
fun customPrefs(context: Context, name: String): SharedPreferences =
context.getSharedPreferences(name, Context.MODE_PRIVATE)
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
/**
* puts a key value pair in shared prefs if doesn't exists, otherwise updates value on given [key]
*/
operator fun SharedPreferences.set(key: String, value: Any?) {
when (value) {
is String? -> edit { it.putString(key, value) }
is Int -> edit { it.putInt(key, value) }
is Boolean -> edit { it.putBoolean(key, value) }
is Float -> edit { it.putFloat(key, value) }
is Long -> edit { it.putLong(key, value) }
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
/**
* finds value on given key.
* [T] is the type of value
* @param defaultValue optional default value - will take null for strings, false for bool and -1 for numeric values if [defaultValue] is not specified
*/
inline operator fun <reified T : Any> SharedPreferences.get(
key: String,
defaultValue: T? = null
): T? {
return when (T::class) {
String::class -> getString(key, defaultValue as? String) as T?
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T?
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T?
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T?
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T?
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
}