[英]Saving Preferences throws exception in Android
I am trying to save user preferences in Android with the following code:我正在尝试使用以下代码在 Android 中保存用户首选项:
import java.util.prefs.Preferences;
class MyClass {
void save() throws Exception {
_prefs.put("NAME","VALUE");
_prefs.flush(); // Throws exception
}
private final Preferences _prefs = Preferences.userNodeForPackage(MyClass.class);
}
... but the "flush" call generates the following exception: ...但“flush”调用会产生以下异常:
W/java.util.prefs: Could not lock User prefs. Unix error code 2.
W/System.err: java.util.prefs.BackingStoreException: Couldn't get file lock.
I guessed it was a problem with permissions.我猜是权限问题。 So I added the following to AndroidManifest.xml
所以我在 AndroidManifest.xml 中添加了以下内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
But I still get the exception.但我仍然得到例外。
How can I save user preferences in Android with the java.util.prefs.Preferences class?如何使用 java.util.prefs.Preferences 类在 Android 中保存用户首选项?
read some doc , especially WARNING section阅读一些文档,尤其是警告部分
On Android, the Preference nodes corresponding to the "system" and "user" preferences are stored in sections of the file system that are inaccessible to apps.
在 Android 上,对应于“系统”和“用户”首选项的首选项节点存储在应用程序无法访问的文件系统部分中。 Further, allowing apps to set "system wide" preferences is contrary to android's security model.
此外,允许应用程序设置“系统范围”首选项与 android 的安全模型相反。
instead of Preferences
use SharedPreferences
, some doc about in HERE而不是
Preferences
使用SharedPreferences
,一些关于这里的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.