【问题标题】:XSharedPreferences always get default value in my xposed moduleXSharedPreferences 总是在我的 xposed 模块中获得默认值
【发布时间】:2019-10-27 19:04:58
【问题描述】:

我尝试了许多不同的方法,但仍然无法正常工作。 始终获取默认值。

    public class HookTest implements IXposedHookLoadPackage  {
        private XSharedPreferences sharedPreferences;
        private final static String modulePackageName = HookTest.class.getPackage().getName();

        public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
            this.sharedPreferences = new XSharedPreferences(modulePackageName, "Values");
            sharedPreferences.makeWorldReadable();
            sharedPreferences.reload();
            XposedBridge.log("Xposed_test value: " +sharedPreferences.getBoolean("isRunning", false));
        }
}

我在 MainActivity 中试过它工作正常

返回正确的值

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=(Button)findViewById(R.id.button);
    editText=(EditText)findViewById(R.id.editText);
    final SharedPreferences pref = this.getSharedPreferences("Values", Context.MODE_PRIVATE);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            editText.setText(""+pref.getBoolean("isRunning", false));
            if(pref.getBoolean("isRunning", false)==true) {
                setVlaue(MainActivity.this, false);
            }else {
                setVlaue(MainActivity.this, true);
            }
        }
    });
}
public void setVlaue(Context context,boolean isRunning) {
    Intent intent = new Intent("my.action.MyReceiver");
    intent.putExtra("isRunning", isRunning);
    context.sendBroadcast(intent);
}

【问题讨论】:

  • handleLoadPackage 通常在应用程序启动之前调用,因此当您阅读isRunning 时,该应用程序尚未启动。根据应用程序写入此值的时间,只读取false 是合理的。

标签: java android sharedpreferences xposed


【解决方案1】:

在大多数情况下,为文件设置正确的权限 (chmod +r shared_prefs shared_prefs/your_prefrences.xml) 会有所帮助。但这不是什么好技巧。

好的解决方案是RemotePreferences,它使用 ContentProvider 来访问您的首选项

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多