Android共享首选项更改侦听器即使在全局引用侦听器时也无法正常工作

时间:2015-03-31 07:45:16

标签: android service sharedpreferences

我有一个注册共享首选项更改侦听器的服务。共享首选项属于另一个应用程序,并且是世界可读的。尽管按照建议here保留了全局侦听器实例,但我的 onSharedPreferenceChanged() 未被调用。 服务代码如下

public class ClientService extends Service {
public static FileObserver observer;  
public static final String addr = "127.0.0.1";
public static final int port = 5001;
public Socket socket = null;
InputStream inputStream;
OutputStream outputStream;
String buffer = new String();
OnSharedPreferenceChangeListener listener;


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Log.d(MainActivity.TAG, "killing service");
    super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.d(MainActivity.TAG, "Starting Activity");
    Context context = null;
    /*try {
        socket = new Socket(addr, port);
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.e(MainActivity.TAG, "Error in opening Socket");
    }*/
    Log.d(MainActivity.TAG,"Carrying on...");

    final MyClientTask clientTask = new MyClientTask(addr, port);
    SharedPreferences sp = null;
    try {
        context = getApplicationContext().createPackageContext("net.osmand.plus", Context.MODE_WORLD_WRITEABLE);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        Log.e(MainActivity.TAG,"OSMAnd Package not found");
    }

    if(context != null) {
        sp = context.getSharedPreferences("net.osmand.settings", Context.MODE_WORLD_READABLE);
        Log.d(MainActivity.TAG, ""+sp.getFloat("last_known_map_lat", 0));

        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {

            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                    String key) {
                // TODO Auto-generated method stub
                Log.d(MainActivity.TAG,"Shared preference "+key+" changed");
                if(key == "last_known_map_lat" || key == "last_known_map_lon") {
                    /* Send this via socket */
                    float data = sharedPreferences.getFloat(key, 0);

                    Log.d(MainActivity.TAG, "Sending data: "+data);
                    clientTask.execute(""+data);

                }
            }
        };
        sp.registerOnSharedPreferenceChangeListener(listener);
    }


    observer = new FileObserver("/data/data/net.osmand.plus/shared_prefs/net.osmand.settings.xml" ) {

        @Override
        public void onEvent(int event, String path) {
            // TODO Auto-generated method stub
            if(event  == FileObserver.MODIFY) {
                Log.d(MainActivity.TAG, "Changed");
            }
        }
    };

    return super.onStartCommand(intent, flags, startId);
}



public class MyClientTask extends AsyncTask<String, Void, Void> {

    String dstAddress;
    int dstPort;
    String response = "";

    MyClientTask(String addr, int port){
        dstAddress = addr;
        dstPort = port;
    }

    @Override
    protected Void doInBackground(String... buffer) {
        try {
            byte[] bytes = buffer[0].getBytes();
            //outputStream.write(bytes);
            Log.d(MainActivity.TAG, "blah");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
    }

}

}

共享偏好设施相关代码位于 onStartCommand()中。 谁有任何线索有什么不对? 请注意,在实际的Sharedpreferences文件中,当我看到使用adb shell

时,我可以观察到值的变化

2 个答案:

答案 0 :(得分:1)

context = getApplicationContext().createPackageContext("net.osmand.plus", Context.MODE_WORLD_WRITEABLE);

您的上下文为空 你的骗局只是使用“这个”服务本身就是一个背景 或者您可以尝试getSharedPreferences("net.osmand.settings", Context.MODE_WORLD_READABLE);

答案 1 :(得分:0)

this post来看,我想要做的事情在Android中不受支持。 WORLD_READABLE的{​​{1}}标志不支持可能导致不一致的多线程支持。在Android开发者网站中,还提到SharedPreferences首选项不支持SharedPreferences的多个流程支持。