Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid appwidget sometimes changes preferences back to default
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>&lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt; &lt;/intent-filter&gt; </code></pre> <p>In other words the BroadcastReceiver will run after a reboot, which works great.</p> <p>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:</p> <pre><code>&lt;intent-filter&gt; &lt;action android:name="android.intent.action.PACKAGE_RESTARTED"/&gt; &lt;/intent-filter&gt; </code></pre> <p>Full section reads:</p> <pre><code>&lt;receiver android:name=".BroadcastReceiverName"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt; &lt;/intent-filter&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.PACKAGE_RESTARTED"/&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>But this doesn't seem to work.</p> <p>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.</p> <p>For completeness sake here is part of the (working) BroadcastReceiver code, I got the idea from <a href="https://stackoverflow.com/questions/7862882/service-does-not-restart-after-clear-memory-appwidget-crashes">Service does not restart after &quot;Clear Memory&quot; + appWidget crashes</a></p> <pre><code>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></pre> <p>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).</p> <pre><code>/* 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(); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload