即使在恢复出厂设置后,Android受保护的变量存储

时间:2019-07-03 16:37:59

标签: android

我正在研究Aosp。我想在系统中保留一个变量,但是当我使用共享首选项时,执行出厂重置时该变量将被删除。我需要更改并保存要保留的变量。像Mac地址一样思考

1 个答案:

答案 0 :(得分:0)

您可以将要保存的所有变量放入json并将其保存在内部存储中 而且您可以随时取回它

用于读写的示例代码

private void saveConfig(String data) {
try {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));
    outputStreamWriter.write(data);
    outputStreamWriter.close();
}
catch (IOException e) {
    Log.e("Exception", "File write failed: " + e.toString());
}   }

private String readConfig() {

String data = "";

try {
    InputStream inputStream = context.openFileInput("config.txt");

    if ( inputStream != null ) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String receiveString = "";
        StringBuilder stringBuilder = new StringBuilder();

        while ( (receiveString = bufferedReader.readLine()) != null ) {
            stringBuilder.append(receiveString);
        }

        inputStream.close();
        data = stringBuilder.toString();
    }
}
catch (FileNotFoundException e) {
    Log.e("Error", "File not found: " + e.toString());
} catch (IOException e) {
    Log.e("Error", "Can not read file: " + e.toString());
}
return data;
}

您的json可以

{
  "variable1": "Value",
  "variable2": false
}