SoFunction
Updated on 2025-04-10

Detailed explanation of the Android application layer production LED indicator light

Detailed explanation of the Android application layer production LED indicator light

It took me half a day to modify the color of the LED indicator in the Java application layer to realize this function!

public class LEDActivity extends Activity implements  {
private static final String TAG = "LED";
Button mLedTest;
int mLedStatus = 0;
private final int mLedColorRed = 0xFFFF0000;

final byte[] LIGHT_ON = { '2', '5', '5' };
final byte[] LIGHT_OFF = { '0' };
private final int RED = 0;
private int color = RED;
String RED_LED_DEV = " /sys/class/leds/torch/brightness";

public void onCreate(Bundle paramBundle) {
  (paramBundle);
  setContentView(.led_test);

  mLedTest = ((Button)findViewById());
  (this);

 //  setledlightcolor(color);
 //  (mLedColorRed);
 //  ("RED"); 
 //  mLedStatus = 1;
 }

 public void onClick(View v) {
 ("LEDActivity", "---onClick---mLedStatus=" + mLedStatus);
 switch (()) {
 case :
 if(mLedStatus == 0) {
 ("open");
 mLedStatus = 1
 }else if(mLedStatus == 1) {
   ("close");
   mLedStatus = 0;
 }
 break;
 }
 }

 @Override
public void finish() {
  setledlightcolor(-1);
  ();
}
private void setledlightcolor(int color) {
  logd("set:" + color);
  boolean red = false;
  switch (color) {
  case RED:
    red = true;
    break;
  default:
    break;
  }
  try {
    FileOutputStream foRed = new FileOutputStream(RED_LED_DEV);
    (TAG, "foRed" + foRed);
    (red ? LIGHT_ON : LIGHT_OFF);
    ();
  } catch (Exception e) {
    loge(e);
  }
}
void logd(Object d) {
  (TAG, "" + d);
}
void loge(Object e) {
  (TAG, "" + e);
}
}

It is actually very simple to write a Button in the XML to click the button. It is just taking out the path of the source code LED indicator, using the output stream, converting the String, and reading the status of the lamp. This function can be realized by running it directly.

Thank you for reading, I hope it can help you. Thank you for your support for this site!