日期:2014-05-16  浏览次数:20492 次

Android数据库内容变化的监听

Android数据库内容变化的监听??

?

首先介绍内容监测的基本模式
基于uri的内容监测的基本模式被android.content.ContentResolver实现
它为基于Uri的内容监测的提供了一个平台。(其实如果有必要,我们可以自己实现一个)
ContentResolver为此提供了三个方法:
注册监听器到某个uri
public?final?void?registerContentObserver?(Uri?uri,?boolean?notifyForDescendents,?ContentObserver?observer)
Register?an?observer?class?that?gets?callbacks?when?data?identified?by?a?given?content?URI?changes.
Parameters
uri?????The?URI?to?watch?for?changes.?This?can?be?a?specific?row?URI,?or?a?base?URI?for?a?whole?class?of?content.
notifyForDescendents?????If?true?changes?to?URIs?beginning?with?uri?will?also?cause?notifications?to?be?sent.?
????If?false?only?changes?to?the?exact?URI?specified?by?uri?will?cause?notifications?to?be?sent.?
????If?true,?than?any?URI?values?at?or?below?the?specified?URI?will?also?trigger?a?match.
observer?????The?object?that?receives?callbacks?when?changes?occur.
取消被注册的监听器
public?final?void?unregisterContentObserver?(ContentObserver?observer)
Unregisters?a?change?observer.
参数
observer?????The?previously?registered?observer?that?is?no?longer?needed.
通知所有注册到uri的监听器,告诉他们内容发生了改变。
public?void?notifyChange?(Uri?uri,?ContentObserver?observer)
Notify?registered?observers?that?a?row?was?updated.?To?register,?call?registerContentObserver().?By?default,?CursorAdapter?objects?will?get?this?notification.
参数
observer?????The?observer?that?originated?the?change,?may?be?null .用以说明observer是第一个发现内容改变的ContentObserver,可为null.
通知所有注册到uri的监听器,告诉他们内容发生了改变。
public?void?notifyChange?(Uri?uri,?ContentObserver?observer,?boolean?syncToNetwork)
Notify?registered?observers?that?a?row?was?updated.?To?register,?call?registerContentObserver().?By?default,?CursorAdapter?objects?will?get?this?notification.
参数
observer?????The?observer?that?originated?the?change,?may?be?null?用以说明observer是第一个发现内容改变的ContentObserver,可为null
syncToNetwork?????If?true,?attempt?to?sync?the?change?to?the?network.?
注1:"基于uri的内容监测的基本模式被android.content.ContentResolver实现",严格来说ContentResolver至少提供了接口。
真正的实现在android.content.ContentService.
其实ContentResolver为基于Uri的内容监测所提供的方法只是调用ContentService相关的方法
注2:"注册监听器到uri"只是说如果notifyChangeuriregisterContentObserver中的uri相匹配,则调用observer的方法onChange
内容监听器ContentObserver主要有三个方法
public?boolean?