Android - App will not read sharedpreferences from Library correctly - android

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.

Related

What is the proper way to implement SharedPreferencesBackupHelper?

initial use of sharedpreferences.
BackupManager bm = new BackupManager(getApplicationContext());
SharedPreferences sharedPreferences = getSharedPreferences("AppData", MODE_PRIVATE);
if(sharedPreferences.getBoolean("my_first_time", true)) {
sharedPreferences.edit().putBoolean("my_first_time", false).apply();
sharedPreferences.edit().putString("user_id", String.valueOf(dbh.insertUserId())).apply();
sharedPreferences.edit().putInt("noteIndex", -1).apply();
sharedPreferences.edit().putInt("checklistIndex", -1).apply();
bm.dataChanged();
}
Backing up data
public class MyPrfsBackupAgent extends BackupAgentHelper {
#Override
public void onCreate() {
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, "AppData");
addHelper("sharedprefs_key", helper);
}
}
My goal is to backup the whole sharedpreferences file 'AppData' and restore it after re-installation of the feature is failing to complete its task. Ive tried it with allowbackup in manifest on both true and false, and running it on my actual device to test it but nothing seems to work accordingly.
items added to AndroidManifest.xml in attempt to fix this
android:backupAgent=".BackupData$MyPrfsBackupAgent"
<meta-data android:name="com.google.android.backup.api_key"
android:value="value" />
I followed the docs on this as well as other stackoverflow references but none have helped me solve the problem. I would be open to trying a different way of backup/restoring but cant find another and wont use autoBackup because it targets sdk 23 while my min is 21.
All the shared preferences data is stored in shared preferences .xml files in getFilesDir().getParentFile() directory in a folder named shared_prefs.
Simply copy all the files there to any place you want.
At reinstall do the reverse and reboot.

how to make a screen in android that runs only when the app installs

I am making an Android project. In that project i want a screen to appear only during the app installation and not every time we start the app. For eg. in whatsapp the page in which we put our name appears only during installation. I want exactly that type of a screen. I am fairly new to android programming so any help will be appreciated. Thanks! Cheers!
You can use the SharedPreferences to identify if it is the "First time" the app is launched.
Just use a Boolean variable ("app_first_time") and change its value to false after your task runs for the first time.
final String PREFS_NAME = "PrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("app_first_time", true)) {
//here the app is being launched for first time, launch your screen
settings.edit().putBoolean("app_first_time", false).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

Give a hint to user at the time of installation time only

I am doing a project in which,i have to give the user a message at the time of installation only.After that the message should be hidden.Now whenever i run the program the message will be displayed.I want to avoid it. I want to do it programatically in android.Can anyone help to resolve this issue?
Thanks in Advance
You can save a flag into the app database or userPreferences and after displaying the message you can set the flag to false or 0 . Anytime the user starts the app you check that flag and see if you have to display it again or not...
Use SharedPreferences.
SharedPreferences settings = getSharedPreferences("MyPref", MODE_PRIVATE);
Editor edt = settings.edit();
edt.putBoolean("Accepted", true);
edt.commit();
boolean isTOSAccepted = settings.getBoolean("Accepted", false);

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