Detecting SharedPreference Changes From Another Instance

I am making a very simple application that can just read and update it. SharedPreferences:

public class MyPreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{
    @Override
    public void onResume() {
        super.onResume();
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener( this );
    }

    @Override
    public void onPause() {
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener( this );
        super.onPause();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Log.d( "xcc", "onSharedPreferenceChanged" );
        reloadOpenGLES();
    }

    public void saveSomeData()
    {
        SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit();
        editor.putString("MyKey", "MyValue" );
        editor.commit();
    }
}

      

Everything works fine and onSharedPreferenceChanged is triggered when I call the saveSomeData () method, but the problem is that - if I open multiple instances of the application, "onSharedPreferenceChanged" is only triggered in the active application, not the one that is running in the background. How can I detect a change from another instance?

Why I want to do this:

I am creating an LWP that uses GLSurfaceView. After the user has changed the settings, OpenGL needs to know to update the scene because the settings have changed, so I call reloadOpenGLES () when the onSharedPreferenceChanged triggers fire.

The weird thing is that if I have LWP as active LWP and then go to wallpaper settings and select it again, change its settings and "SET AS WALLPAPER", the first instance of LWP (the one that was like my LWP phone) does not update settings, which means "onSharedPreferenceChanged" triggers only on the currently active instance instead of running on all instances of the same application. As a result, the user can change some settings of the already existing LWP and after updating the settings nothing happens and he still gets the same old settings, unless he switches to different wallpapers and then returns to that.

In other words, when android changes the LWP, if the previously installed LWP is the same app as the newly installed LWP, it doesn't seem to restart the LWP app, but it probably fires some event instead. Although this event is "onSharedPreferenceChanged", it looks like I'm going in the wrong direction.

Maybe there is a better approach to this problem? Thank!

EDIT:

I have updated my manifest as suggested by adelphus :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.soft.test" android:sharedUserId="com.soft">

    <uses-feature
        android:name="com.soft.test"
        android:required="true" >
    </uses-feature>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

    <application
        android:label="@string/app_name">

        <service android:name=".LWPService" android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper" android:resource="@xml/livewallpaper" />

        </service>

       <activity
            android:name=".MyPreferencesActivity"
            android:exported="true"
            android:launchMode="singleInstance"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.WallpaperSettings" >
        </activity>

    </application>
</manifest>

      

but the problem still persists. Any hints?

Workaround : I was able to reload my settings on the following event:

public abstract class GLES2WallpaperService extends GLWallpaperService 
{
        @Override
        public void onVisibilityChanged(boolean visible) 
        {
           reloadOpenGLES();
        }
}

      

I would still like to figure out how to do this via the onSharedPreferenceChange event from another instance.

+3


source to share





All Articles