简体   繁体   English

如何获取最新的sharedPreferences

[英]How to get most recent sharedPreferences

I have a background service that reads cpu usage and frequency and displays it on notification bar 我有一个后台服务,可以读取cpu的使用情况和频率并将其显示在通知栏上

In application settings(Preferences) i have a option to chose to display only frequency only load or both 在应用程序设置(首选项)中,我可以选择仅显示仅频率负载,或同时显示两个负载

But method for getting shared preferences wont get most recent SharedPreference 但是获取共享首选项的方法不会获取最新的SharedPreference

it get SharedPreference only first time service starts and if i chose diferent option in Preference screen it wont update in service 它只有在第一次服务启动时才获得SharedPreference,如果我在“首选项”屏幕中选择了不同的选项,它将不会在服务中更新

Here is the code 这是代码

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    Runnable runnable = new Runnable() {@Override
        public void run() {
            while (thread) {

                sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
                items = sharedPrefs.getString("notif", "freq");
                System.out.println(items); //this keeps displaying the same value even if i go to Preference screen and change to something else
                if (items.equals("freq") || items.equals("both")) {

                }
                if (items.equals("load") || items.equals("both")) {

                } //reading frequency and load depending on what is selected
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mHandler.post(new Runnable() {@Override
                    public void run() {
                        if (thread) {
                            createNotification(); //create notification
                        }
                    }
                });
            }
        }
    };
    new Thread(runnable).start();

    return START_STICKY;
}

SOLVED 解决了

Because my service was running in separate process i had to add this flag when accesing shared preference 因为我的服务在单独的进程中运行,所以在访问共享首选项时必须添加此标志

private final static int PREFERENCES_MODE = Context.MODE_MULTI_PROCESS;

and change like this 像这样改变

sharedPrefs = this.getSharedPreferences("preference name", PREFERENCES_MODE);

Ensure you write your data to shared preferences correctly, specifically you commit() your changes, as docs say : 确保正确地将数据写入共享的首选项,特别是按照文档说commit()更改:

All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply() 您在编辑器中所做的所有更改都将被分批处理,并且直到您调用commit()或apply()时,才将其复制回到原始的SharedPreferences中。

Here is example code: 这是示例代码:

SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean( key, value );
editor.commit();

I think the error is on the line 我认为错误就在网上

sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

where you are passing 'this' from inside a thread? 您从线程内部传递“ this”在哪里? Can you change it with the application context? 您可以使用应用程序上下文进行更改吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM