Hong
Hong

Reputation: 546

When calling service, onCreate is run but not onStartCommand()

I'm working on the code to call a service when android finishes boot-up.

The service is defined in the manifest.xml called myService.

In the class myService, the code override two methods:

@Override
public void onCreate(){

    myInit();

    Log.d(TAG, "onCreate: ");

}


public int onStartCommand(){

    Log.d(TAG, "onStartCommand first line");

    startScheduler();

    Log.d(TAG, "onStartCommand: ");

    //This service is running in the background all the time. Therefore, return sticky.
    return START_STICKY;
}

While running, the logcat can see "onCreate" but never "onStartCommand". I read through the developer website but can't figure out why.

Comments and suggestions are highly appreciated.

Upvotes: 0

Views: 1224

Answers (1)

Chris
Chris

Reputation: 10435

onStartCommand takes an Intent and two integer parameters.

Try replacing your method with:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand first line");

    startScheduler();

    Log.d(TAG, "onStartCommand: ");

    //This service is running in the background all the time. Therefore, return sticky.
    return START_STICKY;
}

As a general rule, if you think you're overriding or implementing a method, always annotate it with @Override. That will make the compiler error if you miss a parameter, get the name wrong, etc.

Upvotes: 2

Related Questions