Question

I am triying to acess to a shared preferences file wich was created in a different applicaction. I followed some tutorials but not works. This is my case:

App1 (com.example.remoteservice)

SharedPreferences configuracion2;
                configuracion2 = getSharedPreferences("telo", Context.MODE_WORLD_READABLE);

                Editor editor = configuracion2.edit();
                editor.putFloat("x21", Float.parseFloat(x21.getText().toString()));
                editor.commit();

App2 (com.example.grafica)

    Context con = createPackageContext("com.example.remoteservice", MODE_WORLD_WRITEABLE);
            SharedPreferences pref = con.getSharedPreferences(
                    "telo",MODE_WORLD_READABLE);

            ancho = pref.getFloat("x21", 0);

Log.i("smash", "ancho" + String.valueOf(ancho));

And returns 0 because not exists "telo". why??

thanks

Was it helpful?

Solution

App1 (com.example.remoteservice)

SharedPreferences configuracion2; configuracion2 = getSharedPreferences("telo", Context.MODE_WORLD_READABLE);

            Editor editor = configuracion2.edit();
            editor.putFloat("x21", Float.parseFloat(x21.getText().toString()));
            editor.commit();

App2 (com.example.grafica)

Context con = createPackageContext("com.example.remoteservice", Context.CONTEXT_IGNORE_SECURITY); 
        SharedPreferences pref = con.getSharedPreferences(
                "telo",MODE_WORLD_READABLE);

        ancho = pref.getFloat("x21", 0);

Log.i("smash", "ancho" + String.valueOf(ancho));

This should solve it. Also do not call the getSharedPreferences() of current context before this call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top