Store in sharedPreferences - android

I have some things in my app that I need to store, in order to have it available the next time I open the application. I wonder if I could save them using the sharedPreferences mechanism even if I don't have any "view" associated with them ! .If this is possible please let me know, if not, what would you suggest instead ?

All you need is a component that can furnish you a handle to the android.os.Context
An Activity is such a component. SharedPreference's data is stored in a file - somewhat akin to a properties file (key,value pair).
You can also create your own files and store it in the app's private directory.

Related

Flutter, Shared Preferences: Where are the shared preferences stored on the device?

I use the internal shared preferences to store key/values on an Android device. I wanted to have a look at this data directly using a file explorer but I cannot find where it is stored physically. Most apps have an own directory on Android/data but there is no folder for my Flutter app.
Do you have an idea where the data is stored locally?
Thanks in advance.
Edit (To make my problem clearer):
I need to access this data from outside app. A user of my app is not able to start the app anymore for some reasons. I want him to send me the internal data for debugging purposes.
So is it possible to fetch the related files in any way without the app self?
SharedPreferences are stored in an xml file in the app data folder, i.e.
/data/data/YOUR_PACKAGE_NAME/shared_prefs/
You can access these from Device File Explorer Tab inside the Android Studio IDE on the right side :
On native android its stored inside the data folder. The path will be something like
/data/data/<your_package_name>/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
On Flutter projects its stored inside the same folder but with fixed name FlutterSharedPreferences.xml
Something like
/data/data/<your_package_name>/shared_prefs/FlutterSharedPreferences.xml
Edited in response of updated question - 05/01/2023
If your app is relying heavily on sharedpreferences then I don't think there is a way to get it from outside your app. But I have an idea which you can try.
Try this package https://pub.dev/packages/flutter_logs.
This can create log files in file system. If your app is currently crashing after opening you can try to put this log code to write down the sharedpreferences inside the log file. Then you can setup some code to send the logs by email.
But there are few catches in this approach.
You have to set this code in initial screen like splashscreen and wait until this process is complete.
You have to send this updated apk and ask your user to install it.
I suggest you to implement this anyhow as in future you might get stuck in similar situation if you are relying on sharedpreferences.
Hope this helps.

how to dislplay once my fragment when app install in android

I am new to this concept ,How to display my fragment after in application,that fragment only display at after installing my app, please any one suggest me how to do that
Basically you need to store a value outside of the application itself. Shared Preferences or a database (eg SQLite) or a file could be used. The former, Shared Preferences, would be the simplest. There are loads of tutorials and examples eg Android Shared preferences example
You could use a boolean type or even simply check for the existence of any type and then set or create it respectively.

Can you change Shared Preferences from outside your app?

Is there a way user could mess with the shared preferences values without the help of my app? E.g. can I store license details here and not worry about user extracting and copying the license key?
Its really easy to access the shared preferences.
All you need is a file explorer with root access, they are saved in an xml file in /data/data/YOUR_APP_NAME/shared_prefs/YOUR_APP_NAME_preferences.xml
For licencing you should either use google play's licence check or implement your own checking on a remote server.
If the context of the shared preferences are private (you define it when you create) only with root access it is possible to access them, without being the application who created.
Shared preferences are stored inside the app's space, when you uninstall the app, the preferences are also gone.
So normally users cannot get these values, they are stored privately.
However, with root access and a simple memory search app, a user could be able to access the data. (It is always better to store such things server side.)

Android: Cheating prevention for "last level"

I'm building an Android game and I'm not sure where I should save something like "last completed level" or "remaining lives".
I'm pretty sure that I should not save this information in the database, because it's really simple to access an app's database with root access and some SQLite browser.
And I don't want to send it to a webserver, because the game should be playable offline.
What is the most secure place where I can store this information to prevent the player from cheating?
Thanks in advance
You may wanna try one of the three options described here :
http://developer.android.com/training/articles/security-tips.html
Since android is base on UID, it is almost impossible to prevent root user to retrieve data, but you can still encrypt it .
I would go for the internal storage with encryption, and skip the content provider option due to the few data you will store
You could use a non secure storage (like SharedPreferences for example) but use a digital signature to make sure that the value wasn't tampered with.
So you can use Cipher to save your game information file
check this https://stackoverflow.com/a/10782267/2773264
or you can save your file as Object by using ObjectOutputStream
(don't save String Object, save a custom class to prevent from cheating).

Saving preference that can be read and modified by another Application

I built an application that will download and install an APK from a WebService using the technique described here:
Install Application programmatically on Android
During this installation, the Webservice sends a 'flag' that indicates if the SQLite database from the application that is being updated should be deleted or not during it´s first run.
Is there any way to set a "Global Preference" that could be read (if the flag is true, the database should be deleted) and cleared (it should be set to false after deletion to avoid deleting the database all times that app is started) during the first usage of the updated app, without saving it to the SDCard?
I know how to read the preferences from the app that is being updated but, I did´t realize how to modify these preferences from another app.
Thanks a lot.
SharedPreferences are unique to each App/APK - no way to share them that I'm aware of and no 'Global' equivalent.
If you want to share data, the solution is usually some sort of ContentProvider, but that relies on both apps running at the same time.
If you only want to hand-over a token or state, I'd suggest writing a file onto the SDCARD is probably the simplest option?
Here is a tutorial on how to do it.
Basically you have to use MODE_WORLD_WRITEABLE for the prefs file.
To get the context for the other package you use createPackageContext()

Categories

Resources