You have much bigger problems than that.
i want to listen for changes on SMS
Database.
There is no "SMS database", or even an SMS content provider, in the Android SDK. Attempting to access the private proprietary undocumented not-to-be-touched SMS application contents will break on some devices, will break in future versions of Android, will not work with third-party SMS applications, and is generally a bad idea.
The BroadcastReceiver starts a
temporary Service which registers the
ContentObserver.
There is no such thing as a temporary Service
which registers a ContentObserver
.
It might be that the Service
is not temporary, so your ContentObserver
remains registered and your Service
is not shut down. This would occur, for example, if your BroadcastReceiver
called startService()
and your Service
does not call stopSelf()
(e.g., it is not an IntentService
). This is not great, because you are now tying up a process. But, if you can convince your users that it is OK that you are tying up a process, this is the best answer, and your activity can just (re-)start the service when the activity starts up, to ensure that the ContentObserver
is registered.
It might be that your ContentObserver
is not registered long, because the temporary Service
unregisters it right away when the Service
is shut down, and the Service is shut down right away. This is great from a memory consumption standpoint, but it probably isn't that effective for your goals.
Or, it might be that you are leaking memory, because you register the ContentObserver
and shut down the Service
without unregistering the ContentObserver
. This is horrible, because the only way the ContentObserver
will ever get cleaned up is if Android terminates the process. Moreover, it will do this whenever it feels like, because as far as it is concerned, you aren't using the process anymore, despite the thread and ContentObserver
and Service
you leaked. This will result in unreliable code at best.