Register ContentObserver in Application.OnCreate () VS Service

I need my application to monitor (and analyze) outgoing calls (by the way).

  • There is a broadcast for this "NEW_OUTGOING_CALL", but it starts when the call starts, and I need to know when it is actually completed.
  • To do this, I could create a PhoneStateListener and listen to "LISTEN_CALL_STATE", but it starts up again too quickly since the call is not already in the call log database. (I could start the timer, but .. beurk ..)
  • As a last resort, I can put ContentObserver in the ContentObserver URI (and get rid of a few calls) and discover new entries.

Actually, how am I trying to do this (third option), but for this I, of course, must register my ContentObserver, so the question is: where and when should I register it?

Apparently the answer seems to be: within Service .
But then, when to start the service?

  • I need this when starting my application to catch up with the missing entries, so I will need to start the service from all the "called" actions ... Here the application must be opened manually, it may be a problem after rebooting.
  • I need this with every new call: it can be achieved using the BroadcastListener in "NEW_OUTGOING_CALL", starting the service, if it is not already enabled. Get rid of the reboot problem here (while the application was launched once, and not killed since)

But what would be wrong if registering them in Application.OnCreate() ?

  • I already need to subclass the application (or at least the way I handled other things)
  • from my understanding, Application.OnCreate() will be called in any case either when creating one of my Activity (first time), or my BroadcastReceiver (after reboot) starts the service, all I need is the start process ...
  • Service lives as long as my process.
  • I do not need an additional Service for this, I do not need to bind, start, etc.

So, is there something wrong with registering ContentObserver in Application.OnCreate() or what is the advantage of using Service for this?

UPDATE:

In the pro-service, I just found that:

  • If I want my application to run in boot_time, am I listening to "BOOT_COMPLETED" with my BroadcastReceiver correctly? Thus, my process will be created, and I can register my ContentObservers ... but then it will be quickly destroyed:

    07-24 15:44:50.260: I/ActivityManager(531): No longer want com.test.test (pid 1829): empty #17

This is because Android is marked as “Level 5: Process Empty” . What for? Since registering ContentObservers does not make my process "not empty."
On the other hand, starting the service does my “Level3: Service process” process, and it will not be cleaned up anymore (at least not right away.), Therefore, if I later launched the application, the process is already here, quietly listening ...

+6
source share

Source: https://habr.com/ru/post/950146/


All Articles