John Smith
John Smith

Reputation: 2738

onCreate() in service never called

I want to implement a service that handle screen on/off. To do so, I have created BootReceiver that is called at boot completed and within I start my service.

However, OnCreate is never called, why?.

When I print the log of the program I never see the onCreate's log even though I see the onStartCommand's log. How is this possible? Please help me to understand this.

This is BootReceiver that is called in the very beginning:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.w("TAG", "BootReceiver");

        Intent service = new Intent(context, ScreenListenerService.class);
            context.startService(service);

    }

}

This is the service:

public class ScreenListenerService extends Service {


    public void OnCreate(){
        super.onCreate();
        Log.w("TAG", "ScreenListenerService---OnCreate ");
    }


     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

            Log.w("TAG", "ScreenListenerService---onStart ");


        }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

And the manifest:

<service android:name=".ScreenListenerService"></service>

        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                 <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
            </intent-filter>
        </receiver>

Upvotes: 0

Views: 7784

Answers (2)

stinepike
stinepike

Reputation: 54672

wrong spelling in onCreate. You are using OnCreate instead of onCreate. To get rid of this kind of mistake it is better to use @override annotation everytime you override a method

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82543

Change:

public void OnCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");
}

to:

public void onCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");
}

Java is case sensitive, so OnCreate() != onCreate()

Upvotes: 6

Related Questions