Will auto start service also launch the Application? - android

In my App, I have a remote service will auto start once receive "ACTION_BOOT_COMPLETED" broadcast. In this App I also have a class which extend "Application" class as the App entry point.
My question is when the service is launched by the Andriod OS when the device finish reboot, will the method "onCreate()" in the Application class get called first??
What if the service is local, will it make any difference?

I would think that the onCreate() method WILL be called for the application to start. Since it is the natural flow of the activity life cycle.

Related

How to Start android service without activity

I created an application that contain a Service that start from button click of Activity.
But I want to create a new application that doing the same as the last one - but don't have any GUI interface => that mean without any Activity.
I want to make the application to start on device boot and never stop (until the device is shutdown) .
How to do it ?
Is there any way ?
How to do it ?
By using Context.startService() or Context.bindService() methods.
where Context might be Activity, Application, argument of the onReceive() method of BroadcastReceiver etc.

Android Service that works only when app is closed

As the title, I need that an Android Service - that periodically calls a web service - does the job only when the app is closed. When the app is running (don't matter if in background or foreground) the Service must be stopped.
How I can achieve this?
Thank you.
If you have just one Activity as an entry point for your app it would be very easy. This Activity will be the destroyed at end when user leave your app, so overriding onDestroy() and launching Service from there will do the job. When the same Activity starts you can request stopping your Service inside onCreate() of the Activity.

A Local Service implementation closely related to Application Life Cycle

In my App. I have extended Application class, say MyApp and I have few activities say A,B,C,D where A is the default Activity or root Activity. I want to start a Service (say MyService) inside OnCreate of the MyApp and stop it when User logs out from my Application,
However I need a strong way to communicate between my Activities (A,B,C,D), MyApp and MyService. Now Application is launched second time from the Memory. OnCreate of MyApp will not be called so I would like to start the Service from OnClick of the SignIn button which is inside activity A. Now I have a number of questions :-
I have tried both startService and bindService. Problem with startService is I don't get back the Service reference for future use. If I use bindService will that make my Service visible through out the Application life cycle till I explicitly stop it. Which way is applicable in my case ?
What is the best way to implement communication between Service and MyApp / Activities and vice a verse ? By Communication I mean two type of communication, 1. that needs UI thread i.e. start some activity, show some dialog etc. 2. Something like myService.doSomehing().
The Service has a thread which does network communication which should not be closed during the life time of the Application. START_STICKY will work for me or I need to make extra arrangements for that.
This app is supposed to run for GingerBread+ devices...
Bind the service inside your extended app oncreate and create a public method
(getBinding) which returns that binding object ... In your root/default activity 'A' oncreate via getBinding method on casted getApplication object retrieve .. unbind when all activities close (keep counter with each getBinding method)
check this Cleanly binding/unbinding to a Service in an Application
so for 1) Bind service
2) the binding obj
3) START STICKY would be enough

android access running service without startService

I have a service which I know is already running, how does my activity communicate with it without restarting the service.
to elaborate, I have a widget which starts a service, upon click an activity gets loaded, in that scenario the service should still be running.
How do I do something along the lines of:
conditionally checking if the service is alive
accessing methods in the service
Basically, the main thing I do not want to do is run startService(new Intent(...)) within my activity. I don't want to run onStart again within my service.
Ideally I can just add some methods within my service class, and call those within my activity, like I would call any other public method in the project.
I want to start some new threads within my service, and I don't want to make a second service class if I don't have to.
Thanks for the insight
The documentation on startService() might help:
Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
...
Returns
If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.
So if your Service is already running, startService() won't start a new version.

Autostarting Android service upon Application startup

Is there a possibility to autostart Service upon starting Application ? The problem is that I am developing separate UI component that depends on service. Ideally this service should be started as soon as hosted application starts. Could this be done via manifest only or could it be done at all ? I know I can start service from my UI component's code, but I want to start service immediatelly after starting main application even in case if my UI component hasn`t been created yet.
Thanks in advance.
Create a MyApp class which extends Application, and make sure it's declared in your manifest. MyApp's onCreate() is then a good place to start the service if you need to.
See the documentation for the Application class.
You want to use the PERMISSION that notifies you of boot completion. This will allow you to know that the device has started and take action, such as start your service.
RECEIVE_BOOT_COMPLETED Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.

Categories

Resources