在 Android 中讀寫 /sys/ 目錄下的系統文件(如振動器振幅控制)需要 Root 權限,不能使用普通文件流操作,需通過 su Shell 命令執行。
// 錯誤嘗試:普通文件流無權限訪問 /sys/
FileOutputStream fos = new FileOutputStream("/sys/devices/virtual/timed_output/vibrator/amp");
fos.write(value.getBytes()); // 權限被拒絕
// 錯誤的 su 寫法
os.writeBytes(value + " > " + filename + "\n"); // 缺少 echo 命令
public static void writeSysFile(String filename, String value) {
Process process = null;
DataOutputStream os = null;
try {
// 1. 獲取 Root Shell
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
// 2. 正確命令:echo '值' > 文件路徑
// 注意:單引號包裹值,防止特殊字符;重定向符 > 需在 Shell 中執行
String command = "echo '" + value + "' > " + filename + "\n";
os.writeBytes(command);
// 3. 退出 Shell
os.writeBytes("exit\n");
os.flush();
// 4. 等待執行完成並檢查結果
int exitCode = process.waitFor();
if (exitCode != 0) {
// 讀取錯誤流排查
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line;
StringBuilder error = new StringBuilder();
while ((line = errorReader.readLine()) != null) {
error.append(line).append("\n");
}
Log.e("SysWrite", "寫入失敗 exitCode=" + exitCode + ", error=" + error);
}
} catch (IOException | InterruptedException e) {
Log.e("SysWrite", "異常", e);
} finally {
try { if (os != null) os.close(); } catch (IOException ignored) {}
if (process != null) process.destroy();
}
}
Runtime.exec("su") 啟動 Root Shell,後續命令在 Root 權限下執行public static String readSysFile(String filename) {
Process process = null;
BufferedReader reader = null;
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("cat " + filename + "\n");
os.writeBytes("exit\n");
os.flush();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
return reader.readLine(); // 讀取輸出
} catch (IOException e) {
Log.e("SysRead", "讀取失敗", e);
return null;
} finally {
try { if (reader != null) reader.close(); } catch (IOException ignored) {}
if (process != null) process.destroy();
}
}
/sys/ 節點即使 Root 也不可寫振動控制等功能優先使用官方 API:
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(500); // 舊版 API
}
避免直接操作 /sys/ 文件,兼容性差、風險高、易失效。