EncryptedSharedPreferences
public
final
class
EncryptedSharedPreferences
extends Object
implements
SharedPreferences
java.lang.Object
|
↳ |
androidx.security.crypto.EncryptedSharedPreferences
|
An implementation of SharedPreferences
that encrypts keys and values.
String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
"secret_shared_prefs",
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
// use the shared preferences and editor as you normally would
SharedPreferences.Editor editor = sharedPreferences.edit();
Summary
Nested classes |
enum |
EncryptedSharedPreferences.PrefKeyEncryptionScheme
The encryption scheme to encrypt keys.
|
enum |
EncryptedSharedPreferences.PrefValueEncryptionScheme
The encryption scheme to encrypt values.
|
Public methods |
boolean
|
contains(String key)
|
static
SharedPreferences
|
create(String fileName, String masterKeyAlias, Context context, EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)
This method is deprecated.
Use create(Context, String, MasterKey, PrefKeyEncryptionScheme, PrefValueEncryptionScheme) instead.
|
static
SharedPreferences
|
create(Context context, String fileName, MasterKey masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)
Opens an instance of encrypted SharedPreferences
|
SharedPreferences.Editor
|
edit()
|
Map<String, ?>
|
getAll()
|
boolean
|
getBoolean(String key, boolean defValue)
|
float
|
getFloat(String key, float defValue)
|
int
|
getInt(String key, int defValue)
|
long
|
getLong(String key, long defValue)
|
String
|
getString(String key, String defValue)
|
Set<String>
|
getStringSet(String key, Set<String> defValues)
|
void
|
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
|
void
|
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
|
Public methods
contains
public boolean contains (String key)
create
public static SharedPreferences create (Context context,
String fileName,
MasterKey masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme,
EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)
Opens an instance of encrypted SharedPreferences
Parameters |
context |
Context |
fileName |
String : The name of the file to open; can not contain path
separators. |
masterKey |
MasterKey : The master key to use. |
prefKeyEncryptionScheme |
EncryptedSharedPreferences.PrefKeyEncryptionScheme : The scheme to use for encrypting keys. |
prefValueEncryptionScheme |
EncryptedSharedPreferences.PrefValueEncryptionScheme : The scheme to use for encrypting values. |
Throws |
GeneralSecurityException |
when a bad master key or keyset has been attempted |
IOException |
when fileName can not be used
|
getAll
public Map<String, ?> getAll ()
getBoolean
public boolean getBoolean (String key,
boolean defValue)
Parameters |
key |
String |
defValue |
boolean |
getFloat
public float getFloat (String key,
float defValue)
Parameters |
key |
String |
defValue |
float |
getInt
public int getInt (String key,
int defValue)
Parameters |
key |
String |
defValue |
int |
getLong
public long getLong (String key,
long defValue)
Parameters |
key |
String |
defValue |
long |
getString
public String getString (String key,
String defValue)
Parameters |
key |
String |
defValue |
String |
getStringSet
public Set<String> getStringSet (String key,
Set<String> defValues)
Parameters |
key |
String |
defValues |
Set |
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples/Code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-02-24 UTC.