Registering a ContentObserver in a Android Service
<p>I am working on parental control/adult content filtering application. This app continuously monitors the calls and smses on a child's mobile and logs all the activity onto a server. For this I am starting a service (MyService.java)
on BOOT_COMPLETED and in the onCreate method of the service I register a contentobserver for the callLog and sms uri ( refer to the code snippet below ) . </p>

<p>Now the issue is, Since I want to monitor every outgoing, incoming call s and sms I want the service to be continuosly running ( without being  stopped/killed) . Moreover this Service is being just used for registering content observers and not doing any other processing(its OnstartCommand method is dummy ) , so android OS kills the service after sometime. <strong>How do I ensure that the service runs continuosly and keeps the contentobserver object alive ?</strong></p>

<p>Thanks
Srao</p>

<pre>   
public class MyService extends Service {

    private CallLogObserver clLogObs = null;
    public void onCreate() {        
        super.onCreate();       
         try{                           
             clLogObs = new CallLogObserver(this);
             this.getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, clLogObs);              
         }catch(Exception ex)
         {
             Log.e("CallLogData", ex.toString());
         }
        }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {   
    if( clLogObs !=null  )
    {
        this.getContentResolver().unregisterContentObserver(clLogObs);
    }
     super.onDestroy();

    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {  
        super.onStartCommand(intent, flags, startId);           

        return Service.START_STICKY;

    }

    @Override
    public boolean onUnbind(Intent intent) {

     return super.onUnbind(intent);
    }

</pre>
