2011-12-10 42 views
-1

請問有人可以幫我嗎?我有這樣的代碼:如何將字符串設置爲共享首選項?

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");複製文本(字符串)從所謂的「布拉布拉」系統文件。然後我需要把這個文本(字符串)放到我的SharedPreferences中。我怎樣才能實現它?

回答

0

如果這是你的代碼,你應該知道。如果你不這樣做,你可以follow the Tutorial

就你而言,你會想要使用putString()-method


得到什麼cat打印出來,你需要你的Process -object的輸出。使用getInputStream()-method

+0

我知道如何將String放入SharedPreferences,但在這種情況下,它更復雜。問題在於貓命令。你能舉一些例子嗎? – Adam

+0

好吧,我明白了,非常感謝。 – Adam