Android 如何监听联系人数据库中的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to listen for changes in Contact Database
提问by hap497
I am trying to listen for any change in the contact database.
我正在尝试侦听联系人数据库中的任何更改。
So I create my contentObserver which is a child class of ContentObserver
:
所以我创建了我的 contentObserver 这是一个子类ContentObserver
:
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
System.out.println (" Calling onChange" );
}
}
MyContentObserver contentObserver = new MyContentObserver();
context.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);
But When I use 'EditContactActivity
' to change the contact database, My onChange()
does not get called.
但是,当我使用“ EditContactActivity
”更改联系人数据库时,onChange()
不会调用My 。
回答by MannyNS
I have deployed your example as it is and it works fine.
我已经按原样部署了您的示例,并且运行良好。
package com.test.contentobserver;
import android.app.Activity;
import android.database.ContentObserver;
import android.os.Bundle;
import android.provider.Contacts.People;
public class TestContentObserver extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyContentObserver contentObserver = new MyContentObserver();
getApplicationContext().getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI,
true,
contentObserver);
}
private class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(this.class.getSimpleName(), "A change has happened");
}
}
}
Something else must be wrong...
一定是别的什么地方出了问题……
Are you making the changes through the cursor the observer is registered with?
您是否通过观察者注册的光标进行更改?
Check that with the Observer function deliverSelfNotifications(). (it returns false by default)
使用 Observer 函数 deliverySelfNotifications() 进行检查。(默认返回false)
You may want to override that observer function with something like:
您可能希望使用以下内容覆盖该观察者函数:
@Override
public boolean deliverSelfNotifications() {
return true;
}
回答by Nizam
EDIT:MannyNS's answer has now been updated with the new URI (ContactsContract.Contacts.CONTENT_URI
)
编辑:MannyNS 的答案现已更新为新的 URI ( ContactsContract.Contacts.CONTENT_URI
)
A simple TIP about MannyNS 's answer.
关于 MannyNS 答案的简单提示。
Here, People.CONTENT_URI
is deprecated.
此处,People.CONTENT_URI
已弃用。
Code as follows instead.-->ContactsContract.Contacts.CONTENT_URI
代码如下。-->ContactsContract.Contacts.CONTENT_URI
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentobserver);