title: Register ContentObserver in Application.OnCreate() VS Service
body: <p>I need my app to monitor (and parse) outgoing calls (among other things).  </p>
  
  <ul>
  <li>There is a broadcast for that "NEW_OUTGOING_CALL" but it is fired when starting the call and I need to know when is it actually finished.  </li>
  <li>For that I could create a <code>PhoneStateListener</code> and listen to "LISTEN_CALL_STATE" but it gets fired too soon again as the call is not yet inside the call log database. (I could launch a timer but.. beurk..)</li>
  <li>On last resort, I can put a <code>ContentObserver</code> on the CallLog URI (and get rid of the multiple calls) and detect the new entries.</li>
  </ul>
  
  <p>That's actually how I'm trying to do (the third option), but for that I of course need to register my ContentObserver, hence the question : <strong>where and when should I register it ?</strong></p>
  
  <p>Apparently, the answer seems to be : within a <code>Service</code>..<br>
  But then when to start the service ? </p>
  
  <ul>
  <li>I need it on start of my application to catch up with missing entries, so I would need to start the service from all the "callable" activities... Here the app needs to be manually opened, could be a problem after a reboot.</li>
  <li>I need it whenever a new call is made : can be achieved with the <code>BroadcastListener</code> on "NEW_OUTGOING_CALL", starting the service if it's not already on. Get rid of the reboot problem here (as long as the app has been launched once and not user-killed since then)</li>
  </ul>
  
  <p>But what would be wrong by registering them within the <strong><code>Application.OnCreate()</code></strong>  ?  </p>
  
  <ul>
  <li>I already need to subclass the Application (or at least that's how I handled other things)  </li>
  <li>from my understanding, the <code>Application.OnCreate()</code> is going to be called anyway either when one of my <code>Activity</code> is created (first time), or my <code>BroadcastReceiver</code> (after a reboot) starts the service, anything that needs my process to start...   </li>
  <li><code>Service</code> lives also as long as my process lives.</li>
  <li>I don't need an extra <code>Service</code> for that, don't have to bind, start etc... </li>
  </ul>
  
  <p>So is there anything wrong registering ContentObserver in <code>Application.OnCreate()</code> or what would be the advantage of using a <code>Service</code> for that ?</p>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>On the pro-service I'll just discovered that : </p>
  
  <ul>
  <li><p>If I want my application to start at boot_time, I listen to "BOOT_COMPLETED" with my <code>BroadcastReceiver</code> right ? So my process will be created, and I can register my <code>ContentObservers</code>... but then it will be quickly destroyed :</p>
  
  <p><code>07-24 15:44:50.260: I/ActivityManager(531): No longer want com.test.test (pid 1829): empty #17</code>  </p></li>
  </ul>
  
  <p>That's because Android flagged it as a <a href="http://developer.android.com/guide/components/processes-and-threads.html" rel="nofollow">"Level 5 : Empty process"</a>. Why ? Because registering ContentObservers don't make my Process "not empty".<br>
  Starting a service on the other hand makes my process a "Level3 : Service process" and it won't get cleaned anymore (at least not right away..) so if later on I launch the app, the process is already here, quietly listening... </p>