tags:

views:

155

answers:

2

My application after an update needs to fill its ContentProvider with some data found on the server. I found that starting a service from the ContentProvider.onCreate() method works but I'm not sure if it is a good idea or if it works randomly.

I don't know the initialization of an application in android. Are the services always already available before the content provider is created?

A: 

With more details, the more accurate of an answer you will receive, but assuming that your service is associated with your ContentProvider, it is OK.

If your service is called within ContentProvider's onCreate, then it will be available. But if your onCreate is not invoked, anything that is created inside of the method will not be available.

Anthony Forloney
A: 

Hmm...in theory it should be fine, but I'm asking myself why you need a service. For me a service is something running in the background, performing some task which could also be to keep your app up-to-date with fresh data from a server.

If I understand you correctly and you implemented your own content provider, I'd assume that you have to fetch your data from the server just at the time your content provider is being queried by some activity/service, whatever. Wouldn't that be more appropriate and resource gentle?

I don't know the initialization of an application in android. Are the services always already available before the content provider is created?

Android apps are usually designed to be modular which is also why for instance you could share an Activity of your app with other apps or actually invoke - for instance - a SMS sender activity of your Android device. Therefore there isn't a Main() somewhere as you may be accustomed in normal desktop apps, but rather you have the onCreate() which is called by the Android OS to invoke the start of an Activity.

The only entry point I could immagine that is nearest to a Main() in desktop apps is to override Android Application class and register it appropriately in your manifest file

public class AndroidApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}

and your manifest

<application android:name=".main.AndroidApplication" android:icon="@drawable/icon" android:label="@string/app_name">
...
</application>

Here you have your onCreate() which is somehow the first method called when your app starts. But still I would pay attention in what you're going to initialize there. You should always just initialize and load those resources which you really need.

Juri
I already have the service that keeps the data up to date.But in case the application gets updated I need a way to force the update service to do its job before the scheduled moment.
Tughi