Wesley
Wesley

Reputation: 1808

Android Service Class - OnCreate called only once

I'm starting a BroadCast Receiver when net Connectivity Changes in Android. The BroadCast Receiver Starts a Service which handles a Async task to fetch data from server. The problem is little weird, when i test in my device the Service OnCreate method is run for first time "Hurray!" but when i disconnect the network and again try to connect. The BroadCast is Read but not the Service. Am I missing something here? or Is this a bug? or should there be any Intent Actions when I start my Service? Please Help me.

Upvotes: 1

Views: 7319

Answers (2)

waqaslam
waqaslam

Reputation: 68177

The onCreate method in service runs only once till the entire life of service. However, calls to onStartCommand method can be made multiple times by calling startService. If your service is destroyed and then run again, the onCreate will be called again.

This is what this document tells:

onCreate()

The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand() or onBind()). If the service is already running, this method is not called.

Upvotes: 8

Sadeshkumar Periyasamy
Sadeshkumar Periyasamy

Reputation: 4908

The onCreate method of the service will be called first time you start the service only.

Unless you stop the service by stopService or stopSelf methods, the service will be running and the onCreate will not be called again.

But onStartCommand will be called each time you call startService

To Know More : Android Services

Upvotes: 2

Related Questions