I have an Android app which listens to a port and updates the screen based on what it hears. This works flawlessly. The problem happens when I leave the app running for say 20-30 mins. During this time the screen would have gone blank for power saving (display). After this point, the app either becomes unresponsive or totally hangs. I doubt if it's memory leaks. Can't figure out how to go about debugging this issue. Any pointers? TIA
May be there are variables that are lost, try to save and restore the state of your Activity using the methods onSaveInstanceState() and onRestoreInstanceState().
check https://stackoverflow.com/a/151940/1434631
I was building a widget for android and same thing was happening to me. I believe the problem is that process is destroyed and started again from time to time, and then all your variables lose value. You should use content provider to store them and then obtain them when lost.
Related
I'm developing an application in cordova/phonegap and it is about tracking a person his gps location and therefore the app needs to be active for a long time
I can keep the application active as long as it is in memory but when you have opened so many applications that android needs more ram space it still kills the application
I know services exist in android but I just cant tell how to use them in combination with a phonegap app
Is there a way to keep the activity of my app in front of the activity stack so it doesnt get killed or can i write a native service that restarts my application when it gets killed (with thesame state as it got when it got killed)
More detailed description of the problem
ok as requested a more detailed description of the problem
I have a cordova application (html + javascripts)
in a the application i use navigator.watchposition() which gets called when the gps location changes
i then do some logic and store the data in a websql database (on the device itself), as far as i know this database is only accesible from within a webview
now the point of this application is that it you start it at the morning and stop it in the evening (for example lets say say active for 10 hours from start to finish) you dont need to see the application, and the screen doesnt need to be on
I got the stay active part working for when android never ran out of memory
So what I need to do to get to my problem is start the application -> press the home button -> start about 10 different application (to simulate normal use of a phone), the number 10 can vary on different phones with different ram sizes -> after opening enough other applications my phone will run out of ram memory so then android kills some apps and eventually will kill my app.
This is not good for our application at it tracks time at a location and needs to do this fairly accurately
-------
anyway thanks in advance for your help
You should not try to do that. It's against the principles of Android. Instead you should, as you already suggested yourself, use a service to track the GPS location.
I have met the same problem.
In android, activity life cycle can only changed by system, when the activity not on the top, it will easy to be killed by system, so next time you come into your app , it will start from onCreate function on main.java.
If you want make something work background , use service will more appropriate.
I recently began writing a phonegap android application and noticed that when the app is resumed from the background (so I deploy the app to an android tablet, press the home button and then reopen the app from the menu) it gives a timeout error (something to the effect of Error code =-6 The connection to the server was unsuccessful) and then crashes. From what I've tested this only seems to happen when the "Don't keep activities" option is checked in the developer options, when that option is not checked the app works as intended.
It's also worth noting that I recreated the default phonegap application, ran it and encountered the same issue.
Can anyone explain why this happens, or suggest a solution? Obviously I can get around this problem by simply leaving the Don't keep activities option unchecked, but I'm guessing the problem will persist on any android device that has this option checked, which just won't do.
I'm using phonegap 2.5.0 and testing on a device running Android 4.0.3,
Thanks,
Josh
"Don't keep activities" is a developer tool to simulate user activity that would be extremely hard to test for. I personally believe all apps should be tested a second time (at least run automated tests) with this setting turned on, and devs should turn it on / off during development.
Your issue (which I've just ran into on v2.7) comes from a silly implementation of a timeout feature. CordovaWebView.loadUrlIntoView creates and locks (wait()) a thread for 20 seconds (default value), after which time it checks a value to see if the url finished loading - if it hasn't finished, it shows an error message.
This thread exists outside of the lifetime of your activity, so if the activity stops running, the Webview can never finish loading the url, and when the thread wakes up, it does Bad Things trying to show the error.
The same crash could happen without using "Don't keep activities" by simply having the user leave the application and then the system reclaiming the activity's resources (because it is low on memory or something) within 20 seconds.
Using a Handler seems like a more appropriate way to handle this timeout, but without changing the source there are a couple of hacky work arounds.
Call System.exit(0) at the end of your Activity.onDestroy() - this is horrible, but if you only have the one activity and no services, it might be an option
Use reflection to change CordovaWebView.loadUrlTimeout - this is horrible, but it should work, this is the value that the thread checks to see if the url loaded (inc by 1).
I have an activity which is basically a tabbed activity for keeping a score a of cards game.
The problem is that, when I get phone call and I come back to my app, then the score is cleared!
It seems like activity is restarted a phone called is received. Why is this happening and how can I fix that?
Thank you
Android devices had limited memory, and can only run so many apps at once. Phone calls are one of the most taxing tasks on a mobile's hardware, as audio streams aren't exactly small on memory usage.
Due to this, I think your app's current instance is killed to free up RAM, and this results in a loss of scores for you.
To fix this, you could write the value of the scores to SharedPreferences in your Activity's onPause() and then retrieve them in onResume().
I am facing bugs in my application that only show up after the application has been running in the background for many hours. Some times "many hours" are 24 or 36. It is very difficult to try to fix those bugs if I have to wait that long. What are my options? Thank you.
I know of one way which unfortunately is dependent on Android version and provider ROM.
Settings->Developer Options->Do not keep activities.
Set this, then put your app in the background. Your activity will be destroyed immediately. Then resume your app. The bugs will show.
You might also want to review how and when to save instance state between onPause() and onResume()
a likely problem is that some of your variables are cleared, like global variables,due to memory management and when your application comes back through the lifecycle after onResume() it gets null exceptions.
What you can try to do is persist some of those variables in sharedpreferences during onPause() or do null checks in onResume, onStart and onCreate
Make use of Services if you need to run a process in Background for long
I want to know when the app is closed, because I need to erase a Database when the user shutdown the app, just in the moment when the user close the app is the right moment to erase the SQLite Database, how can I detect this?
This is a flawed design idea, which reflects a misunderstanding of the system - when the process overall dies, it's dead, meaning your code is no longer running.
You can do some tracking and have the last onDestory()'d activity do the cleanup as a courtesy, but don't assume that it will always actually happen (the method is not always called). If having a stale copy is a problem, clean it up on the next run.
That said, you can try using the ndk to provide a handler for process termination signals, but still I wouldn't count on it working in all cases. The limited potential to gain any sound functionality from this would probably not justify the effort unless you are already familiar with the concepts involved.
And do not for a minute mistake cleaning up for a security mechanism, as the file is there while your app is running, and would remain if your app terminated in an unexpected way.
Supposing you don't finish() your main activity, clearing your database inside the onDestroy() method of that activity might be the closest of what you want to accomplish. As has been pointed in the comments, refer to http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle.