Android History Content Observer

前端 未结 3 1466
轮回少年
轮回少年 2021-02-10 06:18

I implemented content observer of history but it is behaving weird.For every change in history its onChange() function runs 3-5 times.

static class BrowserOberse         


        
3条回答
  •  暖寄归人
    2021-02-10 07:19

    The exact way in which history is updated is an implementation detail you probably do not want to be relying on. Rather than trying to guess whether the update is finished or not based on the values of various fields, I would suggest restarting a timer each time onChange(..) is called. If the timer expires, you can be reasonably certain that history has finished updating.

    Edit Example of how to use a timer:

    static class BrowserOberser extends ContentObserver implements Runnable {
        private Handler h;
    
        public BrowserOberser() {
            super(null);
            h = new Handler();
        }
    
        @Override
        public boolean deliverSelfNotifications() {
            return true;
        }
    
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            h.removeCallbacks(this);
            h.postDelayed(this, 500);
        }
    
        public void run() {
            Log.d("history observer", "change timer elapsed");
        }
    
    }
    

提交回复
热议问题