android access running service without startService - android

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.

Related

Main activity to connect to existing service again

I have my main activity that creates a service, binds to it and start calling its method. Lets say this Activity is destroyed. The service keeps running (as it should be). Now when the activity starts again, how can I connect to the service again (and not start new one)?
What's the right way of doing it
Thanks
Android services are created as singletons, so the framework will always guarantee that only 1 instance of the service is ever running, even if you call StartService 10 times in a row.
Simply run StartService and then BindService each time. If the service is already started, the StartService will simply not do anything.

Android Periodic Background Services

I have a doubt that if i start a periodic service using alarm manager and start the service from the onCreate method of an activity. How can i prevent the service from triggering multiple times if that activity is launched again and again.
Assuming that you are creating a Normal Service (and not an IntentService), as per the Android Service documentation, when app invokes startService call, service will be instantiated and started (creating a process for it if needed).
Also, if it is running then it remains running.
So, to put it in simple terms,
Life cycle of "Started" service is independent of the life cycle of
Activity which has started this service. This is true irrespective
weather both are running in same process or different processes
So even though your Activity may be getting created multiple times, and if Service you created earlier is still running, then service object that already exists will be reused.
However, if there is a call to startService() from onCreate() of an Activity, this will invoke each time onStartCommand().
Hence, you need to ensure that you have a appropriate code/logic to handle multiple invocations of onStartCommand()
As far as I understood you must do something in either onCreate/onStart and onStop or onResume and onPaused. By do something I mean, in onCreate create what you need, alarm manager, etc then in onStart you can start the service and in onStop you stop the service or unBind from it, in case you want a foreground service. or in onResume or onStop.
Take a look here:
https://github.com/toaderandrei/live_tracking/blob/master/app/src/main/java/com/ant/track/activities/ServiceConnectActivity.java.
It is a tracking app that is based on MyTracks app from google.

Android Service : What happen at the end of Activity life?

Suppose that Activity A starts Service S and binds to Service S.
What will happen to S when A is destroyed?
How can I recreate another Activity that binds to S? The sample code in http://developer.android.com/guide/components/bound-services.html unbind the service in onStop(). I think if I open the app again, a new process is created for another instance of A and S. But I want the new activity to get data from the old service.
What will happen to S when A is destroyed?
If A is the only Activity bound to S and you didn't start the Service via startService(Intent), S will be destroyed. That's because a Service will be alive till the last bound Activity unbounds from the Service. This is documented here.
How can I recreate another Activity that binds to S?
If A is bound and you switch to Activity B via Intent, the Service will be destroyed and recreated when B binds to it.
If you want the Service to be alive even if no Activity is bound to it, you have to call the Servie with startService(Intent). Now it will be around if you explicitly stop it or system means it's time to destroy it. If you don't want this behavior, persist your data and access it at given time.
I think if I open the app again, a new process is created for another instance of A and S
The process remains the same till the process is killed from the system or if you kill the process, which is not recommanded.
Edit:
Only the bound service lifecycle depends on Activities. If you want a stand alone one use startService(). This way it's independent from Activities and runs in background as long as the process of the App is up or you explicitly stop the Service with stopService() / stopSelf(). You could even have a Service in a own App and use IPC to communicate between Apps. It's all a matter of the use case.
As you can see the configuration of a Service is very flexible and you have to decide which fits best for your App.
If You start service through startService() it will keep remaining after Activity finishes.
If You start service through bindService() it will live until last Activity unbounds from it.
Also if service is already started and you call startService() no new instance of service will be created, but in living service method onStartCommand() will be executed.
Almost same when you bind to living service, methon onBind() will be executed.

Activity and Service communication

My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.

How to create an android service that runs after the activity has ended

I have a android app that has an ongoing service for tracking a persons location. I want to be able to start up the service from within the activity and end it from within the activity. When the activity is running I need to be able to bind to the service and communicate to it via aidl. However I need the service to continue even when the activity has ended. I only want the service to end when the it is told to by the activity.
So far I have my activity and service and they can communicate through a aidl file. But at the moment the service ends when the activity does.
How do I get it to continue running when the activity has ended AND how do I rebind to it when the activity is restarted?
I have figured it out.
I was using the bindService method which created my service for me and bound to it (I need a binding), but if a service is created via a call to bindService the service only lives as long as the binding and so when calling unbindService, which I was doing when my activity was destroyed, the service itself also was destroyed.
However if you create your service with a call to createService and then call bindService you will bind to the already running service. Then when you come to unbind from it, it no longer destroys itself. The service will continue until you call stopService.
Then when you re-enter your activity you can check if the service is already running and if it is just call bindService other wise just call startService followed by bindService

Categories

Resources