短按SOS键实现开始与停止白光LED灯闪烁状态

 int mInt=0;
Timer mTimer;
TimerTask mTimerTask;
/*
* 开始白光LED灯报警灯闪烁状态
*/
private void SOS() {
  mTimerTask = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
             mInt = mInt == 0 ? 1 : 0;
            message.what = mInt;
            handler_white.sendMessage(message);
        }
    };
     mTimer = new Timer();
    mTimer.schedule(mTimerTask, 0, 500);

}
/*
 * 运行中白光LED灯报警灯闪烁状态
 */
Handler handler_white = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
               //开灯状态
                HardwareControler.SetWhiteLightState(1);
                break;
            case 0:
             //关灯状态
                HardwareControler.SetWhiteLightState(0);
                break;
        }
    }
};
/*
 * 停止白光LED灯报警灯闪烁状态
 */
private void stopTimer(){
    if (mTimer != null) {
        mTimer.cancel();
        mTimer = null;
    }
    if (mTimerTask != null) {
        mTimerTask.cancel();
        mTimerTask = null;
    }
}

你可能感兴趣的:(短按SOS键实现开始与停止白光LED灯闪烁状态)