下面列出了android.content.SharedPreferences#contains ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean needsToShowVoiceInputKey(final SharedPreferences prefs,
final Resources res) {
// Migrate preference from {@link Settings#PREF_VOICE_MODE_OBSOLETE} to
// {@link Settings#PREF_VOICE_INPUT_KEY}.
if (prefs.contains(Settings.PREF_VOICE_MODE_OBSOLETE)) {
final String voiceModeMain = res.getString(R.string.voice_mode_main);
final String voiceMode = prefs.getString(
Settings.PREF_VOICE_MODE_OBSOLETE, voiceModeMain);
final boolean shouldShowVoiceInputKey = voiceModeMain.equals(voiceMode);
prefs.edit()
.putBoolean(Settings.PREF_VOICE_INPUT_KEY, shouldShowVoiceInputKey)
// Remove the obsolete preference if exists.
.remove(Settings.PREF_VOICE_MODE_OBSOLETE)
.apply();
}
return prefs.getBoolean(Settings.PREF_VOICE_INPUT_KEY, true);
}
@Override
protected String getPersistedString(String defaultReturnValue) {
SharedPreferences pref = getPreferenceManager().getSharedPreferences();
String key = getKey();
if (!shouldPersist() || !pref.contains(key))
return defaultReturnValue;
return String.valueOf(pref.getInt(key, 0));
}
public static boolean readShowsLanguageSwitchKey(final SharedPreferences prefs) {
if (prefs.contains(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY)) {
final boolean suppressLanguageSwitchKey = prefs.getBoolean(
PREF_SUPPRESS_LANGUAGE_SWITCH_KEY, false);
final SharedPreferences.Editor editor = prefs.edit();
editor.remove(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY);
editor.putBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, !suppressLanguageSwitchKey);
editor.apply();
}
return prefs.getBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, true);
}
/**
* Returns true if the latitude and longitude values are available. The latitude and
* longitude will not be available until the lesson where the PlacePicker API is taught.
*
* @param context used to get the SharedPreferences
* @return true if lat/long are saved in SharedPreferences
*/
public static boolean isLocationLatLonAvailable(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
boolean spContainLatitude = sp.contains(PREF_COORD_LAT);
boolean spContainLongitude = sp.contains(PREF_COORD_LONG);
boolean spContainBothLatitudeAndLongitude = false;
if (spContainLatitude && spContainLongitude) {
spContainBothLatitudeAndLongitude = true;
}
return spContainBothLatitudeAndLongitude;
}
/**
* Returns true if the latitude and longitude values are available. The latitude and
* longitude will not be available until the lesson where the PlacePicker API is taught.
*
* @param context used to get the SharedPreferences
* @return true if lat/long are saved in SharedPreferences
*/
public static boolean isLocationLatLonAvailable(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
boolean spContainLatitude = sp.contains(PREF_COORD_LAT);
boolean spContainLongitude = sp.contains(PREF_COORD_LONG);
boolean spContainBothLatitudeAndLongitude = false;
if (spContainLatitude && spContainLongitude) {
spContainBothLatitudeAndLongitude = true;
}
return spContainBothLatitudeAndLongitude;
}
private static Set<String> getStringSetPreference(AccountContext accountContext, String key, Set<String> defaultValues) {
final SharedPreferences pref = getCurrentSharedPreferences(accountContext);
if (null != pref && pref.contains(key)) {
return pref.getStringSet(key, Collections.<String>emptySet());
} else {
return defaultValues;
}
}
@Override
public void setLastExecutionDateFromPreferences() {
SharedPreferences pref = getActivity()
.getSharedPreferences(getString(R.string.wakeupat_preferences_name),
Context.MODE_PRIVATE);
if (pref.contains(getString(R.string.key_last_execution_date))) {
String notFormattedDate
= pref.getString(getString(R.string.key_last_execution_date), null);
if (!TextUtils.isEmpty(notFormattedDate)) {
setLastExecutionDate(DateTime.parse(notFormattedDate));
}
}
}
public boolean isEnabledByPrefs(final Sensor sensor) {
String preferenceKeyString = "pref_" + sensor.name().toLowerCase(Locale.ENGLISH) + "_on";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
if (!prefs.contains(preferenceKeyString)) {
//throw new RuntimeException("Programmer error, could not find preference with key " + preferenceKeyString);
return false;
}
return prefs.getBoolean(preferenceKeyString, true);
}
private String getCookie(String url, String domain) {
SharedPreferences sp = Utils.getContext().getSharedPreferences(COOKIE_PREF, Context.MODE_PRIVATE);
if (!TextUtils.isEmpty(url) && sp.contains(url) && !TextUtils.isEmpty(sp.getString(url, ""))) {
return sp.getString(url, "");
}
if (!TextUtils.isEmpty(domain) && sp.contains(domain) && !TextUtils.isEmpty(sp.getString(domain, ""))) {
return sp.getString(domain, "");
}
return null;
}
private static boolean readSuggestionsEnabled(final SharedPreferences prefs) {
if (prefs.contains(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE)) {
final boolean alwaysHide = SUGGESTIONS_VISIBILITY_HIDE_VALUE_OBSOLETE.equals(
prefs.getString(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE, null));
prefs.edit()
.remove(Settings.PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE)
.putBoolean(Settings.PREF_SHOW_SUGGESTIONS, !alwaysHide)
.apply();
}
return prefs.getBoolean(Settings.PREF_SHOW_SUGGESTIONS, true);
}
/**
* 查询某个key是否已经存在
*/
public static boolean contains(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(getSpName(context), Context.MODE_PRIVATE);
return sp.contains(key);
}
private boolean checkKeyInPreferences(String key) {
SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
return pref.contains(key);
}
@Override
protected void load() throws IOException {
if (!getFile().exists() || !getFile().canRead()) {
throw new FileNotFoundException(getFile() + " doesn't exist or not readable");
}
toc = new LinkedHashMap<>();
SharedPreferences bookdat = getSharedPreferences();
if (bookdat.contains(ORDERCOUNT)) {
int toccount = bookdat.getInt(ORDERCOUNT, 0);
for (int i = 0; i < toccount; i++) {
String label = bookdat.getString(TOC_LABEL + i, "");
String point = bookdat.getString(TOC_CONTENT + i, "");
toc.put(point, label);
Log.d("EPUB", "TOC: " + label + ". File: " + point);
}
} else {
try (BufferedReader reader = new BufferedReader(new FileReader(getFile()))) {
int c = 0;
String line;
Pattern idlinkrx = Pattern.compile("<a\\s+[^>]*\\b(?i:name|id)=\"([^\"]+)\"[^>]*>(?:(.+?)</a>)?");
Pattern hidlinkrx = Pattern.compile("<h[1-3]\\s+[^>]*\\bid=\"([^\"]+)\"[^>]*>(.+?)</h");
SharedPreferences.Editor bookdatedit = bookdat.edit();
while ((line = reader.readLine()) != null) {
String id = null;
String text = null;
Matcher t = idlinkrx.matcher(line);
if (t.find()) {
id = t.group(1);
text = t.group(2);
}
Matcher t2 = hidlinkrx.matcher(line);
if (t2.find()) {
id = t2.group(1);
text = t2.group(2);
}
if (id != null) {
if (text==null) text=id;
bookdatedit.putString(TOC_LABEL +c, text);
bookdatedit.putString(TOC_CONTENT +c, "#"+id);
toc.put("#"+id, text);
c++;
}
}
bookdatedit.putInt(ORDERCOUNT, c);
bookdatedit.apply();
}
}
}
public boolean contains(String key) {
SharedPreferences sharePre = ctx.getSharedPreferences(FileName,
Context.MODE_PRIVATE);
return sharePre.contains(key);
}
@Override
public boolean onFragmentCreate() {
rowCount = 0;
customRow = rowCount++;
customInfoRow = rowCount++;
generalRow = rowCount++;
soundRow = rowCount++;
vibrateRow = rowCount++;
if ((int) dialog_id < 0) {
smartRow = rowCount++;
} else {
smartRow = -1;
}
if (Build.VERSION.SDK_INT >= 21) {
priorityRow = rowCount++;
} else {
priorityRow = -1;
}
priorityInfoRow = rowCount++;
boolean isChannel;
int lower_id = (int) dialog_id;
if (lower_id < 0) {
TLRPC.Chat chat = MessagesController.getInstance(currentAccount).getChat(-lower_id);
isChannel = chat != null && ChatObject.isChannel(chat) && !chat.megagroup;
} else {
isChannel = false;
}
if (lower_id != 0 && !isChannel) {
popupRow = rowCount++;
popupEnabledRow = rowCount++;
popupDisabledRow = rowCount++;
popupInfoRow = rowCount++;
} else {
popupRow = -1;
popupEnabledRow = -1;
popupDisabledRow = -1;
popupInfoRow = -1;
}
if (lower_id > 0) {
callsRow = rowCount++;
callsVibrateRow = rowCount++;
ringtoneRow = rowCount++;
ringtoneInfoRow = rowCount++;
} else {
callsRow = -1;
callsVibrateRow = -1;
ringtoneRow = -1;
ringtoneInfoRow = -1;
}
ledRow = rowCount++;
colorRow = rowCount++;
ledInfoRow = rowCount++;
SharedPreferences preferences = MessagesController.getNotificationsSettings(currentAccount);
customEnabled = preferences.getBoolean("custom_" + dialog_id, false);
boolean hasOverride = preferences.contains("notify2_" + dialog_id);
int value = preferences.getInt("notify2_" + dialog_id, 0);
if (value == 0) {
if (hasOverride) {
notificationsEnabled = true;
} else {
if ((int) dialog_id < 0) {
notificationsEnabled = preferences.getBoolean("EnableGroup", true);
} else {
notificationsEnabled = preferences.getBoolean("EnableAll", true);
}
}
} else if (value == 1) {
notificationsEnabled = true;
} else if (value == 2) {
notificationsEnabled = false;
} else {
notificationsEnabled = false;
}
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.notificationsSettingsUpdated);
return super.onFragmentCreate();
}
private static boolean checkSharedPreferencesKey(String key) throws PbContextNullException {
SharedPreferences pref = getSharedPreferences();
return pref.contains(key);
}
/**
* Checks whether or not the preference exists in the given preference object.
*
* @param prefs The shared preferences to check for preference existence in.
*/
public final boolean has(SharedPreferences prefs) {
return prefs.contains(this.key);
}
/**
* 查询某个key是否已经存在
*
* @param context
* @param key
* @return
*/
public static boolean contains(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 查询某个key是否已经存在
*
* @param context
* @param key
* @return
*/
public static boolean contains(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 查询某个key是否已经存在
*
* @param key
* @return
*/
public static boolean contains(String key) {
SharedPreferences sp = XFrame.getContext().getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.contains(key);
}