tags:

views:

84

answers:

1

I have registered a BOOT_COMPLETED Receiver in my application, i want to listen for changes on SMS Database.

The BroadcastReceiver starts a temporary Service which registers the ContentObserver. Now i want to notify my main activity (which is not started) that the Observer was registered successful. (I want to do this, because if a user installs the app for the first time he don't restart his phone but needs the ContentObserver too. If you have another idea how to do that you can post it. I just want the information that the observer is registered already)

My idea was to notify the activity with a static attribute: MyActivity.sObserverRegistered = true

But i think that is not good and doesn't work because the activity isn't started and this is ignored. Any idea how to solve the problem?

thanks

+1  A: 

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.

CommonsWare
thanks, i will keep the service running, and then reuse it when activity starts. it uses stopSelf() which causes the problems you mentioned. Another point because of the "SMS Content provider": I searched a long time for solution and there aren't proper documented solutions. But this is the only solution i know with the ContentProvider at URI "content://sms" . At the moment it worked on all devices, but must be kept in eyes for the future
Nitromouse