当我尝试用下面这句话去修改一个dev_attr的值:
String strDeviceInfo = Utils.writeInfo("/sys/devices/system/cpu/cpu1/online","1");
public static String writeInfo(String filePath,String value) { String totalStr = ""; String str; try { Log.d("matt-","count +1"); if(!new File(filePath).exists()){ Log.d("matt-","count +++++++++++++++++++++++"); return ""; } Log.d("matt-","count +2"); FileWriter fr = new FileWriter(filePath); fr.write(value); Log.d("matt-","count +3"); fr.close(); return totalStr.trim(); } catch (IOException e) { // Log.d("AsusTestTool","error:"+e.getMessage()); e.printStackTrace(); return ""; } }
的时候,发生了如下错误
01-26 21:25:14.236 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: java.io.FileNotFoundException: /sys/devices/system/cpu/cpu1/online: open failed: EACCES (Permission denied)
01-26 21:25:14.237 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
01-26 21:25:14.238 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-26 21:25:14.238 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
01-26 21:25:14.238 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at java.io.FileWriter.<init>(FileWriter.java:80)
01-26 21:25:14.239 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at com.example.matthew.asustoolforcpuandgpuv10.asus.testtool.Utils.writeInfo(Utils.java:144)
01-26 21:25:14.240 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at com.example.matthew.asustoolforcpuandgpuv10.asus.testtool.cputestandset$3.onClick(cputestandset.java:112)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.view.View.performClick(View.java:5205)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.widget.CompoundButton.performClick(CompoundButton.java:122)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.view.View$PerformClick.run(View.java:21164)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.os.Looper.loop(Looper.java:148)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-26 21:25:14.245 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-26 21:25:14.247 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-26 21:25:14.247 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at libcore.io.Posix.open(Native Method)
01-26 21:25:14.247 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-26 21:25:14.247 5173-5173/com.example.matthew.asustoolforcpuandgpuv10 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
非常明显是没有足够的权限,那么我现在要修改的这个dev_attr的权限是多少呢
root@Z016:/sys/devices/system/cpu/cpu1 # ll online
-rw-r--r-- root root 4096 1970-01-25 22:14 online
root@Z016:/sys/devices/system/cpu/cpu1 # exit
matthew@matthew:~/AndroidStudioProjects/AsustoolforCPUandGPUv1.0$ adb root
adbd is already running as root
matthew@matthew:~/AndroidStudioProjects/AsustoolforCPUandGPUv1.0$ adb remount
remount succeeded
matthew@matthew:~/AndroidStudioProjects/AsustoolforCPUandGPUv1.0$ adb shell
root@Z016:/ # cd /sys/devices/system/cpu/cpu1# chmod 777 online
root@Z016:/sys/devices/system/cpu/cpu1 # ll online
-rwxrwxrwx root root 4096 1970-01-25 22:14 online
我用root把权限改了之后就执行成功了,但是这样也说明app是无法直接操作dev_attr的,还是要手动修改dev_attr的权限才能操作