Android的appwidget有时会改变preferences回默认 [英] Android appwidget sometimes changes preferences back to default

查看:144
本文介绍了Android的appwidget有时会改变preferences回默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇在Android市场上发布appwidgets,他们工作得很好大部分。不过,我已经注意到,preferences,使用共享preferences保存,有时重置为默认值。在这种情况下,缺省值是硬$ C $光盘变量。作为保存在XML中的共享preferences保持不变为保存用户。

I have a couple of appwidgets released on the Android market and they work fine for the most part. However I have noticed that the preferences, saved using shared preferences, sometimes reset to the defaults. The defaults in this case are hard coded variables. The shared preferences as saved in an xml remain the same as saved by the user.

在一个核查的情况下的上述问题,以及其它的东西,如其中停止响应按钮,重新启动的设备后发生。我固定通过创建具有将由一个意图过滤器在清单中激活的的onReceive()方法的一个BroadcastReceiver:

In one verifiable case the above problem, as well as other things such as buttons which stop responding, occurred after a device rebooted. I fixed that by creating a BroadcastReceiver which has an onReceive() method that will be activated by an intent filter in the manifest:

<intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

在换句话说,广播接收器将重新启动,它的伟大工程后运行。

In other words the BroadcastReceiver will run after a reboot, which works great.

不过该设备有时仍然会重置preferences,我注意到一个晚上之后,这种情况发生左右已经处于待命状态。我怀疑这是因为设备(一段时间后,可能是由于待命准备),可能会重新启动appwidget,然后将再次使用默认的preferences。我试图在清单中加入以下为广播接收器的一部分来解决它:

However the device still sometimes resets the preferences and I noticed this happens after a night or so of having been on standby. I suspect this is because the device (after some time, maybe due to going on standby) may restart the appwidget, which then will use the default preferences again. I tried to solve it by adding the following in the manifest as part of the BroadcastReceiver:

<intent-filter>
  <action android:name="android.intent.action.PACKAGE_RESTARTED"/>
</intent-filter>

全部分读取:

<receiver android:name=".BroadcastReceiverName">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_RESTARTED"/>
  </intent-filter>
</receiver>

不过,这似乎并没有工作。

But this doesn't seem to work.

我想知道什么可能导致这个问题,我怎么能检测到它的应用程序,并采取相应的行动,即启动广播接收器并重新加载$ ​​P $ pferences。

I would like to know what may cause this problem and how can I detect it in the app and act accordingly, i.e. start the BroadcastReceiver and reload the preferences.

为了完整起见,这里是(工作)的BroadcastReceiver code的一部分,我从<一个想法href=\"http://stackoverflow.com/questions/7862882/service-does-not-restart-after-clear-memory-appwidget-crashes\">Service后&QUOT不会重新启动;清除内存&QUOT; + appWidget崩溃

For completeness sake here is part of the (working) BroadcastReceiver code, I got the idea from Service does not restart after "Clear Memory" + appWidget crashes

public class BroadcastReceiverName extends BroadcastReceiver
{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    /* stuff done here to reread shared preferences and refresh button's pending intents etc. */

    SharedPreferences config=context.getSharedPreferences(ExampleWidgetConfig.PREFS_NAME, 0);
    int poll=config.getInt(ExampleWidgetConfig.PREFS_UPDATE_RATE, ExampleWidgetProvider.poll);
    int alert=config.getInt(ExampleWidgetConfig.PREFS_ALERT, ExampleWidgetProvider.alert);
    int backg=config.getInt(ExampleWidgetConfig.PREFS_BACKG, ExampleWidgetProvider.backg);

    /* change hardcoded default preferences in case they differ from saved ones */
    ExampleWidgetProvider.poll=poll;
    ExampleWidgetProvider.alert=alert;
    ExampleWidgetProvider.backg=backg;

    /* ... */
  }
}

code这节省了preferences。请注意,这code实际工作,问题不在于preferences不能保存或读取正确的,但在应用程序,这被改变用户的价值难codeD值,是被更改回来(见上面的例子)。

Code which saves the preferences. Please note that this code actually works and the problem is not that the preferences aren't saved or read correctly, but that hardcoded values in the app, which were changed to the user's values, are being changed back again (see above example).

/* change hardcoded values to user's settings */
ExampleWidgetProvider.poll=getpoll();
ExampleWidgetProvider.alert=getalert();
ExampleWidgetProvider.backg=getbackg();

/* store user settings we will continue to use these in the app */
SharedPreferences.Editor configEditor=config.edit();
configEditor.putInt(PREFS_UPDATE_RATE, ExampleWidgetProvider.poll);
configEditor.putInt(PREFS_ALERT, ExampleWidgetProvider.alert);
configEditor.putInt(PREFS_BACKG, ExampleWidgetProvider.backg);
configEditor.commit();

所以,我需要知道如何找出当这些硬codeD值重置为默认。我可以解决它通过读取preferences XML每隔一小时左右,但是这听起来像一个愚蠢的方案。

So I need to know how to find out when these hardcoded values reset to its default. I could work around it by reading the preferences xml every hour or so but that sounds like a stupid solution.

推荐答案

随着文档状态:

参数

键来检索preference的名称。
  利用DefValue价值回归,如果这preference不存在。

key The name of the preference to retrieve. defValue Value to return if this preference does not exist.

如果它存在,或者利用DefValue返回preference值。
  抛出ClassCastException - 如果有这个名字是不是一个preference
  一个int。

Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.

如果文件是可以相信的那么preference不存在可言,你可以通过这个测试你的调用getInt 的值不同,以你的价值观你的XML,如果它返回你的调用getInt 值你知道preferences不存在。

If the documentation is to be believed then the preference doesn't exist at all, you can test this by making your getInt values different to your values in your xml, if it is returning your getInt values you know the preferences don't exist.

在您的preferences必须被删除或摆在首位没有正确设置上述情况下,这似乎不太可能,但。

In the above case your preferences must be being removed or weren't set up properly in the first place, this seems quite unlikely though.

你可以看看是另一回事,你叫提交当你更新你的preferences或者您的preferences被更新。

Another thing you could look at is are you calling commit when you update your preferences or are your preferences being updated.

您也可以尝试与模式播放时您设置preference,尝试 MODE_MULTI_PROCESS 作为撒施接收器是一个独立的进程和presumably你在其他地方设置preferences也尝试'MODE_WORLD_WRITEABLE。

You could also try playing with the modes when you set up your preference, try MODE_MULTI_PROCESS as the broad cast receiver is a separate process and presumably you are setting the preferences elsewhere and also try 'MODE_WORLD_WRITEABLE'.

从评论的更新

首先,code是有点混乱,我猜,因为我不能看到这一切,但你不能在这里复制整个项目,所以我将不得不忍受这一点。

Firstly, the code is a little confusing I guess because I can't see it all but you can't copy the whole project here so I will have to live with that.

这是我的理解这个问题:你在你的preference设置这些值,并且可以调用调用getInt 上在一个点上,这些preferences和它是正确的,那么你调用getInt 再次调用某个事件(可能是在待机去触发)和preference后回到默认为在$规定p $ pference.xml,而不是打电话时传递的默认值调用getInt (因为这会提示preference不存在)。

This is how I understand the problem: You are setting these values in your preference and you can call getInt on these preferences at one point and it's correct, then after a certain event (perhaps triggered by going on stand by) you call getInt again and the preference is back at the default as specified in the preference.xml and not the default value passed when calling getInt (as this would suggest the preference doesn't exist).

我注意到您要复制的值到ExampleWidgetProvider。*变量,你可以肯定,这些都不是您检索为preferences和任何在preferences存在的价值永远不会正确地设置正确的值。我不明白这一点作为你需要的时候你改变任何一个同步他们有这些变量。如果他们是工作的一部分,各地那么他们应该被删除,使他们不卷积原来的问题。

I notice you are copying values to these ExampleWidgetProvider.* variables, are you certain that these aren't the correct values you are retrieving as preferences and whatever values exist in the preferences are never set correctly. I fail to see the point in having these variables as you are required to synchronize them when you change either one. If they are part of a work around then they should be removed so they don't convolute the original problem.

我确信你知道;如果重新启动应用程序的preferences应该是一样的重启为preference值之前被持久(presuming你已经正确配置这些),并因为它说的文件中:

As I am sure you know; if you restart the app the preferences should be the same as before the restart as the preference values are persisted (presuming you have them configured correctly) and as it says in the documentation:

的android:设置defaultValue 作为preference的默认值,这将
  设置或者如果持久关闭或持久性上和
  preference未在持久性存储中。

android:defaultValue The default value for the preference, which will be set either if persistence is off or persistence is on and the preference is not found in the persistent storage.

这表明preference没有得到重置为初始值,而是它不什么都原因存在(如果它是其中反复出现这种特殊的默认值)。

Which suggests that the preference is not getting reset to the original value but rather it doesn't exist for what ever reason (if it's this particular default value which keeps reappearing).

有存在的方法 setDefaultValues​​(上下文的背景下,INT渣油,布尔readAgain = TRUE)但你说你是不是调用一个。因此,似乎不太可能preference越来越复位而是被删除或从未在第一时间存在。

There exists the method setDefaultValues(Context context, int resId, boolean readAgain=true) but you said you aren't calling that. So it seems unlikely that the preference is getting reset but rather it is removed or never existed in the first place.

这篇关于Android的appwidget有时会改变preferences回默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆