Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,807,866 Members 51,070 Now Online
XDA Developers Android and Mobile Development Forum

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

Tip us?
 
neverweep
Old
(Last edited by neverweep; 12th August 2014 at 06:51 AM.) Reason: some spelling faults
#1  
Junior Member - OP
Thanks Meter 0
Posts: 7
Join Date: Apr 2014
Unhappy [Q] Still don't know how to reload prefs in realtime

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);
    }
 
GermainZ
Old
#2  
GermainZ's Avatar
Forum Moderator / Recognized Developer
Thanks Meter 6,380
Posts: 5,467
Join Date: Aug 2012
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.
 
neverweep
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 7
Join Date: Apr 2014
Quote:
Originally Posted by GermainZ View Post
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.
 
GermainZ
Old
#4  
GermainZ's Avatar
Forum Moderator / Recognized Developer
Thanks Meter 6,380
Posts: 5,467
Join Date: Aug 2012
Quote:
Originally Posted by neverweep View Post
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.
 
pyler
Old
#5  
Senior Member
Thanks Meter 440
Posts: 357
Join Date: Jan 2013
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: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes