Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] Still don't know how to reload prefs in realtime

OP neverweep

12th August 2014, 07:41 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
7 posts
Join Date:Joined: Apr 2014
More
I'm a newbee to xposed module development. I searched sometimes in this forum and viewed some open-source mudules on github, but I still don't know how to reload prefs in my module.

I try to change the setting repeatedly, but logcat always displays that prefs returned a false. My code is here below:

This is the main class loaded by xposed:
Code:
public class Main implements IXposedHookLoadPackage, IXposedHookZygoteInit, IXposedHookInitPackageResources{
    private static XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName());

    ....
    SOME HACKS
    ....

    private BroadcastReceiver xReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            context = mContext;
            if (intent.getAction().equals("xxx.xxx.SETTING_CHANGED")){
                 prefs.makeWorldReadable(); // Wether this line is added or not, the result is same.
                 prefs.reload();
                 Log.d(TAG, String.valueOf(prefs.getBoolean("key", false)));
            }
        }
    };
}
This is the setting screen class:
Code:
public class Setting extends PreferenceActivity implements OnSharedPreferenceChangeListener{

    ListPreference lp;
    ListPreference _lp;
    EditTextPreference etp;
    CheckBoxPreference cbp;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting);
        getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Intent intent = new Intent();
        intent.setAction("xxx.xxx.SETTING_CHANGED");
        Setting.this,sendBroadcast(intent);
    }
Last edited by neverweep; 12th August 2014 at 07:51 AM. Reason: some spelling faults
12th August 2014, 04:55 PM   |  #2  
GermainZ's Avatar
Forum Moderator / Recognized Developer / XDA Portal Team
Thanks Meter: 7,359
 
5,917 posts
Join Date:Joined: Aug 2012
More
From memory, I believe the XSharedPreferences constructor takes the preferences' file name, which is usually something like "com.mypackage_preferences" and not only "com.mypackage".

Note that you don't need to use makeWorldReadable in your module since you're doing that in your preferences activity.
13th August 2014, 03:58 AM   |  #3  
OP Junior Member
Thanks Meter: 0
 
7 posts
Join Date:Joined: Apr 2014
More
Thumbs up
Quote:
Originally Posted by GermainZ

From memory, I believe the XSharedPreferences constructor takes the preferences' file name, which is usually something like "com.mypackage_preferences" and not only "com.mypackage".

Note that you don't need to use makeWorldReadable in your module since you're doing that in your preferences activity.

Thanks for your reply. But I can't understand clearly and I'm even more confused.

Code:
XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName());
This works well. Read preferences correctly.

In the source code of XSharedPreference class. The function "XSharedPreferences(String packageName)" will call "XSharedPreferences(String packageName, String prefFileName)" and complete the default preferences file name. I don't think I need to use full path and file name?

I still don't know how to reload settings correctly. Could you give me a copy of detailed code to work perfectly?

Awating your reply.
13th August 2014, 04:53 AM   |  #4  
GermainZ's Avatar
Forum Moderator / Recognized Developer / XDA Portal Team
Thanks Meter: 7,359
 
5,917 posts
Join Date:Joined: Aug 2012
More
Quote:
Originally Posted by neverweep

Thanks for your reply. But I can't understand clearly and I'm even more confused.

Code:
XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName());
This works well. Read preferences correctly.

In the source code of XSharedPreference class. The function "XSharedPreferences(String packageName)" will call "XSharedPreferences(String packageName, String prefFileName)" and complete the default preferences file name. I don't think I need to use full path and file name?

I still don't know how to reload settings correctly. Could you give me a copy of detailed code to work perfectly?

Awating your reply.

If I'm not mistaken, this:
Code:
XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName());
will load an "empty" preferences file (because it won't exist) so everything will have the default value. Try something like this instead:
Code:
XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName() + "_preferences");
As for reloading preferences, you seem to be doing it correctly (XSharedPreferences.reload method), it's just that it's not working as intended for the reason explained above.
13th August 2014, 04:40 PM   |  #5  
Senior Member
Thanks Meter: 614
 
536 posts
Join Date:Joined: Jan 2013
More
No, neverweep's code:
Code:
XSharedPreferences prefs = new XSharedPreferences(Main.class.getPackage().getName());
is good. I also use such code in my modules and everything works.

Rovo89 has code to add "_preferences" automatically (link: https://github.com/rovo89/XposedBrid...ences.java#L37)
The Following User Says Thank You to pyler For This Useful Post: [ View ]
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes