Android倒计时、延时操作与新建线程、获取当前时间

原创
2017/04/25 09:33
阅读数 450
/** * 倒计时45分钟 */
private int second = 0;//秒
private int minute = 45;//分
Handler handler = new Handler();
/**
 * 倒计时开始
 */
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        timeCountDown();
        handler.postDelayed(this, 1000);
    }
};
/**
     * 倒计时操作
     */
    private void timeCountDown(){
        if (minute == 0) {
            if (second == 0) {
                time_tv.setText("0" + minute + ":" + "0" + second);
                Message message = new Message();
                message.what = 1;
                handlerStop.sendMessage(message);
//                jumpActivity(null,ExamEndActivity.class);
//                finish();
            } else {
                second--;
                if (second >= 10) {
                    time_tv.setText("0" + minute + ":" + second);
                } else {
                    time_tv.setText("0" + minute + ":" + "0" + second);
                }
            }
        } else {
            if (second == 0) {
                second = 59;
                minute--;
                if (minute >= 10) {
                    time_tv.setText(minute + ":" + second);
                } else {
                    time_tv.setText("0" + minute + ":" + second);
                }
            } else {
                second--;
                if (second >= 10) {
                    if (minute >= 10) {
                        time_tv.setText(minute + ":" + second);
                    } else {
                        time_tv.setText("0" + minute + ":" + second);
                    }
                } else {
                    if (minute >= 10) {
                        time_tv.setText(minute + ":0" + second);
                    } else {
                        time_tv.setText("0" + minute + ":0" + second);
                    }
                }
            }
        }
    }
/**
   * 停止倒计时
   */
int  count = 0;
  final Handler handlerStop = new Handler() {
               public void handleMessage(Message msg) {
                       switch (msg.what) {
                           case 1:
                                   count = 0;
                                   handler.removeCallbacks(runnable);
                                   break;
                           }
                       super.handleMessage(msg);
                   }

                   };

 

/** * 延时操作一 */
mHandler .postDelayed(mRunnable, 1000);
/**
 * 延时操作一
 */
private Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        mHandler.sendEmptyMessage(1);
    }
};

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        //1s后执行代码
        
    }
};
/**
 * 延时操作二
 */
public void delayedShow(){
    new Handler().postDelayed(new Runnable(){
        public void run() {
            //1s后执行代码
        }
    }, 1000);
}

新建线程:

new  Thread(new Runnable() {
    @Override
    public void run() {
        /**
         * 要进行的操作
         */
}
}).start();

获取当前时间

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String time = formatter.format(curDate);
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部