Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android应用层制作LED指示灯

$
0
0

在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转化,就能读出灯的状态,直接运行就可以实现该功能.

作者:Google_huchun 发表于2017/6/25 22:04:42 原文链接
阅读:603 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>