stackoverflow community.
I have a problem that I've been trying to solve since a few days ago.
I have a service running in an Android application that receives and sends messages. For those messages, I have counters that save the ammount of sent and received sms and errors. For example, when a message is sent, the counter adds +1 and shows the total on screen (in a not focusable editview).
Another thing I do is bind my service, so I can switch it on or off according to my needs. Everything works fine when the application is running, but if it is finished my variables are lost, but I need to keep counting the messages somehow and show the values when the user opens the application again. How can I do this?
I've been using shared preferences variables to save these numbers and others configurations, but when the app is killed, those numbers can't keep increasing.
When the application is closed, can I save the counters values somewhere? Can I access to the shared preferences from the service with the application closed? What if I use a content provider? Can I access to it from the running service even if the app is closed?
Regards.
There is no problem accessing shared preferences from a service. So, just save your variables on your services onDestroy() method and reload them onCreate();
This should work to get the preferences:
Context ctx = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
Here's a great answer: How do I get the SharedPreferences from a PreferenceActivity in Android?
Related
I'm saving some data into sharedpreferences and my problem is the data remains even when the App is closed, and even if the pda is reset
Another question about the lifecycle, my App should keep working in background and it does if i press the "menĂº" button of my pda, choose other apps in the meanwhile, etc... But if I press the "back" button, it executes the ondestroy method and the App doesn't keep working.
Thanks!
sharedpreferences are designed to do just that. persist data between app executions..
-- http://developer.android.com/guide/topics/data/data-storage.html#pref
if you want your app to remain active in the background you need to have it run as a service.
-- https://developer.android.com/training/run-background-service/create-service.html
The sharedpreferences are intented to stay when the app is closed. It's just an xml-file storing your data. If you don't need some of the values you either may reset them in the onCreate of your Base-Activity or define a global class that extends Application and store your data there in global variables.
I tried to find an explanation about this issue but I couldn't find anything.
I have a remote service that is working in the background. This service reads some preferences from a SharedPreference.
The problem I have happens when I change a preference from a PreferenceActivity (the activity and the service belong to the same application and the same package). I change the preference and it is saved correctly, but as soon as the service reads that preference (the service never modifies the preferences, it only modifies some internal preferences that cannot be modified from the PreferenceActivity) the preference is 'resetted' and the service gets the resetted value.
This is happening with CheckBoxPreference. I don't know if this would happen with other kind of preferences as I don't have any of them.
After I change the preference I should restart the service? Or I need to 'refresh' the preferences in the service? Maybe this is a problem related with using HoloEverywhere?
To get the SharedPreference object I use the following code:
PreferenceManager.wrap(context, getSharedPreferencesName(context), Context.MODE_MULTI_PROCESS);
This code is specific for HoloEverywhere and it equals this:
context.getSharedPreferences(getSharedPreferencesName(context), Context.MODE_MULTI_PROCESS);
'getSharedPreferencesName()' just returns a string composed by "package.name_preferences".
Thank you.
I have a remote service that is working in the background.
Why did you make your service be remote?
I change the preference and it is saved correctly, but as soon as the service reads that preference (the service never modifies the preferences, it only modifies some internal preferences that cannot be modified from the PreferenceActivity) the preference is 'resetted' and the service gets the resetted value.
That's because you made your service remote. Simply remove the android:process attribute from your manifest, to have all your components run in the same process, and this problem will go away. Along the way, you will make the user happier, because you won't be consuming as much RAM and battery.
After I change the preference I should restart the service? Or I need to 'refresh' the preferences in the service?
If you truly have a legitimate reason for having a remote service -- and IMHO there's a greater chance that I will spontaneously regrow my hair -- you will need to restart the service's process, AFAIK. SharedPreferences are cached per process, and I don't know of a way to force Android to reload SharedPreferences from disk except by restarting the process.
Or, you could not have a remote service. The choice is yours.
I am new in Android and is developing an app which runs in background as service to collect user activity. till now my app is able to get information about Time_Start, Time_End and Name of other app used by user.
I want to improve my app to be able to count how many interactions(like user tap, touch,...) user make while using other app. Can any one give some advices about this issue? Just the way to do and I'll do all details by myself.
Thanks!
The best way to do this is to implement Listeners for the types of user interactions you would like to count. Have a global variable in your activity. Every time the user interacts, add one to it.
If you want to keep count across the entire app, you could save a variable in SharedPreferences, and at the end of each activity, add your activity's global variable count to the value you have store in shared preferences. Or, you could just edit the value you have stored in SharedPreferences right off the bat on every user interaction. Just remember to reset the value of the SharedPreferences when you first start the application, as values stored in SharedPreferences are stored even after the app is closed unless you specifically remove them, or clear the SharedPreferences.
Another way you could keep count is by using a global static variable that is accessible from all other activities. This way you would be able to increment it from other activities. This is generally considered bad practices though, so I advise against this.
The documentation for SharedPreferences is here:
http://developer.android.com/reference/android/content/SharedPreferences.html
The documentation for implementing input event Listeners is here:
http://developer.android.com/guide/topics/ui/ui-events.html
Hope this helped!
My app uses SharedPreferences (with the help of a PreferenceActivity) to store all settings. But sometimes all settings are deleted and all values are set back to default. Mostly on Android 3.x tabs.
On normal smartphones there are no problems.
I have observed that all settings are deleted when the application process is killed by Android.
I noticed this because the notification icon is no longer displayed.
I also wonder why the process is killed on tablets and not on smartphones. On my smartphone the notification icon never disappears.
My app also has a service, the main process and the service both read and write settings to the SharedPreferences.
I've sometimes noticed that all settings are cleared once the service writes to the SharedPreferences. But it does not happen every time.
Any ideas?
I realize that the SharedPreferences should not be used by different processes, but it works on all smartphones without problems!
I found out the reason why the settings disappear:
If two processes write data to SharedPreferences, it can happen that all SharedPreferences are reset to default values.
In my case all settings are cleared when the service write the first data to SharedPreferences.
Now, the service provides only read access to the SharedPreferences. The settings that are accessed by both processes writing are stored in a database.
SharedPreferences usually work great and as you mentioned without any problem on smartphones. It should also work on tablets. However it would be best to use sqlite or backing your shared prefs on a server for each users id data loss is a problem.
I have had the same issue.
All values from shared preferences become default if one value occasionally written with key = null. This is possible when you creating new constants using Eclipse, which inits them with null by default, and then you forgot to go to the beginning of the class and change constant to some meaningful value.
If you continue using the app, read operation of shared preferences will return valid values.
But when context is changed(for example, when activity killed by android and recreated) all values from shared preferences will return default values(zeroes).
So check your shared preferences's keys!
I hope this answer will help someone.
I would like to know if it is possible to for some application other my own to change a preference value of my application. Also are the preferences been kept by android when the phone is turned off?
Is it safe to store some data(flags) on sharedpreferences in order to notify an activity for something?
When user clears the application data what exactly is erased, shared preferences data?
Is it preferred to use an internal private file to store secure data? Such as passwords?
Also I would like to be able to show a dialog when I detect a certain behavior, for this I have a monitoring service that has to notify the main activity about that. currently this is done through a callback method but I would like to maintain that state even if the application is killed or the phone reboots.
So I thought of setting a sharedpreference value (flag) and then on the oncreate method check if that flag exists. Also should I also check on the resume method?
Preferences (including SharedPreferences) are stored in files under your application's private data directory. No other application can read or write there, unless the phone has been rooted. This internal storage is flash-based and survives the phone being turned off... not much would work if it didn't. :)
As a general security principle you should never store a password. Secure systems store and compare password hashes, not the passwords themselves.
It's fine to store application state in preference data... personally I'd read it in onCreate() and thereafter write the value back to preferences either at the point it changes or in onPause().