详解Android应用层制作LED指示灯
在Java应用层修改LED指示灯的颜色,这个花了我半天时间, 才实现该功能!
public class LEDActivity extends Activity implements View.OnClickListener {
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) {
super.onCreate(paramBundle);
setContentView(R.layout.led_test);
mLedTest = ((Button)findViewById(R.id.ledcolor));
mLedTest.setOnClickListener(this);
// setledlightcolor(color);
// mLedTest.setTextColor(mLedColorRed);
// mLedTest.setText("RED");
// mLedStatus = 1;
}
public void onClick(View v) {
Log.d("LEDActivity", "---onClick---mLedStatus=" + mLedStatus);
switch (v.getId()) {
case R.id.ledcolor:
if(mLedStatus == 0) {
mLedTest.setText("开");
mLedStatus = 1
}else if(mLedStatus == 1) {
mLedTest.setText("关");
mLedStatus = 0;
}
break;
}
}
@Override
public void finish() {
setledlightcolor(-1);
super.finish();
}
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);
Log.d(TAG, "foRed" + foRed);
fRed.write(red ? LIGHT_ON : LIGHT_OFF);
fRed.close();
} catch (Exception e) {
loge(e);
}
}
void logd(Object d) {
Log.d(TAG, "" + d);
}
void loge(Object e) {
Log.e(TAG, "" + e);
}
}
直接在xml中写一个Button,用来实现点击按钮,其实很简单,只是把源码的led指示灯的路径拿出来,用过输出流,将String转化,就能读出灯的状态,直接运行就可以实现该功能.
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!