Sharedpreferences issue with samsung galaxy S2(4.0.3) - android

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));

Related

Android SharedPreferences not working

I am tring to solve a problem very strange
I have an app that was working great in all of my testing devices, after a factory reset on Nexus 5, When it try to get an int from the SharedPreferences with code
LEVEL_MAXIMO_ALCANZADO = sp.getInt(SP_NIVEL_JUEGO_MAXIMO, 1);
It give me an String (but only in Nexus 5, in other devices it give me the int but in my nexus 5 is causing an exception java.lang.String cannot be cast to java.lang.Integer )
If I get the value of the String with the code
String prueba = sp.getString(SP_NIVEL_JUEGO_MAXIMO, "1");
I get a value of 5t+SNTiVFHA=
I dont have any idea why this only pass in nexus 5 after factory reset
If someone can give a tip to follow it will be awesome, I am lost at this point
Code - ( ctx is a Context Object)
public static SharedPreferences sp;
public static SharedPreferences.Editor editor;
if(sp == null) sp = ctx.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
if(editor == null) editor = sp.edit();
editor.putInt(SP_DINERO, DINERO);
editor.commit();
Additional info
I also tryed to unistall app and reboot device
I used ObscuredSharedPreferences a class which encrypt them and in that class it give me pad block corrupted exception, then I swap to SharedPreferences but same error
I made the factory reset due to an issue where my phonne said that it was full when only 5gb was used
so maybe is a memory issue on the device?
I just made a Reset ( Wipe cache and wipe data, and factory reset from bootloader) and now It is working again
I think that the memory got corrupted and It was failing.

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.

Shared prefs slow

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();

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

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