Android 倒计时器

原创 2018年04月17日 14:33:08

前9秒每隔3秒记录 log.e

public class TestActivity extends AppCompatActivity {

    private static final String TAG = TestActivity.class.getSimpleName();
    private Handler handler;
    private Timer timer;
    private int i;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        timer = new Timer();
        timer.schedule(new mTask(), 1000, 1000);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 3:
                        Log.e(TAG, "handleMessage: " + 3);
                        break;
                    case 3 * 2:
                        Log.e(TAG, "handleMessage: " + 3 * 2);
                        break;
                    case 3 * 3:
                        Log.e(TAG, "handleMessage: " + 3 * 3);
                        break;
                }
                return false;
            }
        });
    }

    class mTask extends TimerTask {
        @Override
        public void run() {
            handler.sendEmptyMessage(i++);
        }
    }
}

收藏助手
不良信息举报
您举报文章:Android 倒计时器
举报原因:
原因补充:

(最多只允许输入30个字)