Android中的ContentObserver问题
为什么这样注册getContentResolver().registerContentObserver(Uri.parse("content://sms"),true,n...
为什么这样注册getContentResolver().registerContentObserver(
Uri.parse("content://sms")
,true, new MySmsObserver(new Handler()));可以监听到有新信息,而这样
getContentResolver().registerContentObserver(
Uri.parse("content://sms/inbox")
,true, new SmsObserver(new Handler()));
却不行?我只是把Uri写的更具体了,是不是这个参数格式固定为"content://XXX",其中xxx为某个ContentProvider的名字,不能有子目录?真的是这样的话这个参数后面的true或者false有什么作用? 展开
Uri.parse("content://sms")
,true, new MySmsObserver(new Handler()));可以监听到有新信息,而这样
getContentResolver().registerContentObserver(
Uri.parse("content://sms/inbox")
,true, new SmsObserver(new Handler()));
却不行?我只是把Uri写的更具体了,是不是这个参数格式固定为"content://XXX",其中xxx为某个ContentProvider的名字,不能有子目录?真的是这样的话这个参数后面的true或者false有什么作用? 展开
2个回答
2015-11-10 · 知道合伙人金融证券行家
关注
展开全部
项目需求:
1 当联系人更新时,本地表可以获取到更新的通知
2 程序退出后,仍然可以更新
搜了网上很多文章,发现可以使用ContentObserver这个类实现需求1,对于需求2,当然是用Service实现了。
代码如下:
[java] view plaincopyprint?
public class DBUpdateService extends Service {
protected static final int LOCALCONTACTS_SYNC = 1;
private final static int ELAPSE_TIME = 5000;// FIXME: Should remind this
// time.
/*
* Set an observer to listen the LocalContacts' change. As the DB of contact
* would be change after make a call or send a message. We should listen the
* LogDB too.If it change, we believe the contact is not changed. It's not
* the best way,just is a temporary solution,but it can handle many cases.
*/
public ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i(TAG, "Contact onChange");
mHandler.removeMessages(LOCALCONTACTS_SYNC);
Log.v(TAG, "removeMessages finish.");
mHandler.sendEmptyMessageDelayed(LOCALCONTACTS_SYNC, ELAPSE_TIME);
}
};
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.v(TAG, "++++handleMessage " + msg);
switch (msg.what) {
case LOCALCONTACTS_SYNC:
//do what you want to do
break;
default:
break;
}
}
};
@Override
public void onCreate() {
super.onCreate();
<span style="white-space: pre;"> </span>......
getContentResolver().registerContentObserver(
<strong>ContactsContract.Contacts.CONTENT_URI</strong>, true, mObserver);<strong>---------注册ContentObserver</strong>
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;<strong>---------------这个可以使Service在需要运行时候自动运行,即使程序退出了</strong>
}
@Override
public void onDestroy() {
getContentResolver().unregisterContentObserver(mObserver);<span style="font-family: Arial,Helvetica,sans-serif;"><strong>-----------------------------------------------注销ContentObserver</strong></span>
}
}
public class DBUpdateService extends Service {
protected static final int LOCALCONTACTS_SYNC = 1;
private final static int ELAPSE_TIME = 5000;// FIXME: Should remind this
// time.
/*
* Set an observer to listen the LocalContacts' change. As the DB of contact
* would be change after make a call or send a message. We should listen the
* LogDB too.If it change, we believe the contact is not changed. It's not
* the best way,just is a temporary solution,but it can handle many cases.
*/
public ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i(TAG, "Contact onChange");
mHandler.removeMessages(LOCALCONTACTS_SYNC);
Log.v(TAG, "removeMessages finish.");
mHandler.sendEmptyMessageDelayed(LOCALCONTACTS_SYNC, ELAPSE_TIME);
}
};
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.v(TAG, "++++handleMessage " + msg);
switch (msg.what) {
case LOCALCONTACTS_SYNC:
//do what you want to do
break;
default:
break;
}
}
};
@Override
public void onCreate() {
super.onCreate();
......
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, mObserver);---------注册ContentObserver
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;---------------这个可以使Service在需要运行时候自动运行,即使程序退出了
}
@Override
public void onDestroy() {
getContentResolver().unregisterContentObserver(mObserver);-----------------------------------------------注销ContentObserver
}
}
参考:
http://stackoverflow.com/questions/10173996/content-observer-onchange-method-called-twice-after-1-change-in-cursor?answertab=votes#tab-top
http://stackoverflow.com/questions/10431881/how-to-know-the-contact-is-updated-in-android/10432259#10432259
http://blog.csdn.net/luoshengyang/article/details/6985171
更新:
对于问题2,只需要注册了contentobserver就可以实现了,因为系统有一个ContentService会管理注册的observer。
如果有数据修改,系统会自动向注册的observer发送通知。
代码中需要实现两个onChange
1 当联系人更新时,本地表可以获取到更新的通知
2 程序退出后,仍然可以更新
搜了网上很多文章,发现可以使用ContentObserver这个类实现需求1,对于需求2,当然是用Service实现了。
代码如下:
[java] view plaincopyprint?
public class DBUpdateService extends Service {
protected static final int LOCALCONTACTS_SYNC = 1;
private final static int ELAPSE_TIME = 5000;// FIXME: Should remind this
// time.
/*
* Set an observer to listen the LocalContacts' change. As the DB of contact
* would be change after make a call or send a message. We should listen the
* LogDB too.If it change, we believe the contact is not changed. It's not
* the best way,just is a temporary solution,but it can handle many cases.
*/
public ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i(TAG, "Contact onChange");
mHandler.removeMessages(LOCALCONTACTS_SYNC);
Log.v(TAG, "removeMessages finish.");
mHandler.sendEmptyMessageDelayed(LOCALCONTACTS_SYNC, ELAPSE_TIME);
}
};
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.v(TAG, "++++handleMessage " + msg);
switch (msg.what) {
case LOCALCONTACTS_SYNC:
//do what you want to do
break;
default:
break;
}
}
};
@Override
public void onCreate() {
super.onCreate();
<span style="white-space: pre;"> </span>......
getContentResolver().registerContentObserver(
<strong>ContactsContract.Contacts.CONTENT_URI</strong>, true, mObserver);<strong>---------注册ContentObserver</strong>
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;<strong>---------------这个可以使Service在需要运行时候自动运行,即使程序退出了</strong>
}
@Override
public void onDestroy() {
getContentResolver().unregisterContentObserver(mObserver);<span style="font-family: Arial,Helvetica,sans-serif;"><strong>-----------------------------------------------注销ContentObserver</strong></span>
}
}
public class DBUpdateService extends Service {
protected static final int LOCALCONTACTS_SYNC = 1;
private final static int ELAPSE_TIME = 5000;// FIXME: Should remind this
// time.
/*
* Set an observer to listen the LocalContacts' change. As the DB of contact
* would be change after make a call or send a message. We should listen the
* LogDB too.If it change, we believe the contact is not changed. It's not
* the best way,just is a temporary solution,but it can handle many cases.
*/
public ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i(TAG, "Contact onChange");
mHandler.removeMessages(LOCALCONTACTS_SYNC);
Log.v(TAG, "removeMessages finish.");
mHandler.sendEmptyMessageDelayed(LOCALCONTACTS_SYNC, ELAPSE_TIME);
}
};
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.v(TAG, "++++handleMessage " + msg);
switch (msg.what) {
case LOCALCONTACTS_SYNC:
//do what you want to do
break;
default:
break;
}
}
};
@Override
public void onCreate() {
super.onCreate();
......
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, mObserver);---------注册ContentObserver
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;---------------这个可以使Service在需要运行时候自动运行,即使程序退出了
}
@Override
public void onDestroy() {
getContentResolver().unregisterContentObserver(mObserver);-----------------------------------------------注销ContentObserver
}
}
参考:
http://stackoverflow.com/questions/10173996/content-observer-onchange-method-called-twice-after-1-change-in-cursor?answertab=votes#tab-top
http://stackoverflow.com/questions/10431881/how-to-know-the-contact-is-updated-in-android/10432259#10432259
http://blog.csdn.net/luoshengyang/article/details/6985171
更新:
对于问题2,只需要注册了contentobserver就可以实现了,因为系统有一个ContentService会管理注册的observer。
如果有数据修改,系统会自动向注册的observer发送通知。
代码中需要实现两个onChange
展开全部
Uri.parse("content://sms/inbox")
初略看了下,android中应该没有inbox这么一说的。sms是跟contract中的ID走的
初略看了下,android中应该没有inbox这么一说的。sms是跟contract中的ID走的
更多追问追答
追问
问题是
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms/inbox")
, null, null, null, null);
while (cursor.moveToNext())
{。。。。。。。}
这样却可以查询到收件箱的内容
追答
监听和查找是一样的吗?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询