is there a way to retain some of my data after uninstalling the app and than again installing it on the device. I need to check if my app had been installed on one's device previously or not.
Yeah it's possible if you save App Data in External Storage of your device because that data is not deleted even if your app is uninstalled.
Upon reinstalling the app, you can read the file data to determine was the app previously installed or it is a fresh install in case that file doesn't exists.
Note:- Data in External Directory would be visible to user so it might be a case that user may delete the data from file manager.
You can use shared preferences to store this data.
Create the sharedreferences with MODE_WORLD_READABLE.
You can set a flag as true when the app is run, and can extract this value on each installation to find out whether this value exists.
Something like below:
When the app is run(Write this code in the launch code for your app-which is triggered first):
SharedPreferences sharedpreferences = context.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("app_already_installed", "true");
editor.commit();
Checking whether the app was already installed(Write this where you want to check for earlier installations):
SharedPreferences sharedpreferences = context.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
String is_app_already_installed= sharedpreferences.getString("app_already_installed", "false");
if(is_app_already_installed.equals("true"){
//do-something
}
else{
//do-something
}
Related
I am using WebView for loading a webpage in my app. I have enabled cache in-app so when the internet is not available the WebView loads caches.
Actually The problem here arrives is that when I open WebView for the first time when the app is installed or when caches are cleared when the caches are not yet saved in local and internet is off, It shows webpage is not available error.
So I decided to show AlertDialog warning showing that if no internet connection is available. But then it loads the no internet connection AlertDialog warning everytime when internet connection in not available.
I want that AlertDialog warning only when the cache is not available in storage , is there any option to check if cache is available in storage or not?
I got a solution to this problem by using SharedPreferences. I have done research on this and I found I can use SharedPreferences to check if the app is starting the first time or not.
I added following in my code
SharedPreferences prefs = getSharedPreferences("prefs",MODE_PRIVATE);
boolean firstStart = prefs.getBoolean("firstStart",true);
And also I used
if(firstStart)
and called the
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstStart", false);
editor.apply();
in the statement where I wanted to save the state.And all worked fine.
I have a SharedPreference that counts the amount of launches of my App. I expect this to be 0, when I install my app. Nevertheless it is 14. The same strange behaviour I observe with my database, that already exists on a fresh install.
I didn't intent to recycle my app data (like in the Google Cloud). On my device in the account settings the app-data sync is on. If I turn it off, and make a reinstall I observe the same behaviour.
Anyone has every observed such a behaviour? Is there any way to prevent recycling old data and force a proper install?
In Android Marshmallow Google introduced the "Auto Backup" feature which is turned on by default if your targetSdkVersion is >=23.
This will back up your database and SharedPreferences by default and restore it when you re-install the application. To turn this feature off you have to add android:allowBackup="false" to your AndroidManifest.xml.
More info here: Auto Backup for Apps
Review your code in AndroidManifest on tag application if it has android:allowBackup="false". If you don't have (by default it's true), your app participates on android backup and restore infrastructure and can happen exactly what you say.
More information in this post: What is "android:allowBackup"?
This needs to be handled from app side regarding shared Prefs.
Created a shared preference helper class and in the helper class have the below condition.
private static String SHARED_PREFS_VERSION = "SharedPrefsVersion"; // In this save the version of current shared Prefs.
void SharedPrefsHelper() {
if( BuildConfig.Version > getSharedPrefsVersion() ) {
SharedPreferences.Editor editor = prefs.edit(); editor.clear(); // Clear all the shared Prefs and update the current version.
SetSharedPrefsVersion(BuildConfig.Version);
}
}
For further reference look at :
https://stackoverflow.com/a/12381061/7364024
My login system uses SharedPreferences to store information about the user. The main two variables for the login system are:
loggedin - boolean
userID - int of the userID that's logged in (primary key on the DB)
When the user logs in, loggedin is set to 1 and the userID is set to the user ID fetched from the DB. When the logout button is pressed loggedin is set to 0 and userID set to null.
Situations:
I log in, close the app and repoen it = fine, still logged in to the correct account
I log in, then logout, close & reopen the app = fine, comes up with login screen
I carry out invalidate caches and restart in android studio = fine, stays logged in on the correct account.
Now here's the one that's going wrong: If I login, then uninstall the app off my phone and press run on android studio the app launches and logs in to a really old account that no longer exists on the DB; and I can't work out why this is happening.
My only thought is there is a userID stored on the device that isn't being removed, but that could be completely wrong. Any ideas?
I've added some log tags throughout the code and before the app is uninstalled the userID is correct and when it's been reinstalled it's the old one.
Shared Preferences are always cleared along with uninstalling app.
But since android-21 Backup task stores preferences by default to cloud. Later when you uninstall then install newer version .You are probably going to use restored preferences. To avoid that, just add this to your manifest ( or at least manifest for debug). -
<application ...
android:allowBackup="false">
...
</application>
Read this:http://developer.android.com/guide/topics/data/backup.html
It's Important to mention here that process of backup is blackbox .. you don't know when it starts, and period between checks ... so better for developing to disable it.
I wrote the following code to create two shared preference files:
SETTINGS_NAME for normal business, and OAUTH_NAME
to hide oauth consumer keys, secrets, and access tokens.
[I've read many times on stackOverflow this is the best, though not great, way to hide them.]
protected void onStart() {
super.onStart();
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
sharedPreferencesOauth = getSharedPreferences(Constants.OAUTH_NAME, Context.MODE_PRIVATE);
}
But file OAUTH_NAME is not at all private!
It may be true that it can't be programmatically read by other apps,
but on a rooted device it can be opened and examined in ES File Explorer;
It's in /data/data/{packageName}/share_prefs/SHARED_PREFS_OAUTH_NAME.xml .
Certainly not much of a challenge for a hacker.
File SETTINGS_NAME, on the other hand, does seem to be more private in that
it is NOT in folder /data/data/{packageName}/share_prefs/ .
My question: why is file SETTINGS_NAME not in folder .../shared_prefs while file OAUTH_NAME -- also created MODE_PRIVATE -- is in folder .../shared_prefs?
Private settings are saved in an application's private directory.
On a rooted device nothing is private. It's like leaving your house's door wide open and wondering how can you be safe against a thief.
Saving a file in a more hidden location doesn't give much security, because anyone can dump .apk, decompile it and find where that file was saved. The only way you can save secure data on a rooted phone is to use encryption without storing a password.
You are running on Rooted-device right ?
Once rooted, everything can be accessed even the system file. so MODE_PRIVATE is working in this case.
You can access them via any explorer app. Can't protect your file now !!!
I have developed a Import / Export feature for my app so uses can export the database and shared preferences to be used in another phone. This has worked fine up until I got myself a S3 and tried to do it.
After some work I got the database import / export to work (duel SCdards issue) but the shared preferences is still causing me trouble.
On a new install the import fails because there is no shared preferences file on the phone. Once I update an option the File is created (added in a check to see if the file on the phone exists)
This to create it
this.sharedprefs = context.getSharedPreferences("sharedprefres.xml", Activity.MODE_PRIVATE);
This to check file is there
File sharedPrefencesFileOnPhone = new File(/data/data/PACKAGE_NAME/shared_prefs/, "sharedprefres.xml");
if (sharedPrefencesFileOnPhone.exists())
Log.d(DEBUG_TAG, "SP : Running Copy");
The Copy then seems to go though but none of the options are updated.
This all works on the emulator with no issues
The check only works once I have updated a option and the file is created which is given me to belive the shared preferences are saved.
Does the S3 hide the shared preferences somewhere else and if so is there some code for me to track it down that would work on multi phones
Thanks for your time
Try
/dbdata/databases/package.name/shared_prefs/package.name_preferences.xml (if it exists use it)
otherwise
new File(getFilesDir(), "../shared_prefs");
or
/data/data/package.name/shared_prefs/package.name_preferences.xml