Set brightness of flash in Android

2019-08-28 22:22发布

问题:

In my program I tested this:

try {               
    FileWriter mWriter = new FileWriter("/sys/class/leds/flashlight/brightness");
    mWriter.write(125);
    mWriter.flush();
} catch (Exception e) {
    Log.e(TAG, "setFlashlightEnabled failed", e);
}

I have this in the log :

01-13 14:56:30.578: E/test(603): setFlashlightEnabled failed
01-13 14:56:30.578: E/test(603): java.io.FileNotFoundException: /sys/class/leds/flashlight/brightness (Permission denied)
01-13 14:56:30.578: E/test(603):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-13 14:56:30.578: E/test(603):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
01-13 14:56:30.578: E/test(603):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
01-13 14:56:30.578: E/test(603):    at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
01-13 14:56:30.578: E/test(603):    at java.io.FileWriter.<init>(FileWriter.java:80)
01-13 14:56:30.578: E/test(603):    at sad.sdsa.sdjahskdhj.TestpuissanceActivity.onClick(TestpuissanceActivity.java:87)
01-13 14:56:30.578: E/test(603):    at android.view.View.performClick(View.java)
01-13 14:56:30.578: E/test(603):    at android.view.View$PerformClick.run(View.java)
01-13 14:56:30.578: E/test(603):    at android.os.Handler.handleCallback(Handler.java)
01-13 14:56:30.578: E/test(603):    at android.os.Handler.dispatchMessage(Handler.java)
01-13 14:56:30.578: E/test(603):    at android.os.Looper.loop(Looper.java)
01-13 14:56:30.578: E/test(603):    at android.app.ActivityThread.main(ActivityThread.java)
01-13 14:56:30.578: E/test(603):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 14:56:30.578: E/test(603):    at java.lang.reflect.Method.invoke(Method.java:507)
01-13 14:56:30.578: E/test(603):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
01-13 14:56:30.578: E/test(603):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
01-13 14:56:30.578: E/test(603):    at dalvik.system.NativeStart.main(Native Method)

I have tested in ADB Shell this :

echo 125 > /sys/class/leds/flashlight/brightness

It works in root but not in normal mode.

What is the solution to change the flash output without being root?

回答1:

It belongs to system and is being protected by system, so you must be a root to access.Also all Android devices dont have the same flashlight design in structure, sample like: in HTC device, it supports API for handling 3 different brightness level, but others don't...