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()
Related
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.
I need to update an application on Google-Play but I WANT that all the old settings saved via Shared Preferences and Local Storage will be wipe instead of being kept (because I have change the main structure of the data that will be saved and it's not compatible anymore with the old one).
How can I do that programmatically?
Thank you in advance for the future answers.
Heres how I would do it:
If you haven't already, create an Application class that extends android.app.Application. Make sure you declare this in your manifest. Read more about this here - http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android.
Then in your applications onCreate method, check the version of the app, and if it matches the new version, run your methods to clear out the shared preferences,local storage.
You can then set a boolean preference that the 'upgrade' has been completed,so your app doesn't try to clear out the data every time it's run.
I am having an abstract class having all static objects and function to store some globel data of the application during the execution session.
All data reset to null once I relaunch the application in ICS and above version with Systems's setting ALWAYS_FINISH_ACTIVITIES is set as true.
Whats the better way to mainitan data on application relaunch?
Regards,
Android IT
Editing my question:
I know that Sharepreference can be a better option but I dont want to store data for multiple sessions of the application and data I am storing is huge.
Regards,
Android
IT
Each Android app is running in a dependent process. When an app become background and system need memory, the app's process may be killed by system. In this situation, when app relaunch, static objects will lost.
I think use SharedPreference to maintain data is a better way.
You can store data using SharedPreferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Store the data on the local storage would seem appropriate: http://developer.android.com/training/basics/data-storage/index.html
You can store the data local using a XML file. You can read the XML File on startup with a DOM Reader
http://www.ibm.com/developerworks/library/x-android/
If you have lots of data you can use a SQLite Database
http://developer.android.com/training/basics/data-storage/databases.html
If there are not too much data you can keep them in SharedPreferences
You access them with PreferenceManager.getDefaultSharedPreferences()
Once application exit or destroy that will remove all data . if you want to access after relaunch you should save your data as per your requirement . like local or web. its deepen you. right now you can store data in preference so it will useful after relaunch the application.
I think this is better for you : How to use SharedPreferences in Android to store, fetch and edit values
Thanks for previous replies,
is it possible to delete the stored content from sqlite once re-install the application. I am storing data in database, once i re-install the same app again, the previous data still stores in sqlite. i want to delete the stored content while re install the app. i am not sure about this.
This seems like a hack and maybe not the actual answer, but can you -- with each new version of your app -- increment an identifier (from 10 to 11) in the code and then check against a stored preferences containing that identifier. If you have a constant in your code that is higher than the identifier stored on the previous device, then you can clear the database to whatever you think its state should be. Then with each new released version of the app you increment this number..
Edit: In API level 9 and higher, you can -- whenever your app starts up -- write the date in which the app was installed (see here for an explanation on how to find the install date). If you check that it was installed after the date which is written, kill the data!
Thanks for all replies,
I maintain the Version code for all the builds, once i re-install the application, i check the version, if i found the version changes, i simply remove the DB. this case only applicable for overriding the install of APk(ie without un-installing the application). When we made a uninstall, the internal data and sqlite for the application ill automatically deleted.
sqlite databases are stored as files on your file system, to delete the data. You just need to delete the file.
What you'd want to do is setup some way of detecting if the app is being run for the first time, if this is the first time the app has been run then check the database exists, if it does delete it. Then recreate the database as empty.
Or you could go through and remove all the data in each table/drop each table in the database on the first run if the database exists.
Read here : Detect Android app upgrade and set Application class boolean for show/hide of EULA
Create a UpgradeBroadcastReceiver of your own that will run the delete instructions you want and register it in your manifest file.
When you delete the app, then the database should be deleted too. Unless you go to some trouble to keep it. If you simply update the app, then the data should be kept. If you need to delete the data upon reinstall, try this:
Every time you start an Activity, call PackageManager.getPackageInfo() and check lastUpdateTime. Compare it with a time stamp that you store in the database or a shared preference. If lastUpdateTime is newer, delete the your database.
application SharedPreferance is deleted on un-installing the app, so save a boolean to determine if the application is running for the first time or not.
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.