本文介绍了Android的历史内容观察员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我实现了历史内容的观察者却是表现weird.For在历史上每一次改变它的onChange()函数运行3-5次。

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 BrowserOberser extends ContentObserver {
    public BrowserOberser() {
        super(null);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.d("History", "onChange: " + selfChange);
    }

}

我也用我的注册观察者

I also registered my observer using

BrowserOberser observer = new BrowserOberser();
getApplication().getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, observer );

和补充所需的权限清单中。

and added required permissions in manifest.

的code工作正常,但的onChange();运行3-5次,历史上每一次变化
谁能帮我出一个解决这个问题呢?

The code works fine but onChange(); runs 3-5 times for each change in history
Can anyone help me out with a solution to this problem?

推荐答案

这就是我终于解开了我的情况ATLEAST的问题(我想读历史时,有变化的历史)

This is how I finally solved the problem for my case atleast(I wanted to read history when there is change in history)

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        /**
         * Get SharedPreferneces of the user
         */
        SharedPreferences pref= myContext.getSharedPreferences("com.tpf.sbrowser",
                Context.MODE_PRIVATE);
        long wherelong = pref.getLong("Date", 0);
        DatabaseManager db=new DatabaseManager(myContext,1);
        String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.URL, BookmarkColumns.DATE,};
        String sel = Browser.BookmarkColumns.BOOKMARK + " = 0";
        Cursor mCur = myContext.getContentResolver().query(
                Browser.BOOKMARKS_URI, proj, sel, null, null);
        Log.d("onChange", "cursorCount"+mCur.getCount());
        mCur.moveToFirst();
        String title = "";
        String url = "";
        long lastVisitedDate=0;
        DbMessage msg = new DbMessage(lastVisitedDate,url, title);
        /**
         * Start reading the user history and dump into database
         */
        if(mCur.moveToFirst() && mCur.getCount() > 0) {
              while (mCur.isAfterLast() == false) {
                  title =mCur.getString(0);
                  url = mCur.getString(1);
                  lastVisitedDate =mCur.getLong(2);
                  if ((lastVisitedDate>wherelong) && (!title.equals(url))) {
                      msg.set(lastVisitedDate, url, title);
                      db.InsertWithoutEnd(msg);
                      pref.edit().putBoolean("BrowserHistoryRead", true).commit();
                      pref.edit().putLong("Date", lastVisitedDate).commit();
                      myContext.updateTime(wherelong,lastVisitedDate);
                      wherelong=lastVisitedDate;
                  }
                  mCur.moveToNext();
              }
          }
    }

但如果仍然将是巨大的,如果有人能告诉的onChange哪个迭代(在问题的简单code)只对应哪个国家的页面加载。据我了解,在第一次迭代的标题是相同的URL,并在第三次迭代正确的页面标题是present。但重定向期间的onChange得到了所谓的高达5倍。因此,有人可以证实这迭代coressponds到哪个阶段?

But still if would be great if someone could tell which iteration of onChange(in the simple code of question) corresponds to exactly which state in page loading.From what I learned, in first iteration title was same as url and in the third iteration the correct page title was present. But during redirection, onChange got called upto 5 times. So can someone confirm which iteration coressponds to which stage?

这篇关于Android的历史内容观察员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:09