[Q] XSharedPreferences - never loaded

Search This thread

TheLexus

Senior Member
Jun 4, 2011
141
12
Hi guys,

I'm currently a little bit confused because i try to create my first XPosed module for Xposed 2.6.1 on a Android 4.0.3 unit but i have trouble with my preferences. I tried multiple things but it was never working.

Here is the preferences code of my Settings activity. That one works and it creates a the preference file with rw-rw-r permissions and the preferences are correctly saved.

Code:
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE | MODE_MULTI_PROCESS);
getPreferenceManager().setSharedPreferencesName(Const.PACKAGE + "_preferences");
addPreferencesFromResource(R.xml.preferences);

That one is the xposed side snippet:

Code:
preferences = new XSharedPreferences(Const.PACKAGE, Const.PACKAGE+"_preferences");
if (preferences.getAll().size()==0)
{
  debug("Preferences seems not to be initialized!");
}

It never loads any of my preferences. I tried the constructor XSharedPreferences(Const.PACKAGE) instead, but without any luck...

I checked the directory permissions of the settings directory, and that one seems to be correct (rwxrwxr-x).

Thanks for every help!

Bye
 
Last edited:

TheLexus

Senior Member
Jun 4, 2011
141
12
It is set to the hardcoded package Name. No class.getPackage... I added a file.isreadable and it returns false. I dont know, im just out oft any ideas. :(
 

theknut

Senior Member
May 4, 2009
990
4,148
www.theknut.de
It is set to the hardcoded package Name. No class.getPackage... I added a file.isreadable and it returns false. I dont know, im just out oft any ideas. :(
Is there any reason why you set MODE_MULTI_PROCESS? Can you please try removing it, delete your preference file and try again?
In my UI I open SharedPreferences from <packagename>_preferences.xml
getSharedPreferences(Common.PREFERENCES_NAME, Context.MODE_WORLD_READABLE)
and in the module I open the settings via
new XSharedPreferences(Common.PACKAGE_NAME);

Please remove your current preferences file and the MODE_MULTI_PROCESS and try the code from above.
 

caioketo

Senior Member
Mar 4, 2011
157
31
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:
SharedPreferences.Editor editor = LogExtras.getAppContext().getSharedPreferences(Commons.PACKAGE_NAME, Context.MODE_WORLD_READABLE).edit();
editor.putString("package_to_look", _packageToLook);

And when I try to get it from the module it always returns size 0
private static final XSharedPreferences preferences = new XSharedPreferences(Commons.PACKAGE_NAME, Commons.PACKAGE_NAME+"_preferences");

public static void refreshPreferences() {
Log.d("Utils", "reading prefs");
preferences.reload();
Log.d("Utils", Integer.toString(preferences.getAll().size()));
Commons.PACKAGE_TO_LOOK = preferences.getString("package_to_look", Commons.PACKAGE_TO_LOOK);
log("Package to Look: " + Commons.PACKAGE_TO_LOOK);
}

---------- Post added at 04:30 PM ---------- Previous post was at 03:34 PM ----------

I tested the prefs in the context, and it works, only in xposed it dont load.
So I got the app saving the pref and reading the pref working, but when I try to read the pref with XSharedPreference, the size is always 0.
Already tried everything, but nothing works.
 
  • Like
Reactions: Maxr1998

Maxr1998

Recognized Developer
Apr 15, 2013
2,161
3,221
25
Germany
maxr1998.de
Google Nexus 4
Nexus 7
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:
SharedPreferences.Editor editor = LogExtras.getAppContext().getSharedPreferences(Commons.PACKAGE_NAME, Context.MODE_WORLD_READABLE).edit();
editor.putString("package_to_look", _packageToLook);

And when I try to get it from the module it always returns size 0
private static final XSharedPreferences preferences = new XSharedPreferences(Commons.PACKAGE_NAME, Commons.PACKAGE_NAME+"_preferences");

public static void refreshPreferences() {
Log.d("Utils", "reading prefs");
preferences.reload();
Log.d("Utils", Integer.toString(preferences.getAll().size()));
Commons.PACKAGE_TO_LOOK = preferences.getString("package_to_look", Commons.PACKAGE_TO_LOOK);
log("Package to Look: " + Commons.PACKAGE_TO_LOOK);
}

---------- Post added at 04:30 PM ---------- Previous post was at 03:34 PM ----------

I tested the prefs in the context, and it works, only in xposed it dont load.
So I got the app saving the pref and reading the pref working, but when I try to read the pref with XSharedPreference, the size is always 0.
Already tried everything, but nothing works.

I have a similar problem, but only for one preference:
Code:
public class Main implements IXposedHookZygoteInit, IXposedHookLoadPackage {

    public static final String MY_PACKAGE_NAME = Main.class.getPackage().getName();
    private static XSharedPreferences pref, packagePref;

    @Override
    public void initZygote(StartupParam startupParam) throws Throwable {
        pref = new XSharedPreferences(MY_PACKAGE_NAME, Common.PREF);
        packagePref = new XSharedPreferences(MY_PACKAGE_NAME, Common.PREF_PACKAGE);
    }

    @Override
    public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
        pref.reload();
        packagePref.reload();
        final String packageName = lpparam.packageName;

        boolean masterSwitchOn = pref.getBoolean(Common.MASTER_SWITCH, true);
        XposedBridge.log("Master Switch " + (masterSwitchOn ? "on" : "off"));
        if (!masterSwitchOn) {
            return;
        }

        if (!packagePref.getBoolean(packageName, false)) {
            return;
        }

        Long timestamp = System.currentTimeMillis();
        Long permitTimestamp = packagePref.getLong(packageName + "_tmp", 0);
        if (permitTimestamp != 0 && timestamp - permitTimestamp <= 4000) {
            return;
        }
        .
        .
        .
}

So, the package boolean works, the master switch doesn't…

It only seems to work after opening and closing the app multiple times.
 
Last edited:

rovo89

Senior Recognized Developer
Jan 4, 2012
2,585
81,434
So you found any way to make it work??
I ran into the same problem, I set the preferences by the code:

Are you sure that your app saves the file to shared_prefs/<packagename>_preferences.xml? Better verify it using a file explorer. It's the default for preference activities, but it might not be for the methods you use.

I have a similar problem, but only for one preference:

Similar stuff here. Does your application really save the preferences in two different files, named shared_prefs/<Value of Common.PREF>.xml and shared_prefs/<Value of Common.PREF_PACKAGE>.xml? If yes, are both of these files world-readable?
 
  • Like
Reactions: Maxr1998

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    So you found any way to make it work??
    I ran into the same problem, I set the preferences by the code:
    SharedPreferences.Editor editor = LogExtras.getAppContext().getSharedPreferences(Commons.PACKAGE_NAME, Context.MODE_WORLD_READABLE).edit();
    editor.putString("package_to_look", _packageToLook);

    And when I try to get it from the module it always returns size 0
    private static final XSharedPreferences preferences = new XSharedPreferences(Commons.PACKAGE_NAME, Commons.PACKAGE_NAME+"_preferences");

    public static void refreshPreferences() {
    Log.d("Utils", "reading prefs");
    preferences.reload();
    Log.d("Utils", Integer.toString(preferences.getAll().size()));
    Commons.PACKAGE_TO_LOOK = preferences.getString("package_to_look", Commons.PACKAGE_TO_LOOK);
    log("Package to Look: " + Commons.PACKAGE_TO_LOOK);
    }

    ---------- Post added at 04:30 PM ---------- Previous post was at 03:34 PM ----------

    I tested the prefs in the context, and it works, only in xposed it dont load.
    So I got the app saving the pref and reading the pref working, but when I try to read the pref with XSharedPreference, the size is always 0.
    Already tried everything, but nothing works.
    1
    So you found any way to make it work??
    I ran into the same problem, I set the preferences by the code:

    Are you sure that your app saves the file to shared_prefs/<packagename>_preferences.xml? Better verify it using a file explorer. It's the default for preference activities, but it might not be for the methods you use.

    I have a similar problem, but only for one preference:

    Similar stuff here. Does your application really save the preferences in two different files, named shared_prefs/<Value of Common.PREF>.xml and shared_prefs/<Value of Common.PREF_PACKAGE>.xml? If yes, are both of these files world-readable?