How can a user destroy a service outside of the app. I made an app that the user can never "Exit" because the main activity disables the "back" button, but now my service notification can't be removed (there will eventually be conditions on when it is shown, but not right now)! I know that android manages the memory when the app is in the background like that, but if I really wanted to close that service, how would this be done?
You can use the Bindservice.The Service is stoped as the Bindservice.
Related
My Android application exists of a main activity in which the user has the ability to start several other activities. In order to have a permanently working keyword recognition (speech recognition), I use an IntentServicethat is started in the onCreate() of the main activity. The service needs to be working as long as any activity of the app is in the foreground.
However, this service uses the microphone all the time, so it's desirable to stop the service once the user returns to his/her home screen. I know the basics about activity lifecycles, but my question is: what is the best way to stop the IntentService when the user returns to the home screen from any activity?
To restart the service when the app is re-opened, I was thinking of a superclass that all activities inherit from. In the superclass I would set a static boolean serviceStopped that is set to true when the user goes to his/her home screen (and the service is stopped). In the onResume() method, this boolean will be checked and the service will be restarted if needed.
As I know there is no way to detect if the user leave your application and is in the home screen.
I think there is no a simple way to detect if your application is open/closed; however I found this How to get the list of running applications? and I think it could help you.
You could also try to start a Service in the background and check when any of your activities start or is destroyed.
The main activity of my app takes the current location and send it to server via httppost using asynk. My problem is that when the user push home button and app goes to background after a long time period the system kill my app as it should. onDestroy is called and my app unregistering the locationlistener and also closes the Activity.I tried saveinstancestate method but it doesn't help me.So is any trick to make my activity lives longer in background?
There's no way to keep your "Activity" alive but you can (and have to) implement an Android Service: http://developer.android.com/reference/android/app/Service.html. Just move your background functionality there.
How do I keep a service running, when all activities of an application are viewable. and close the service only when I leave the app?
Is there a way to start a service in one activity of an app and stop that same service in another activity of that same application?
I don`t want that service to continue running when the user leaves the application.
There is really no concept of 'application' and leaving it. What happens if open a link from one of your activities, launch the browser to view it, then come back via the back button? Did you really leave the app?
What does your service do? Do activities bind to it? If so, it will be automatically shut down after the last client unbinds. If not, it should shut itself when it has finished doing it's work (Cf. IntentService). If it doesn't fit either of those patterns, maybe you don't need a service at all, just some background thread(s)?
Edit (based on comments below):
For a service running a media player, the usual way is to have an ongoing notification for the service that lets the user bring up an activity to control the service. Or have buttons on the notification in JB to achieve something similar. Additionally, if you make the service a foreground one, that will give it higher priority and it is less likely to be killed if resources are low.
I'm trying to get the main activity for my app to finish (not remain an active application in background) while keeping the process alive for a service.
If the user presses the home button and leaves my app or another activity is launched from outside of my app, I want the activity to be closed and NOT listed as an active application.
It is important, however, that the process stays alive. I don't want my background service to die as well.
What's the best way to go about doing this?
You should not forcibly close the application as the system does well in handling this itself. Instead you should call finish() to signal that the app is done and can be disposed of(your service will continue running).
By default Services don't have a UI. Once started they run until they crash or are killed . The user can close your app and launch a new one and the Service will persist.
Activities on the other hand are only running when they are visible. When the user navigates to another activity the previous activity is paused, stopped, or killed.
A simple way to do what you've briefly described above would be to create an Activity that starts a Service. That way when use navigates away form your Activity the Service will keep running in the background.
If you want your activity to die completely whenever a new Activity comes into view simply put a call to finish() in the onPause() or onStop() methods for your activity (which ever is more appropriate for your app).
im currently developing an app which plays the steam audio using MediaPlayer class. And i'd declare its main (Player) activity as SingleTop. Also on button "Back" it does moveTaskToBack(true) which acts the same as button Home does. So it just stays and plays on background and if user wants to see the gui he just starts the app once again (which is less convenient) or he clicks the special app's notify. Exit provided via menu.
but what are the benefits of using service instead of activity in such case? Definitely it would be more complex to develop, i have to say. Even instantiating the GUI while "on background" will take much more time, i'm afraid.
From the Android Documentation:
Activities
An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.
Services
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.
also
Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.
The Android OS can destroy your Activity if it runs out of resources, but it won't destroy the service.
EDIT: you should use startForeground() to ensure your Service won't be killed in situations where the resources are low. From the docs:
Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.