Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>i did a similar implementation and had set a <code>ContentObserver</code> over calendar's events <code>URI</code>. and i faced all the problems, you are facing now. So your question with my solutions/suggestions goes here.... </p> <blockquote> <p>1) Once I have set up a service that registers a ContentObserver on my Cursor, does that observer only exist within the service? I mean, if the service is killed, does the contentObserver continue to observe?</p> </blockquote> <p>No, it will not observe the contents your contents over URI. but there is solution to this. you can create an always running service. This service will be created again as soon as it is dismissed due to some memory issue or other unexpected dismissal. It will always continue to run unless it is dismissed explicitly. To create such service override <code>OnStartCommand(Intent intent, int flags, final int startId)</code> in your Service class and return <code>START_STICKY</code>. Have a look at the code below. </p> <pre><code>public int onStartCommand(Intent intent, int flags, final int startId) { String authenticationKey = ((MainApplication) getApplicationContext()).getDbConnector().getUserAuthDefault() ; // if user is not registered yet, stop this service, it may be called from boot reciever. if(authenticationKey == null){ stopSelf(); } // restart, if we get killed. return START_STICKY; } </code></pre> <p>.</p> <blockquote> <p>2: I suspect the answer is no, but I will ask anyway. Is there anyway of knowing which contact being updated is triggering the onchange method of my contentObserver? currently I have to compile the list of all teh contacts on the phone and send them off to my remote server, it would be so much easier to just send details of the contacts being updated.</p> </blockquote> <p>You again guessed it right.:). the answer is No.<br> There is no way to know specifically which contact has been updated. ContentObserver just provide an event to let you know that some change has been made to the data pointed by the uri. But i think you are handling the situation inefficiently, as you are compiling list of all the contacts and sending them back to the server.<br> I would suggest you to maintain a list of contacts, which are successfully sent to server, in the local storage. And next time when you get a call in <code>onChange()</code> method of <code>contentObserver</code> you fetch all <code>contacts</code> from <code>content_URI</code>, compare them with previously stored list and send only updated contacts to server. </p> <blockquote> <p>3: This is my main question, when I make a change to my Contact List the onChange method is being fired twice in quick succession. 1 change, 2 calls. Is there anyway of managing this?</p> </blockquote> <p>Yes this happens, but i guess you are in wrong impression. In my case it used to fire randomly twice or thrice or even more times. so i had set a threshold_time interval. A time interval within which if <code>ContentObserver</code> is fired again, then i would not process it. I did something like this.. </p> <pre><code>long lastTimeofCall = 0L; long lastTimeofUpdate = 0L; long threshold_time = 10000; /* (non-Javadoc) * @see android.database.ContentObserver#onChange(boolean) */ @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Log.d("content service", "on change called"); lastTimeofCall = System.currentTimeMillis(); if(lastTimeofCall - lastTimeofUpdate &gt; threshold_time){ //write your code to find updated contacts here lastTimeofUpdate = System.currentTimeMillis(); } } </code></pre> <p>Hope these suggestions/solutions would help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload