13
votes

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 ) .

Now the issue is, Since I want to monitor every outgoing, incoming call s and sms I want the service to be continuously 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. How do I ensure that the service runs continuously and keeps the contentobserver object alive ?

   
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);
    }
4
People have already commented on how you can't keep a service running but, since it sounds like the service just registers ContentObservers, it sounds like the real question is whether those observers stay registered when the service is killed (I believe they do) and how to keep from re-registering new observer instances if the service is re-started.spaaarky21

4 Answers

3
votes

You cannot ensure your service to be running continuously on Android.

For the use-case you mention, it is better to rely on Broadcast receiver like ACTION_NEW_OUTGOING_CALL & SMS_RECEIVED.

If you feel, above supported Broadcast receivers doesn't cover all your use-cases. Use AlaramManager to periodically start your SERVICE and look into CALL_LOGS and SMS table for any change in data and take appropriate action (this may involve check marking the last visited data on CALL_LOGS and SMS table).

2
votes

you can set the service to run in the foreground . this will give your service a much lower chance of being killed by the OS .

read here for more information .

0
votes

The content observer get unregistered as soon as your service get killed. So registering once is not an option and your service must be running all the time. to guarantee this you must return START_STICKY from onStartCommand. You can also start the service in foreground.

But what if a new call arrive when the service is killed? To handle this you can store last processed _id in preferences of your app. and each time get all the new call logs with greater id than saved id. You must process the logs on service onCreate as well as on observer onChange.

-1
votes

You cant guarantee it will run continuously, but you can return START_STICKY from onStartCommand which guarantees that Android will re-start your service if it is killed for any reason.