SharedPreferences push [closed]

0

I would like to save my push messages in SharedPreferences and then teach them in a layout as it could be.

I use firebase cloud messaging to send push messages.

    
asked by jorje garcia 31.08.2016 в 03:57
source

1 answer

2

To use SharedPreferences you must first load the manager with:

SharedPreferences preferences = this.getSharedPreferences("TAG_APP_PREF", Context.MODE_PRIVATE);

Save data

To save a value, a manager editor is defined.

SharedPreferences.Editor editor = preferences.edit();

Now you can store data, for example a string would be made:

editor.putString("DJ","Paco Pil");

Other types of data can be stored

putBoolean, putFloat, putInt, putLong, putString

Once the data has been added to the editor, it must be saved, it is done with:

editor.apply()

Get data

To obtain data, it is done directly from the manager defined for SharedPreferences

String DJName = preferences.getString("DJ", "");
Log.d("debug, "El valor de DJ es: " + DjName);

Note that if there is no DJ within the SharedPreferences you will be given the default value of the second parameter, in this case ""

Functional example

SharedPreferences preferences = this.getSharedPreferences("TAG_APP_PREF", Context.MODE_PRIVATE);

//Escribir datos en el gestor
SharedPreferences.Editor editor = preferences.edit();
editor.putString("DJ","Paco Pil");
editor.putString()
editor.apply();

//Leer datos del gestor
String name = preferences.getString("DJ", "");
Log.d(TAG, "El valor de DJ es: " + name);
    
answered by 09.10.2016 в 11:49