Android SharedPreferences not working - android

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.

Related

firebase database in unity editor is not working

I am trying to access to my firebase database like this
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
It's work fine in my android device ,but it's not working in the editor.
I am getting this exception
WebSocket: ws_12 - WebSocketException during handshake
Disable the FirebaseDatabase.dll for Editor platform under
Assets/Firebase/Plugins/Mono/
Then enable the FirebaseDatabase.dll under
Assets/Firebase/Plugins/
I don't know how clean this solution, but it works for me
Firebase.Database.DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
A similar thing is happening with me, just reverse it. Reference is working fine in the editor but not on an android device. I checked for the access to the DLL it has been provided in editor and android both.

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

Android local variable get's lost when using camera intent

I'm dealing with a random problem which related to camera usage. Before I call camera intent - I generate UUID to store file with this name. I store this UUID in private variable like so:
private String requestedFileName;
When camera done - I'm processing this file, looks something like this:
public void onPictureTaken(int index)
{
//First of all - remember picture in database for reference.
FileData.InsertFile(mContext, UUID.fromString(requestedFileName));
//Reduce taken picture if needed, otherwise let it be original.
if (Preferences.getImageSize(mContext) > 0)
{
Imaging.scaleImageFile(mContext, requestedFileName, Preferences.getImageSize(mContext));
}
I see users report issue exception that boils down to requestedFileName == null when onPictureTaken called
Caused by: java.lang.NullPointerException
at java.util.UUID.fromString(UUID.java:210)
at com.idatt.views.FourImagesView.onPictureTaken(FourImagesView.java:151)
at com.idatt.views.TrailerUnitView.onPictureTaken(TrailerUnitView.java:233)
Everything works good on my phone (Nexus S) and in emulator. But users report this exception and I'm not sure why this is happening..
I've seen this happen on the Nexus phones, and some others. If you use DDMS to watch what is going on, I bet you'll see that your process is actually being terminated and then restarted. Thus your local state is being lost. You need to persist it, since Android can basically kill your process and restart it whenever it wants if you switch to a new task (and most of the camera capture intents set the NEWTASK flag).
If your class is an Activity you can use onSaveInstanceState() to store your filename, then read it back out of the Bundle you get in onCreate().
If you are not an Activity you can use the SharedPreferences store as a temporary place to save the filename:
private static void saveTempFileName(Context context, String filename) {
SharedPreferences settings = context.getSharedPreferences("whatever", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("com.yourstuff.whatever", filename);
editor.commit();
}
As #jeffamaphone noted you are probably having issues with app configuration changes. Application configuration change happens when something happens that affects the runtime environment of your app. Most notably this are: orientation change or keyboard hide/show.
Try this: start your app, invoke the Camera app (via your app action), change orientation, return to your app (via appropriate action). Does this sequence produce the error? Then you have issues with configuration change - when orientation changes usually (depending on your app settings) Android system restarts (kills and creates new instance) your Activity, which probably creates all new Views (without UUID set).
See handling configuration changes.

Categories

Resources