Android 实时获取系统时间

按分钟

  mViewBinding.butStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentFilter filter = new IntentFilter();//创建意图过滤器对象
                filter.addAction(Intent.ACTION_TIME_TICK);//为接收器指定action,使之用于接收同action的广播
                registerReceiver(receiver, filter);//动态注册广播接收器

                Calendar instance = Calendar.getInstance();
                int year = instance.get(Calendar.YEAR);
                int month = instance.get(Calendar.MONTH) + 1;
                int day = instance.get(Calendar.DAY_OF_MONTH);
                int hour = instance.get(Calendar.HOUR_OF_DAY);
                int minute = instance.get(Calendar.MINUTE);
                int second = instance.get(Calendar.SECOND);

                tvTime.setText(getStr(hour) + ":" + getStr(minute));
            }

        });
 static BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_TICK)) {

                System.out.println(" xxxxxxxxxxxxxx ");
                if (tvTime != null) {
                    //TODO更新时间
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//                    //获取当前时间
//                    Date date = new Date(System.currentTimeMillis());
//                    String format = simpleDateFormat.format(date);


                    Calendar instance = Calendar.getInstance();

                    int year = instance.get(Calendar.YEAR);
                    int month = instance.get(Calendar.MONTH) + 1;
                    int day = instance.get(Calendar.DAY_OF_MONTH);
                    int hour = instance.get(Calendar.HOUR_OF_DAY);
                    int minute = instance.get(Calendar.MINUTE);
                    int second = instance.get(Calendar.SECOND);

                    tvTime.setText(getStr(hour) + ":" + getStr(minute));
                }
            }
        }
    };


    public static String getStr(int a) {
        String mStr;
        if (a < 10) {
            mStr = "0" + a;
        } else {
            mStr = a + "";
        }
        return mStr;
    }

秒的

private boolean flag = false;


    static Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                Calendar instance = Calendar.getInstance();
                int year = instance.get(Calendar.YEAR);
                int month = instance.get(Calendar.MONTH) + 1;
                int day = instance.get(Calendar.DAY_OF_MONTH);
                int hour = instance.get(Calendar.HOUR_OF_DAY);
                int minute = instance.get(Calendar.MINUTE);
                int second = instance.get(Calendar.SECOND);

                tvTime.setText(getStr(minute) + ":" + getStr(second));
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        tvTime = mViewBinding.tvTime;
        flag = true;

        new Thread() {
            @Override
            public void run() {
                while (flag) {

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message msg = new Message();
                    msg.what = 0;  //消息(一个整型值)
                    handler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler

                }
            }
        }.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag = false;
    }

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值