开发者

Android: using registerContentObserver() to be notified as the contacts are changed

I'm using registerContentObserver() to be notified as the contacts are changed, but when I register for the content uri:People.CONTENT_URI and when I observe in the log cat I'm getting the notify as "开发者_如何学运维false" even after changing the contact.

I have also overridden the deliverSelfNotification to true. What am I doing wrong?


Not sure what your asking, your question is a bit vague.

Here is how I listen out for changes in the SMS content provider, you may find it useful

String url = "content://sms/"; 
        Uri uri = Uri.parse(url); 
        getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler)); 

        /uriSms = Uri.parse("content://sms/inbox");
        Cursor c = getContentResolver().query(uriSms, null,null,null,null); 

        //Log.d("COUNT", "Inbox count : " + c.getCount());


}

class MyContentObserver extends ContentObserver { 

    public MyContentObserver(Handler handler) { 

        super(handler); 

    }

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

@Override public void onChange(boolean arg0) { 
    super.onChange(arg0);

     Log.v("SMS", "Notification on SMS observer"); 

    Message msg = new Message();
    msg.obj = "xxxxxxxxxx";

    handler.sendMessage(msg);

    Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
    String protocol = cur.getString(cur.getColumnIndex("protocol"));
    if(protocol == null){
           Log.d("SMS", "SMS SEND"); 
           int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
           Log.d("SMS", "SMS SEND ID = " + threadId); 
           getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);

    }
    else{
        Log.d("SMS", "SMS RECIEVE");  
         int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
         getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
    }

}


If you are targeting anything newer than api level 3, you should use ContactsContract.Contacts.CONTENT_URI.

and then it's just a matter of: getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentObserver);

You will not know what has changed with this method though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜