代码之家  ›  专栏  ›  技术社区  ›  user3243651

从不同的应用程序访问共享引用文件[重复]

  •  0
  • user3243651  · 技术社区  · 11 年前

    我正在尝试访问在不同应用程序中创建的共享首选项文件。我遵循了一些教程,但没有成功。这是我的情况:

    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();
    

    附录2(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));
    

    并返回0,因为不存在“telo”。为什么?

    谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   Mobility    11 年前

    App1(com.example.remoteservice)

    SharedPreferences配置2; configuracion2=getSharedPreferences(“telo”,Context.MODE_WORLD_READABLE);

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

    附录2(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(“粉碎”,“anco”+String.valueOf(anco));

    这应该可以解决这个问题。在调用之前,也不要调用当前上下文的getSharedPreferences()。