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.
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.
First sorry for my english.
I have a problem, and i can't find a solution, it sounds like:
i'm developping an app that's getting my location from gps and send it to a tcp server on pc , and store the data into a listview (for example). I have set a timer that send the location every 2 seconds. Everything works fine even if i connect two clients to server, until the phones gets locked .. then my server receive ugly string ..it seems like the sent-strings it straddles (the string contains parts of data from bought clients, parts are concatenated) .. but when i unlock the phones the server receive normal strings again..
I want to know how to make my app run in the same parameters when lock screen occurs .. Any ideas?
If you are doing this inside an activity or a fragment you are probably having an issue with the lifecycle of your app. If you want to understand the lifecycle, read this documentation article: http://developer.android.com/training/basics/activity-lifecycle/index.html
Doing nothing on your onPause method won't prevent your activity from sleeping, Android can kill your activity anytime.
The proper way to do this would be inside a Service, a service is a special component on Android that is executed independently of what the user is doing or not doing, and in this case, you could create a service that holds a wake lock in order to prevent it from sleeping for the couple of seconds you need to send your data.
An easier solution would be to use something like this Location polling library and suit it to your needs.
When the screen locks your activity is either paused on stopped and it is important you handle these methods so that any interuptions are handled elegantly and without error. Or so the app will continue to run in the background.
If you read up about the activity lifecycle.
During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity to pause. For example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially visible but currently not the activity in focus, it remains paused.
However, once the activity is fully-obstructed and not visible, it stops (which is discussed in the next lesson).
Activity Lifecycle from android.com
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).
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.
i am working on an application that send and get data from internet each 5 min
if i press home key and my app goes to background... it will still continue sending/getting data from internet? or i have to do something special?
thanks
"If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state."
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
No. Activities shouldn't be depended on to process tasks in the background.
The following link illustrates the fundamentals of different Android components and what they do, i.e. the "parts" of an app.
You should be using a Service for background processing.
From what you're saying, I'd suggest an IntentService fired by an Alarm.
Application Fundamentals