Service stops automatically in Android - android

I am working on a Android Project that control Android Device through Speech. For that I am using speech Recognition Service of Google.
Here I have created a service for handling Speech recognition.
But what is going is that after some time this service gets stopped automatically and my device stops responding to the speech of the user.
Then little seconds later, it starts responding again.
So my question here is that how can I stop my service from being destroyed automatically and if possible is there any trick to call my broadcast receiver whenever my service stops?
So that I can create my Broadcast receiver for handling service destruction.
And how can I also stop my device from ringing beep tune when Speak recognition Service going on?

You have to start your Service with startForeground(). You have to pass it a Notification that will be placed in the notification bar permanently. This make your Service unstoppable from the system.
As always refer to android documentation for more details.
http://developer.android.com/guide/components/services.html#Foreground

Related

Best way to create a service that doesn't die.(like WhatsApp or Facebook)

I have tried till now
- start sticky
- alarm to restart service
- on task removed start the service
- use job service (has latency and executes task slow)
Is there a proper method to make an infallible background service like the popular apps?
Creating a background service that "does not die" is not possible in android.
You can create a service and take certain measures to have it running as much as possible, but the OS will kill it at times and your service will not be running until the OS decides to restart it (in case it is a sticky service).
Things you can do:
Make the service sticky, so that it will be restarted by the OS after the OS kills it. It is impossible to predict when it will be restarted. It can be almost instant, it can take seconds, minutes, hours, or more.
Start the service when you open the app.
Start the service when the app is upgraded, using the MY_PACKAGE_REPLACED broadcast.
Start the service when the device is (re)booted, using the BOOT_COMPLETED and REBOOT broadcasts.
Override onTaskRemoved in the service, to schedule a restart of the service in case the user swipes away your app from the list of recent apps.
Use FCM to periodically send messages to the app with an instruction to start the service in case it is not running anymore.
You can never have a 100% uptime, but this will get you close.
A background service, if not visible by the user will always be killed earlier or later by the Android system. It is the way memory management work.
If you really need to make a service continously run, you need to show a permanent notification to the user (like when you are using a radio app or a music player app).
What whatsapp and facebook probably do is to wake up the app remotely with any sort of messaging such as Firebase Cloud Messaging (ex-Google Cloud Messaging) or using Broadcast Receiver on certains events... But it surely isn't en ever going on service.
Read this part of Android documentation to better understand this:
Service Process Lifecycle.
As you can see, to give priority to your service process you will need to start it in foreground and pass it an Ongoing Notification using startForeground(int id, Notification notification).
Use setOngoing(true) in your NotificationBuilder to set a Notification as an Ongoing one: setOngoing(boolean b) doc.
Finally you usually want to add action in your Ongoing Notification (such as Player controls or the possibility to close the notification and hence your service when memory will be collected)
Here a full sample code:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.notification_title))
.setContentText(getString(R.string.notification_text))
.setSmallIcon(R.drawable.ic_small_icon)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentIntent(pendingIntent)
.setOngoing(true)
//.addAction(android.R.drawable.close_service,"Close", closeServiceIntent)
.build();
startForeground(Constants.NOTIFICATION_ID, notification);
It's not a good idea, check out this article. Using a service in this case is not a good approach.

Android implementation service and launch in background also if the application is died

I am trying to find how to enable the application to perform a background task.
I use the example seen here "http://saigeethamn.blogspot.fr/2009/09/android-developer-tutorial-part-9.html" and I do not know if that is how I have proceed to execute a script permanently?
Also how to get it reactivated when starting the mobile?
So how should we do and if we can so that the service works even if you force stopping the application?
A big thank you for your help
No service can work if the user force stops an application. Force stopping puts the entire app into a state where no services, receivers, or activities can be run unless the user explicitly does so.
If you want to start a service when the phone starts, you need a broadcast receiver for the BOOT_COMPLETE broadcast.

Activity Recognition when the app is not running?

In the demo video provided by google, it is said that you don't need to keep a service running to receive updates on activity recognition. But all code examples I could find show that you need to register ActivityRecognition in MainActivity for it to work.
How can I make Activity Recognition independent of application lifecycle?
For e.g. If a user is jogging, show him a notification to record his walk?
Can it work similar to a broadcast receiver which is called as soon a user is connected to wifi?
As an example you could subscribe to ActivityRecognition events when your phone booted successfully. You only need to do it once. After that the PendingIntent is called in the specified interval and you do not need to have a service running all the time in the background.

What happens to background service when the app is updated in android?

Let's say I started a repeating background service that was stated on first app launch and on boot. What happens when I provide an update of the app. Will that background service be killed?
Will user have to open the app again to register the background service again or app will get some callback on update?
Edit-1: As one of the answer suggest if app has to be relaunched again to start the service then how does alarm application works fine after the update without relaunching(I believe it usages background service to start the alarm)?
Will that background service be killed?
It will be killed.
Will user have to open the app again to register the background service again or app will get some callback on update?
It depends. Basically it'd require user activity as app is not relaunched automatically after update. But if you target API 12 or higher (which you should nowadays) you can try to use ACTION_MY_PACKAGE_REPLACED broadcast. As per doc:
Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application
that was replaced. It does not contain any additional data; to receive
it, just use an intent filter for this action.
so you can do you stuff either in BroadcastReceiver trigger something once you receive this broadcast.
The service will be killed and needs to started again.
A Service doesnt run on a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
So when the application is updated, the application is sent to the stopped state.
You can test this.
From google play store initiate a update for the app (which has a service E.g Whatsapp).
Open the app and wait for it to complete. It stops. you can check the internal running processes. Connect the phone to DDMS. Check the processes.

Android: Detecting a Bluetooth device in a background service (Wearable)

I want to create a service for my wearable that runs in background that will detect a Bluetooth device. I have written code that is able to detect the Bluetooth device but it uses Activity - onCreate() method to start scanning.
I read many articles on the internet about writing services that runs in background but in every example the service is started from the Activity. I want my service to be running without starting from any Activity. Is this possible? I mean when I install my app is it possible for the service to start running automatically?
Seems like there is no way of starting a service without starting the Activity. I got the answer from there How to start android service on installation

Categories

Resources