Save Application Wide Boolean in SharedPreference - java

I know that this is a common question asked, and I have spent all afternoon trying different solutions that don't seem to work.
I am trying to store a boolean receiveNotifications in SharedPreferences but when I send a notification it still comes through. When I check whether the boolean is set in the activity I set it in, it says that the value is what it should be, but when I call this in my Firebase MessagingService it still allows the notification to come through.
This is my first time using them so if you see the obvious answer thats why.
Storing the Boolean:
// shared preferences
notificationsPref = mContext.getSharedPreferences("notifications", MODE_PRIVATE);
SharedPreferences.Editor editor = notificationsPref.edit();
editor.putBoolean("receiveNotifications", false);
editor.apply();
Checking if Boolean is Set:
// check if they want to receive notifications
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("notifications", MODE_PRIVATE);
Boolean areNotificationsAllowed = sharedPreferences.getBoolean("receiveNotifications", true);
if (areNotificationsAllowed){
Toast.makeText(this, "Send Notification", Toast.LENGTH_SHORT).show();
sendNotification(contentTitle, messageBody);
}

A push message is a Json object, next example is directly from the docs:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
There are 3 types of push messages, notification, data, and both;
//Notification
"message":{
"notification":{
}
}
//data
"message":{
"data":{
}
}
//both
"message":{
"notification":{
},
"data":{
}
}
Each will trigger a different behavior in the app depending if the app is open or not.
Notification: if the app is open the code on the service will be executed, if not the notification is showed by default
Data: Always the code on the service will be executed
Both: f the app is open the code on the service will be executed, if not the notification is showed by default and the data will be available in the launcher activity as extra obtainable from the intent
The Firebase web console will always send "notification" type and if you add data as custom params it will send both.
Your boolean will never be taken in consideration if the app is closed and the notification comes from the web console.

Turns out that no matter what you do Firebase must override whatever you have set in the application. I found this out by instead of sending from the Firebase console, I sent notification from my web server. The notification was stopped perfectly and the Shared Preference worked.

private SharedPreferences prefs;
private SharedPreferences.Editor editor;
prefs = getApplicationContext().getSharedPreferences("notifiactions",
MODE_PRIVATE);
editor = prefs.edit();
/////Assigning a Boolean///////////
editor.putBoolean("receiveNotifications", false);
editor.commit();
//Retrieving Boolean
prefs = getApplicationContext().getSharedPreferences("notifications",
MODE_PRIVATE);
bool = prefs.getBoolean("receiveNotifications", true);
//Try replacing this with your code also
if(bool){
}

Related

SharedPreferences does not return the default value

I have a sharedpreferences and I have created a method for it to be checked if it is the user's first time in the app. But it always returns the opposite of the default value.
My code:
public static Boolean getFirstOnApp(Context context) {
SharedPreferences pref = context.getSharedPreferences(LOGIN_PREFS_NAME, Context.MODE_PRIVATE);
return pref.getBoolean(KEY_FIRST_TIME_ON_APP, true);
}
Is always returns false.
I call it on my controller:
if (SaveSharedPreferences.getFirstOnApp(context)) {
fabAtivaMapeamento.performClick();
SaveSharedPreferences.setFirstOnApp(context, false);
}
SaveSharedPreferences.setFirstOnApp(context, false); has never been called before. It is only changed within this If
I already uninstalled the app, forced it to stop, cleared data and cache.
How to solve?
use like this to check if it's first time or not
SharedPreferences pref = getSharedPreferences(LOGIN_PREFS_NAME, Context.MODE_PRIVATE);
if (pref.getBoolean(KEY_FIRST_TIME_ON_APP, false)) {
//in here it's not for first time
} else {
//in here it's first time
}
Edit - Check if the following works.
public static Boolean getFirstOnApp(Context context) {
SharedPreferences pref = context.getSharedPreferences(LOGIN_PREFS_NAME, Context.MODE_PRIVATE);
//Check if the shared preference key exists.
//This way, you can determine if the fault is here or elsewhere.
if (pref.contains(KEY_FIRST_TIME_ON_APP)) {
return pref.getBoolean(KEY_FIRST_TIME_ON_APP, true);
} else {
return true;
}
}
Check if you've set your application to allow backup in the manifest android:allowBackup="true".
Set it as false, uninstall and then reinstall the app.
android:allowBackup=“false”
In case that doesn't fix the issue, then try setting the following in your manifest, uninstalling the app and then reinstalling it.
android:fullBackupContent="false"

Variables don't get changed after going back

I have a boolean variable public static boolean isInDarkTheme but when I try to change the value in my settings activity it only gets changed tempolarily.
I did it so:
if (on) {
//Do something when Switch button is on/checked
MainActivity.isInDarkTheme = true;
} else {
//Do something when Switch is off/unchecked
MainActivity.isInDarkTheme = false;
}
Log.d("DarkTheme", "SETTINGS " + MainActivity.isInDarkTheme);
in my settings the variable is changed but when I go back to my main with the
arrow I created with this:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
picture with this button
it is still the same in the main
but! when I use my software key to get back to the MainActivity it get saved
picture with software back key
Any idea what I can do that it get saved with the other button?
your variable will not be saved and will be collected by garbage collector once the activity is destroyed.
you have to use something like SharedPreferences.
to save the variable
SharedPreferences sharedPrefrences = getSharedPreferences("pref_name", MODE_PRIVATE);
sharedPrefrences.edit().putBoolean("isDarkTheme", true).apply();
to load
SharedPreferences sharedPrefrences = getSharedPreferences("pref_name", MODE_PRIVATE);
// key , default value
boolean isDark= sharedPrefrences.getBoolean("isDarkThem", false);
read about SharedPreferences here
The most likely reason is that both activities are loaded by different class loaders with the effect that the MainActivity you "see" in your Settings activity is a different one than the one you "see" in your other activity. You can find that out by logging the classloader "attached" to the MainActivity by calling MainActivity.class.getClassLoader()

Unable to fetch SharedPreferences

I'm trying to get familiar with using SharedPreferences, by building a simple test app where I store and retrieve user preferences by using a class that extends 'PreferenceActivity'.
The problem is that every time I shut down the app and start it again I'm unable to load the SharedPreferences values that I earlier selected.
In the MainActivity's onCreate method I am first calling a method 'loadPreferences', then creating an ImageView and a button.
I assigned onclick listener to the button that creates and starts a new intent.
...onClick(View v){
Intent intent = new Intent(MainActivity.this, MyPrefsActivity.class);
startActivity(intent);
}
In MyPrefsActivity class I have a ListPreference that has a 'Preference.OnPreferenceChangeListener'.
... {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
imageNumber = Integer.parseInt(newValue.toString());
return true;
}
};
Upon return from MyPrefsActivity to MainActivity in the 'onResume':
protected void onResume() {
super.onResume();
savePreferences();
loadPreferences();
}
private void savePreferences(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("imageNumber", MyPrefsActivity.imageNumber);
editor.apply();
}
private void loadPreferences (){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int imageNumb = prefs.getInt("imageNumber", 0);
switch (imageNumb){
case 0:
imageView.setImageResource(R.mipmap.image1);
break;
case 1:
imageView.setImageResource(R.mipmap.image2);
break;
case 2:
imageView.setImageResource(R.mipmap.image3);
break;
default:
imageView.setImageResource(R.mipmap.image4);
}
}
When 'loadPreferences' is called for the first time upon startup, 'imageNumb' allways has a value 0, instaed of the value that I assigned to on previous run in method 'savePreferences'.
I noticed that after startup when I enter 'MyPreferencesAction' for the first time and open the ListPreferences, the checkbox that I selected on the last run is already selected for me. It seems like my selection is saved but when I try to load SharedPreferences am I messing up something???
You have to save your preferences in onPause event. Right now you are saving it in onResume when values you want are no longer there.
#Override
protected void onPause(){
super.onPause();
savePreferences();
}
#Override
protected void onResume() {
super.onResume();
loadPreferences();
}
There is another issue with your code. You are saving preferences in MainActivity, but you are changing them in MyPrefsActivity. That will not work. You have to save changes to the preferences in MyPrefsActivity, use above load/save pattern in there too. If you don't have any preference changes happening in MainActivity, you can safely omit calling savePreferences from it.
Depending on MyPrefsActivity declaration calling MyPrefsActivity.imageNumber from MainActivity may not be safe, you will have to change that code too. Like I said, most likely you don't need it at all in MainActivity (if you saving are only preferences values that are set in MyPrefsActivity) and that should be part of MyPrefsActivity.
Also Preference.OnPreferenceChangeListener is probably redundant since it's main usage is to be invoked when the value of this Preference has been changed by the user and is about to be set and/or persisted. This gives the client a chance to prevent setting and/or persisting the value.
I realized there was a lot of needless complexity in my code. So I got completely rid of the method savePreferences(). Instead i simplified the PreferenceChangeListeners method onPreferenceChange(...):
This seems to me to be the most simplest way to update SharedPreferences when using PreferenceActivity.
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.getEditor().
putInt("imageNumber", Integer.parseInt(newValue.toString())).apply();
return true;
}
Now I save the SharedPreferences manually, only when 'onPreferenceChange' is called. Not on 'onResume', 'onStop', 'onRestart' or on 'onPause'.
Please inform me if this is a bad way to change SharedPreferences.
Quoting Dalija Prasnikar: "reference.OnPreferenceChangeListener is probably redundant since it's main usage is to be invoked when the value of this Preference has been changed by the user and is about to be set and/or persisted. This gives the client a chance to prevent setting and/or persisting the value."
If you understood you correctly Dalija, there is a 'onPreferenceChange(...)' like method that does the job but does not have a boolean return value but void? I was not able to find any examples, could you please show or point to an examle?

The method getPreferenceManager() is undefined for the type BatteryService in android app

I am making a service so I have service class which is extended from Service. I am getting a checkbox from PreferenceScreen and based on whether it is checked or not I perform an action. To get CheckBoxPreference I use getPreferenceManager() method. But it has red underline. When I hover over it shows me this error The method getPreferenceManager() is undefined for the type BatteryService. When I use the same code in my main class then it works. Here is my code
public int onStartCommand(Intent intent, int flags, int startId) {
initNotification();
final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("cbAlarm");
checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (newValue.toString().equals("true")) {
Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_LONG).show();
registerReceiver(batteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
} else if (newValue.toString().equals("false")) {
stopService(new Intent(getBaseContext(),
BatteryService.class));
Toast.makeText(getApplicationContext(),
"Unchecked", Toast.LENGTH_SHORT).show();
}
return true;
}
});
return START_STICKY;
}
How can I solve this problem? Help Please
The getPreferencesManager() function is not available on Service derived objects - only on PreferenceActivity or PreferenceFragment derived objects where you likely copied the above code from, or individual Preference objects.
If you only wanted to sample the value of the checkbox preference when your service starts, use something like:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean alarm = sp.getBoolean("cbAlarm", false); // defaulting to false
However, I'm guessing from your Listener code that you also want to be notified if the user changes the setting. That would mean keeping onPreferenceChange in the PreferenceActivity/PreferenceFragment, and sending the service a message. For that, consider something like Android best way to communicate from an Activity to a Service / Engine, or simply start/stop the whole service if it has nothing to do when the Alarm is off!

Instructions called before the activity is closed

I'm trying to understand if the user has enabled the admin or not and LATER update a checkbox (in onResume).
The problem is that the activity which allows the user to enable the admin is launched and it's launched the following code without waiting for the user decision.
How could bypass it?
private void doAttivaRimuoviAdmin() {
if (isAdminAttivo()) {
mDPM.removeActiveAdmin(mDeviceAdminSample);
} else {
Intent localIntent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
localIntent.putExtra("android.app.extra.DEVICE_ADMIN", mDeviceAdminSample);
localIntent.putExtra("android.app.extra.ADD_EXPLANATION",
getString(R.string.spiegazione_amministratore));
startActivityForResult(localIntent, 1);
// se non è stato dato il permesso, non attiva la checkbox
Editor e = mPrefs.edit();
if (isAdminAttivo()) {
e.putBoolean("spegnischermoabilitato", true);
} else {
e.putBoolean("spegnischermoabilitato", false);
}
e.commit();
Log.i(getString(R.string.app_name), ""+ mPrefs.getBoolean("spegnischermoabilitato", false));
}
}
In poor words, the sharedpreference "spegnischermoabilitato" always has FALSE within it.
From the documentation of startActivityForResult():
Launch an activity for which you would like a result when it finished.
When this activity exits, your onActivityResult() method will be
called with the given requestCode. Using a negative requestCode is the
same as calling startActivity(Intent) (the activity is not launched as
a sub-activity).
In other words, you will need to override onActivityResult(). In this method you will then have to check that the method is called with the requestCode you set in startActivityForResult(), and also that it contains the expected resultCode. If it does write your preference.

Categories