3

I am a new Android developer and I've got my first app nearly complete.

One of the last things I have on my list of things to do is Tasker integration. I have the integration working up to a point.

My app is currently setup to work as a plugin and accept a string from tasker and update a preference with the new string. This is working.

The problem is when I relaunch my app, the value of the preference isn't updating. I can't get the preference summary to update until I completely close the app and relaunch it.

So the question is, how do I force my app to reload the preferences from within the app's onResume or is there a better way all together?

This is the code from the receiver that updates the preference value:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String album_name = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).commit();
Log.d(TAG, "fr: "+prefs.getString("text_default_album", ""));

This is the in the activity's onResume:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String album_name = prefs.getString("text_default_album", "");
Log.d(TAG, "album_name: "+album_name);

final Preference album = getPreferenceScreen().findPreference("text_default_album");
album.setSummary(album_name);

The manifest declaration for the receiver:

    <receiver
            android:name=".receivers.FireReceiver"
            android:exported="true"
            android:process=":background"
            tools:ignore="ExportedReceiver" >

        <!-- this Intent filter allows the plug-in to discovered by Locale -->
        <intent-filter>
            <action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
        </intent-filter>
    </receiver>

UPDATE: For anyone who may run into this question and be struggling to figure it out, once I removed the android:process=":background" in the AndroidManifest.xml declaration of the receiver. It now works as expected.

4

1 回答 1

0

提交将新值保存在存储中,但不会更新内存中的值。如果你想要这个功能,你应该使用 apply。在这里这里阅读更多

所以你必须更换

prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).commit();

prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).apply();

希望这可以帮助...

于 2013-07-15T00:35:33.290 回答