美文网首页
时间更新——TimeObserveView

时间更新——TimeObserveView

作者: Anieal | 来源:发表于2017-08-09 15:49 被阅读0次

实现每分钟更新一下时间

TimeObserveView

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.zdy.tv.utils.TimeUtils;

public class TimeObserveView extends android.support.v7.widget.AppCompatTextView {

    private BroadcastReceiver timeTickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            refreshTime();
        }
    };

    public TimeObserveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        refreshTime();
    }

    private void refreshTime() {
        setText(TimeUtils.getTodayTime());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        IntentFilter filter = new IntentFilter();
        //代码核心
        filter.addAction(Intent.ACTION_TIME_TICK);
        getContext().registerReceiver(timeTickReceiver, filter);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getContext().unregisterReceiver(timeTickReceiver);
    }
}

TimeObserveView 使用

<com.zdy.tv.ui.next.view.TimeObserveView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="68dp"
        android:layout_marginTop="40dp"
        android:textColor="#adb0b4"
        android:textSize="18sp"
        tools:text="05月04日 周四" />

总结

    其实只要使用好ACTION_TIME_TICK,就能很容易实现每分钟刷新一下时间,至此又了解了一个新知识。

相关文章

  • 时间更新——TimeObserveView

    实现每分钟更新一下时间 TimeObserveView TimeObserveView 使用 总结 其实只要使用好...

  • 更新时间

    筛选条件: (1) //p[@class='P-gx P-gx-zf'][contains(text(),'昨')...

  • 更新时间

    因为作者大大是学生党,现在又开学了,so不定期更新,希望体谅

  • 更新时间。

    以后正文在每周五和周六和周日更新。简单点说就是周末。其他的闲杂时间我会跟一些课外题。

  • 更新时间

    终于更新了,4月注定是热闹的 ,动画有《一拳超人》,美剧有《权力的游戏》,电影更是前有《沙赞》后有《复联4》,...

  • 更新时间

    又是熟悉的更新时间,突然发现好像每天基本都是十一点多十二点才更新,这么晚更新一是因为怕日更断了,另一方面是...

  • 更新时间再定

    小伙伴们新年快乐,老杨最近在家都呆傻了,一直没办法出门,天天除了吃就是睡,口罩也用的一干二净,煮了用用了在煮,希望...

  • 更新时间再定

    小伙伴们新年快乐,老杨最近在家都呆傻了,一直没办法出门,天天除了吃就是睡,口罩也用的一干二净,煮了用用了在煮,希望...

  • 更新时间再定

    小伙伴们新年快乐,老杨最近在家都呆傻了,一直没办法出门,天天除了吃就是睡,口罩也用的一干二净,煮了用用了在煮,希望...

  • 更新时间再定

    小伙伴们新年快乐,老杨最近在家都呆傻了,一直没办法出门,天天除了吃就是睡,口罩也用的一干二净,煮了用用了在煮,希望...

网友评论

      本文标题:时间更新——TimeObserveView

      本文链接:https://www.haomeiwen.com/subject/gchqrxtx.html