读取菜单键home键返回键的背光节点,控制其闪烁

来源:互联网 发布:淘宝违规处罚 编辑:程序博客网 时间:2024/06/07 00:37

读取菜单键home键返回键的背光节点,控制其闪烁

    ----线程应用、节点文件读写

package cn.ckt.factorymode;import java.io.File;import java.io.FileWriter;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;/* * 读取菜单键home键返回键的背光节点,控制其闪烁 * */public class ButtonBackLightTest extends Activity implements OnClickListener{public static final String KEY="ButtonBackLightTest";public static final String BUTTON_LED_FILE = "/sys/class/leds/button-backlight/brightness" ;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.factory_auto_button_backlight);writeValueToFile(255);buttonLightTest();}private void buttonLightTest(){//另起线程延迟300ms开始,往节点写入0(灭)、255(亮)、休眠300ms 并循环5次new Handler().postDelayed(new Runnable(){//==new Handler(getMainLooper)还是运行在主线程中,循环不结束,按键要等循环结束才能响应public void run() {int loop = 0;while(loop <= 5){writeValueToFile(0);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}writeValueToFile(255);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}loop++;}}},300);/*new Thread(new Runnable(){//另起线程,该线程执行不影响主线程,主线程的按键可以随时响应public void run(){int loop = 0;while(loop <= 5){writeValueToFile(0);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}writeValueToFile(255);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}loop++;}}}).start();*/}    private void writeValueToFile(int brightness) {    try{//IO流控制:往节点中写入相应的值    File buttonBacklightFile = new File("/sys/class/leds/button-backlight/brightness");    FileWriter fr = new FileWriter(buttonBacklightFile);    String state = String.valueOf(brightness);    fr.write(state);     fr.close();    }catch(IOException e){    e.printStackTrace();    }finally{    }    }@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub}}


0 0