Shared prefs slow - android

After some days of using shared prefs for my app , I have notified it became slower after each operation and make my app slower .
It only get prefs like :
getBooelan
and for seting password :
Editor edit = prefs.GetEditor();
edit.putboolean("reff", "value");
edit.commit();
where is the problem ? what should I do ?
thanks

This is how I use my SharedPreferenceEditor. I am not sure if it would solve your problem or not but have a go at this code and see if this works for you.
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("reff","value");
editor.commit();

Related

Android - App will not read sharedpreferences from Library correctly

My app was working great until I got it ready for deployment. I have a portion of my app that checks to see if a checkmark is checked in the preferences. Well since I added as a library and am running through another application (created a free version of the app and trying to keep my code as a library) it always returns false.
SharedPreferences appPrefs = context.getSharedPreferences("com.company.widget_preferences", Context.MODE_PRIVATE);
boolean blNotifications;
blNotifications = appPrefs.getBoolean("notifications_new_message", false);
if (blNotifications)
{
//always returns false
}
Thanks for your help.
Figured it out guys. For some reason using SharedPreferences appPrefs = context.getSharedPreferences worked in dev, but as soon as I made it into a Library it didn't like it anymore. The correct way to call a default sharepreference is the following.
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String value = pref.getString("name_of_my_pref", "default_value");
Thanks again.

Sharedpreferences issue with samsung galaxy S2(4.0.3)

I have developed on Sip based application for making and receiving a call. And chat feature is also included in this application.
My application runs fine in on Samsung Galaxy y and Sony Ericsson's ICS version. but while using the application on Samsung Galaxy S2 the user is not get registered.its gives me wrong password Error on Asterisk.
So that means its unable get value from sharedpreferences or its doesn't stores the value on the device.
Yeah, it is a massive pain but as far as I can tell you can't use SharedPreferences on Samsung Galaxy S1/S2 phones. No explanation :( Massive pain
Error creating SharedPreferences - couldn't create directory for SharedPreferences file
Don't know how apps that do appear to save get round it - maybe they use a database.
How I have done it:
Creation:
Editor editor = getSharedPreferences(Constants.SaveDataName, MODE_PRIVATE).edit();
editor.putString(Constants.SaveDataName, xmlString);
editor.commit();
Loading:
getSharedPreferences(Constants.SaveDataName, MODE_PRIVATE).getString(Constants.SaveDataName, "");
If relevant: this is in a service.
#juned, now that I look at the code I see nothing special. Anyhow here are some parts of the code that had to do with sharedPref.
setting SharedPref
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("longitude", (float)location.getLongitude());
editor.putFloat("latitude", (float)location.getLatitude());
editor.commit();
and fetching
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
GeoPoint userLocationPoint = new GeoPoint((int) (sharedPreferences.getFloat("latitude", (float) 0.0) * 1E6),
(int) (sharedPreferences.getFloat("longitude", (float) 0.0) * 1E6));

Android getDefaultSharedPreferences

My code is:
final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean hasBeenShown = prefs.getBoolean(eulaKey, false);
Always returns different values depending on os version. Tested in 2.2, 2.3.4, 3.2, 4.0.3 - returns correct value. But for device Zte blade with 2.3.7 with CianogenMod 7.1 - result is always false. I suppose default value for getBoolean.
Here is code writing boolean:
final String eulaKey = "mykey";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();
Does anybody have any idea?
Update:
Comparing my current code with my previous version of code - there is no difference in code.
Only difference is in manifest: code works Ok with minVersion=8 and targetVersion=8
Now I'm compiling with minversion=8 and target=13 /because of Admob/.
Maybe some APIs changed, but I found nothing on this.
SOLUTION:
-Starting app from shortcut and from menu gives me different DefaultSharedPreferences. After removing DefaultSharedPreferences from my code - it works perfect. I can't just say: people don't make shortcuts, so I had to change code.
Try it this way:
final String eulaKey = "mykey";
Context mContext = getApplicationContext();
mPrefs = mContext.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(eulaKey, true);
editor.commit();
in which case you can specify your own preferences file name (myAppPrefs) and can control access persmission to it. Other operating modes include:
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
MODE_MULTI_PROCESS
If you've upgraded to targeting API 30 drop this in your gradle dependencies:
implementation 'androidx.preference:preference-ktx:1.0.0'//For Kotlin Projects
implementation 'androidx.preference:preference:1.1.1'//For Java Projects
After re-synching Gradle change all of your imports from
import android.preference.PreferenceManager
To
import androidx.preference.PreferenceManager

Give a hint to user at the time of installation time only

I am doing a project in which,i have to give the user a message at the time of installation only.After that the message should be hidden.Now whenever i run the program the message will be displayed.I want to avoid it. I want to do it programatically in android.Can anyone help to resolve this issue?
Thanks in Advance
You can save a flag into the app database or userPreferences and after displaying the message you can set the flag to false or 0 . Anytime the user starts the app you check that flag and see if you have to display it again or not...
Use SharedPreferences.
SharedPreferences settings = getSharedPreferences("MyPref", MODE_PRIVATE);
Editor edt = settings.edit();
edt.putBoolean("Accepted", true);
edt.commit();
boolean isTOSAccepted = settings.getBoolean("Accepted", false);

using getPreferece() to set a first time run flag

I have this code that should read an unset preference on the first run:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences settings = getPreferences(MODE_PRIVATE);
firstTime = settings.getBoolean("firstTime", true);
Log.d("mything", "firstTime returns as: " + firstTime);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstTime", false);
editor.commit();
the variable "firstTime" is always returned as false.
I am uninstalling my app and loading it afresh.
Can someone explain?
Thanks in advance
Are you using Samsung Galaxy S with 2.2.1 firmware? There is known bug that shared preferences are not being removed if application is uninstalled. For example see comments here
Hmm strange. I run your code and work as expected. The first time you run it is logs
05-10 14:53:59.390: DEBUG/mything(4895): firstTime returns as: true
and if you run it again it always logs
05-10 14:55:25.780: DEBUG/mything(4895): firstTime returns as: false
Are you sure you are not missing something in the log ?

Categories

Resources