【问题标题】:Acess a sharedpreference file from different app [duplicate]从不同的应用程序访问共享首选项文件[重复]
【发布时间】:2014-02-20 08:57:59
【问题描述】:

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

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

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

谢谢

【问题讨论】:

标签: java android preferences


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2012-09-11
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多