Android 外部更改时重新加载SharedReferences(不终止应用程序)
我正在备份共享的\u prefs,它已成功恢复,但在重新加载活动(finish();staticivity(…);)时不会更新值Android 外部更改时重新加载SharedReferences(不终止应用程序),android,sharedpreferences,Android,Sharedpreferences,我正在备份共享的\u prefs,它已成功恢复,但在重新加载活动(finish();staticivity(…);)时不会更新值需要终止它,然后它将加载值。。。。 这是我的密码: public pFunctions (Context context ) { this.context = context; initPrefs(); } private void initPrefs() { settings = Prefere
需要终止它,然后它将加载值。。。。 这是我的密码:
public pFunctions (Context context ) {
this.context = context;
initPrefs();
}
private void initPrefs() {
settings = PreferenceManager.getDefaultSharedPreferences(context);
settings_edit = settings.edit();
S_UNLOCK_FINGER = settings.getBoolean("finger_unlock", false);
S_BACKUP_SAVE = settings.getBoolean("backup_saves", true);
......
}
每次,在创建或刷新活动时,我都将此函数称为
prefs = new pFunctions(this);
但价值观保持不变,直到应用程序如上文所述被杀死我的问题是:一旦xml资源文件(在data/data/../shared\u prefs中)被更改或删除,然后手动添加,如何重新加载首选项?根据Android文档,您需要使用
模式\u MULTI\u PROCESS
模式通知磁盘对共享首选项的更改,无论如何,这种方法现在已经被弃用,但这是实现这一过程的可能方法之一
public static boolean reloadSharedPreference(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS)
.edit()
.putBoolean("RELOAD", true)
.commit();
}
请参阅Android文档:
/**
* SharedPreference loading flag: when set, the file on disk will
* be checked for modification even if the shared preferences
* instance is already loaded in this process. This behavior is
* sometimes desired in cases where the application has multiple
* processes, all writing to the same SharedPreferences file.
* Generally there are better forms of communication between
* processes, though.
*
* <p>This was the legacy (but undocumented) behavior in and
* before Gingerbread (Android 2.3) and this flag is implied when
* targeting such releases. For applications targeting SDK
* versions <em>greater than</em> Android 2.3, this flag must be
* explicitly set if desired.
*
* @see #getSharedPreferences
*
* @deprecated MODE_MULTI_PROCESS does not work reliably in
* some versions of Android, and furthermore does not provide any
* mechanism for reconciling concurrent modifications across
* processes. Applications should not attempt to use it. Instead,
* they should use an explicit cross-process data management
* approach such as {@link android.content.ContentProvider ContentProvider}.
*/
@Deprecated
public static final int MODE_MULTI_PROCESS = 0x0004;
/**
*SharedReference加载标志:设置后,磁盘上的文件将
*即使共享首选项
*实例已在此进程中加载。这种行为是错误的
*有时需要在应用程序具有多个
*进程,所有进程都写入同一个SharedReferences文件。
*一般来说,双方之间有更好的沟通方式
*然而,过程。
*
*这是和中的遗留(但未记录)行为
*在Gingerbread(Android 2.3)之前,当
*针对此类释放。适用于以SDK为目标的应用程序
*大于Android 2.3的版本,此标志必须为
*如果需要,显式设置。
*
*@see#GetSharedReferences
*
*@deprecated MODE_MULTI_进程在中无法可靠工作
*Android的一些版本,而且不提供任何
*跨平台协调并发修改的机制
*过程。应用程序不应尝试使用它。相反
*他们应该使用明确的跨流程数据管理
*方法,例如{@link android.content.ContentProvider ContentProvider}。
*/
@不赞成
公共静态最终整数模式多进程=0x0004;
根据Android文档,您需要使用模式\u多进程
模式来通知磁盘对共享首选项的更改,无论如何,此方法现在已被弃用,但这是实现此过程的可能方法之一
public static boolean reloadSharedPreference(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS)
.edit()
.putBoolean("RELOAD", true)
.commit();
}
请参阅Android文档:
/**
* SharedPreference loading flag: when set, the file on disk will
* be checked for modification even if the shared preferences
* instance is already loaded in this process. This behavior is
* sometimes desired in cases where the application has multiple
* processes, all writing to the same SharedPreferences file.
* Generally there are better forms of communication between
* processes, though.
*
* <p>This was the legacy (but undocumented) behavior in and
* before Gingerbread (Android 2.3) and this flag is implied when
* targeting such releases. For applications targeting SDK
* versions <em>greater than</em> Android 2.3, this flag must be
* explicitly set if desired.
*
* @see #getSharedPreferences
*
* @deprecated MODE_MULTI_PROCESS does not work reliably in
* some versions of Android, and furthermore does not provide any
* mechanism for reconciling concurrent modifications across
* processes. Applications should not attempt to use it. Instead,
* they should use an explicit cross-process data management
* approach such as {@link android.content.ContentProvider ContentProvider}.
*/
@Deprecated
public static final int MODE_MULTI_PROCESS = 0x0004;
/**
*SharedReference加载标志:设置后,磁盘上的文件将
*即使共享首选项
*实例已在此进程中加载。这种行为是错误的
*有时需要在应用程序具有多个
*进程,所有进程都写入同一个SharedReferences文件。
*一般来说,双方之间有更好的沟通方式
*然而,过程。
*
*这是和中的遗留(但未记录)行为
*在Gingerbread(Android 2.3)之前,当
*针对此类释放。适用于以SDK为目标的应用程序
*大于Android 2.3的版本,此标志必须为
*如果需要,显式设置。
*
*@see#GetSharedReferences
*
*@deprecated MODE_MULTI_进程在中无法可靠工作
*Android的一些版本,而且不提供任何
*跨平台协调并发修改的机制
*过程。应用程序不应尝试使用它。相反
*他们应该使用明确的跨流程数据管理
*方法,例如{@link android.content.ContentProvider ContentProvider}。
*/
@不赞成
公共静态最终整数模式多进程=0x0004;
“对首选项的修改必须通过SharedReferences.Editor对象,以确保首选项值保持一致状态,并在提交到存储时进行控制”(来自)。好了,并没有办法去做你们想做的事情。但一旦应用程序被关闭->再次启动,它就会被加载。我的意思是:1。我打开应用程序2。连接到驱动器并还原备份。。。它将替换xml资源,但即使再次调用DefaultSharedPref,效果也不会应用于活动。它需要应用程序重新启动,然后我先前下载的xml资源成功应用于应用程序。。。那么编辑器在这里扮演什么角色呢?“那么编辑器在这里扮演什么角色?”--SharedReferences
仅支持通过编辑器更改值。您正在通过其他方式更改值SharedReferences
不支持这一点。因此,我没有其他选择,然后数据库存储值“对首选项的修改必须通过SharedReferences.Editor对象,以确保首选项值保持一致状态,并控制它们何时提交到存储”(从)。好了,并没有办法去做你们想做的事情。但一旦应用程序被关闭->再次启动,它就会被加载。我的意思是:1。我打开应用程序2。连接到驱动器并还原备份。。。它将替换xml资源,但即使再次调用DefaultSharedPref,效果也不会应用于活动。它需要应用程序重新启动,然后我先前下载的xml资源成功应用于应用程序。。。那么编辑器在这里扮演什么角色呢?“那么编辑器在这里扮演什么角色?”--SharedReferences
仅支持通过编辑器更改值。您正在通过其他方式更改值SharedReferences
不支持这一点。所以,除了数据库之外,我没有其他选择来存储值