I am developing an application in android studio, and I have to save the location of the device, I did a "locationService" class that extends service, so the location is segmented on my server and the service would be in the background.
I have read the android documentation and there are two types of service (service and linked service), I use the linked service because I need to show the location data in an activity.
The problem is when I close the application, the service dies and does not record the change of location.
How can I prevent the linked service from dying. Thank you
I assume you mean a "bound" Service, not a "linked" Service. A bound Service will stop running when the last client disconnects from it. To prevent this, you need to call startService() and make sure that you return START_STICKY from onStartCommand() in your Service.
You don't need to use a "bound" Service in order to pass data from the Service to an Activity. Another way is to have your Service broadcast the data (by adding the data as an "extra" to an Intent and calling sendBroadcast() with the Intent). Your Activity can then set up a BroadcastReceiver to listen for any data braodcast by your Service.
Related
I am developing an Android application where I start a Service via an Intent. The service opens a connection and reports its success to an Activity. Afterwars the user shall be able to start a data transmission via Button click. This is also done by the service.
Is it the best way to send another Intent to my service? And does a service always exist only once or is another instance created of it?
No matter how many times you start the service, the service will be started only once and at any time only one instance of the service is created. If you start the service multiple times using startService() method then onStartCommand() method will be called multiple times.
The best way to communicate the service from the activity is to bind the service(bounded service).
I am trying to make 2 applications which will interact with each other using AIDL.
Application 1: Will be a service.
Application 2: Will be Activity(with a button) which will show some data which will be fetched by Application 1 service.
Now to start this interaction I know we can make one AIDL file in both applications and when user presses the button in application 2 we can involve the function of service from application one. That is lets say application 2 requests the current time then application 1(Service) will fetch the current time and return it to application 2.
My doubt is that I want to interact the other way round. I want to inform the activity from service when some particular digit occurs in time(or some other event). I am not sure how to proceed with this way of communication ie from Service to Activity.
Some pointers will be really helpful.
You know you can send message from Activity to service, Reference Bound Service
By Following above tutorial You should Consider Sending Handler from Activity to Service using Messenger Class in Intent.
So now Service and Activity can send message to each other which will execute corresponding Handlers
Use Broadcast receiver in Activity and let Service broadcast messages (with same Intent as used by Broadcast receiver in Activity). These messages which are broadcasted from Service will be received by broadcast receiver in the Activity.
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.
I read some similar questions (for example at this link), but the problem I'm asking is a bit different. In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
Suppose we have a package that contains the MainService service and MainServiceActivity activity. In the file "AndroidManifest.xml" this activity is declared with action MAIN and category LAUNCHER. This activity is used to configure the service via the SharedPreferences and start the service by invoking startService method. In other words, typically the user launches the MainServiceActivity and configures/starts the MainService.
Now consider another activity (Let's call it SecondActivity) that is part of another package. Depending on the configuration, the service starts this activity using the startActivity method, so this other activity is running on a separate process than the MainService. As soon as the activity is running, it should inform the service.
At this point, a communication request/reply begins between the MainService and the SecondActivity: the service sends a request and the activity sends a reply.
The communication via messaging might fit, but the MainService is started through startService method, so the bindService method can not be invoked by activities that want to bind to the service.
Then I had an idea that makes use of an additional service (Let's call it UtilityService), which is part of the same package of MainService: the UtilityService could be started using the bindService method. As a consequence:
as soon as the MainService is running, it might perform the bind to the UtilityService;
when the MainService launches an external activity (for example the above SecondActivity), this activity bind to the UtilityService.
In this way, both the MainService and the SecondActivity are connected to the UtilityService, where the latter acts as an intermediary for communication.
Are there alternatives to this idea?
In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
You can both bind and start a service, if you wish. It's a bit unusual, but it can be done.
Are there alternatives to this idea?
Binding has nothing in particular to do with services being able to communicate with activities. Using some sort of callback or listener object via binding is a possibility, but it is far from the only one.
You can:
Have the service send a broadcast Intent, to be picked up by the activity
Have the activity send a PendingIntent (e.g., via createPendingResult()) to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity (or wherever the activity wants it to go, such as a broadcast)
Have the activity pass a Messenger tied to its Handler to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity
All of those work perfectly well between processes, as well as within a process.
You can use Android Interface Definition Language (AIDL).
You can find an easy to use guide here
So i have a class that listens to incoming calls, i want to make a service to all my app to receive calls even when i'm not in the application's UI.
How to trigger this class(broadcast receiver). I used "sendBroadcast" and have a FC.
sendBroadcast(new Intent(context, IncomingCallReceiver.class));
Thank you for your help.
In your case I would use the following approach:
Create Service and start it from your Activity (you mentioned that you have several applications, so starting first of them may also start the Service).
Make sure that Service does not work forever, so stop the service when you do not need it any more (last of your applications is finished). Service may terminate self even without Activity by calling stopSelf(). Please note that also System may terminate your Service and prevent it from working forever.
Make private class within Service that extends BroadcastReceiver and register it for the Intents you want to monitor using Service function registerReceiver().
Once you receive wanted Intent, you can call some Service function from within BroadcastReceiver onReceive(). For example, you may sendBroadcast() using some custom Intent that is recognized by your applications.
When Service is stopped make sure that you un-register the BroadcastReceiver extension using Service function unregisterReceiver().
UPDATE:
Service building guidelines
SDK example that illustrates BroadcastReceiver extension usage within Activity. All important related to register/unregister is the same if you do that within Service: android-sdk-windows\samples\android-8\Home\src\com\example\android\home\Home.java
Start Activity from Service
Handle the case of Activity already running in the background