Android onCreate or onStartCommand for starting service
onCreate()
is called when the Service
object is instantiated (ie: when the service is created). You should do things in this method that you need to do only once (ie: initialize some variables, etc.). onCreate()
will only ever be called once per instantiated object.
You only need to implement onCreate()
if you actually want/need to initialize something only once.
onStartCommand()
is called every time a client starts the service using startService(Intent intent)
. This means that onStartCommand()
can get called multiple times. You should do the things in this method that are needed each time a client requests something from your service. This depends a lot on what your service does and how it communicates with the clients (and vice-versa).
If you don't implement onStartCommand()
then you won't be able to get any information from the Intent
that the client passes to onStartCommand()
and your service might not be able to do any useful work.
When calling service, onCreate is run but not onStartCommand()
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.
What can I expect when onCreate() calls startService()
You are correct in that a Service
will call onCreate
and onStartCommand
if it is started via Context.startService
. So in this sense, when you return START_STICKY, the Service will continually run until an explicit call to stopService()
is called. It will also be destroyed and restarted during this lifecycle.
Another way to create a Service, is by binding to it. As per the docs:
Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand().
So, it's possible for a Service to be created by simply binding to it. However, the lifecycle of a Service indicates that it will remain if it is started or a client is still bound to it. Meaning, that if it was created by a bind command, it will immediately be destroyed as soon as the client unbinds.
So, if a Service starts itself in the onCreate()
, it will ensure that it puts itself in the started state regardless of whether it was created by binding or by an explicit call to startService
. Since there's no actionable intent, the onStartCommand
will just pass straight through. An clients that call startSevice
will, presumably, have actionable Intents in which case the Service will perform its duties.
Android Service onstart() getting called only after onCreate() of the calling activity ends
Does this will invoke service in new thread?
In Java, objects do not run on threads. Methods run on threads.
I can see onStartCommand of the service is invoked only after the onCreate() of the activity exits.
Correct. startService()
merely queues a request to start the service. Android cannot do that until you return control of the main application thread to the framework. So long as onCreate()
is running, you are tying up the main application thread. You need to make sure that each callback (e.g., onCreate()
) is very, very fast, ideally well under 1 millisecond in duration.
If yes how to start a service parallely, i.e. oncreate can continue to exit in parallel with the onStartCommand of the service.
That is not possible, sorry.
If android restarts a Service is onCreate called again?
See here, the dev guide. http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
onCreate()
is only called when the process starts, which can either be the first time the service is running, or if it was killed on restarted, essentially this is called whenever it starts.
onStartCommand()
is called whenever a client calls startService()
.
When a service is destroyed / completely stopped, Android is supposed to call onDestroy()
on that service. I think it's possible for that to not happen (e.g. process is killed not through Android system). In the case of a bound service, this is when there are not more active client binders.
Edit: onCreate()
Service starts; onStartCommand()
someone uses service; onDestroy()
Service is killed / stopped.
Android repeated Service - onCreate called once, onStartCommand called many
your service running many time because of start_sticky
if your service is killed by Android due to low memory, and Android clears some memory, then...
STICKY: ...Android will restart your service, because that particular flag is set.
NOT_STICKY: ...Android will not care about starting again, because the flag tells Android it shouldn't bother.
REDELIVER_INTENT: ...Android will restart the service AND redeliver the same intent to onStartCommand() of the service, because, again, of the flag.
suggest to your start_not_sticky
onCreate doesn't run in a service
Change your Service class to this:
public class ProximityBeacon extends Service
{
private static final String TAG = "TAG";
private String server;
private String ID;
public ProximityBeacon() {
}
public ProximityBeacon(String server)
{
this.server=server;
//TODO indirizzo del server
}
public void startBeaconDetecting(String ID, Intent intent, Context context)
{
this.ID=ID;
context.startService(intent);
}
public void stopBeaconDetecting()
{
stopSelf();
onDestroy();
}
public void onCreate()
{
super.onCreate();
Log.i(TAG, "ProximityBeacon Oncreate");
}
public int onStartCommand(Intent intent, int flag, int startId)
{
Log.i(TAG, "ProximityBeaconon StartCommand");
return Service.START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
and then call it using this:
new ProximityBeacon("").startBeaconDetecting("" ,new Intent(HomeActivity.this, ProximityBeacon.class) ,mContext) ;
then add it in manifest.xml like this :
<service android:name=".services.ProximityBeacon" />
Output : 19:25:11.873 23002-23002/com.sample.service I/TAG: ProximityBeaconon StartCommand
Related Topics
How to Display a Yes/No Dialog Box on Android
Can't Include C++ Headers Like Vector in Android Ndk
Livedata Update on Object Field Change
Updating the List View When the Adapter Data Changes
Cancel Notification on Remove Application from Multitask Panel
App Not Run in Visual Studio Emulator for Android in Visual Studio 2015
Findviewbyid() Returns Null When I Call It in Oncreate()
Android Studio Compile Error "Content Is Not Allowed in Prolog"
Android: When Should I Use a Handler() and When Should I Use a Thread
How to Get the Uuid of My Android Phone in an Application
When Should One Use Theme.Appcompat VS Themeoverlay.Appcompat
How to Add a Fast-Scroller to the Recyclerview
Android - Configure Spinner to Use Array
Android Material: Status Bar Color Won't Change
What's Wrong with Debugging in Eclipse on Android
How to Change Textcolor in Alertdialog
How to Store a Boolean Value Using Sharedpreferences in Android