1. 注册动态广播:时间改变监听
  /**
     * interval update time
     */
    private void registerUpdateTimeReceiver() {
        //register time update
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        registerReceiver(mTimeUpdateReceiver, filter);
    }
  1. 接受时间改变监听,处理事件
  /**
     * broad receive time update
     */
    BroadcastReceiver mTimeUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }
            String action = intent.getAction();
            if (action == null || action.isEmpty()) {
                return;
            }

            if (action.equals(Intent.ACTION_TIME_TICK)) {
                //system every 1 min send broadcast
                updateTimeUi();
            } else if (action.equals(Intent.ACTION_TIME_CHANGED)) {
                //system hand change time send broadcast
                updateTimeUi();
            }
        }
    };
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐