如何将字符串放入共享首选项?

时间:2011-12-10 14:39:02

标签: java android string sharedpreferences

请问有人帮帮我吗?我有这段代码:

Process a;
try {
    a = Runtime.getRuntime().exec("su"); 

    DataOutputStream swp = new DataOutputStream(a.getOutputStream());
    swp.writeBytes("cat /proc/sys/vm/blabla\n");
    swp.writeBytes("exit\n"); 
    swp.flush();
    try {
        a.waitFor();
        if (a.exitValue() != 255) {
            // TODO Code to run on success
            toastMessage("root");
            }
        else {
            // TODO Code to run on unsuccessful             
           toastMessage("not root");
           }
    } catch (InterruptedException e) {
        // TODO Code to run in interrupted exception    
        toastMessage("not root");
        }
} catch (IOException e) {
    // TODO Code to run in input/output exception
    toastMessage("not root");
}

此代码swp.writeBytes("cat /proc/sys/vm/blabla\n");从名为“blabla”的系统文件中复制文本(字符串)。然后我需要将此文本(String)放到我的SharedPreferences中。我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

如果这是您的代码,您应该知道。如果不这样做,您可以follow the Tutorial

在您的情况下,您需要使用putString()-method


要获得打印出来的cat,您需要输出Process - 对象。使用getInputStream()-method