Registering a ContentObserver in a Android Service -
i working on parental control/adult content filtering application. app continuously monitors calls , smses on child's mobile , logs activity onto server. starting service (myservice.java) on boot_completed , in oncreate method of service register contentobserver calllog , sms uri ( refer code snippet below ) .
now issue is, since want monitor every outgoing, incoming call s , sms want service continuously running ( without being stopped/killed) . service being used registering content observers , not doing other processing(its onstartcommand method dummy ) , android os kills service after sometime. how ensure service runs continuously , keeps 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); }
you cannot ensure service running continuously on android.
for use-case mention, better rely on broadcast receiver action_new_outgoing_call & sms_received.
if feel, above supported broadcast receivers doesn't cover use-cases. use alarammanager periodically start service , call_logs , sms table change in data , take appropriate action (this may involve check marking last visited data on call_logs , sms table).
Comments
Post a Comment