下面列出了android.content.SharedPreferences#Editor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void updateParams(boolean inval) {
pitches = new int[rows][keys];
int p = 0;
for (int i = 0; i < pitch; ++i) p += hasBlackRight(p) ? 2 : 1;
for (int row = 0; row < rows; ++row) {
for (int key = 0; key < keys; ++key) {
pitches[row][key] = p;
p += hasBlackRight(p) ? 2 : 1;
}
}
if (inval) {
SharedPreferences.Editor editor =
PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
editor.putInt("piano_rows", rows);
editor.putInt("piano_keys", keys);
editor.putInt("piano_pitch", pitch);
editor.apply();
invalidate();
}
}
@Override
public void onSuccess(final AuthorizeResult authorizeResult) {
final String authorizationCode = authorizeResult.getAuthorizationCode();
final String redirectUri = authorizeResult.getRedirectURI();
final String clientId = authorizeResult.getClientId();
SharedPreferences.Editor preferences = Util.getPrefernces(myContext).edit();
preferences.putString("clientID",clientId);
preferences.apply();
RequestBody formBody = new FormBody.Builder()
.add("grant_type", "authorization_code")
.add("code", authorizationCode)
.add("redirect_uri", redirectUri)
.add("client_id", clientId)
.add("code_verifier", codeVerifier)
.build();
tokenHanlder.doPostRequest(formBody,TokenHandler.FirstMainActivityDoPostRequest);
}
public void save(Bundle bundle) {
Validate.notNull(bundle, "bundle");
SharedPreferences.Editor editor = cache.edit();
for (String key : bundle.keySet()) {
try {
serializeKey(key, bundle, editor);
} catch (JSONException e) {
// Error in the bundle. Don't store a partial cache.
Logger.log(
LoggingBehavior.CACHE,
Log.WARN,
TAG,
"Error processing value for key: '" + key + "' -- " + e);
// Bypass the commit and just return. This cancels the entire edit transaction
return;
}
}
editor.apply();
}
public static void deleteProxy(ProxyInfo proxyInfo)
{
if (currentProxy == proxyInfo)
{
currentProxy = null;
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
boolean enabled = preferences.getBoolean("proxy_enabled", false);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("proxy_ip", "");
editor.putString("proxy_pass", "");
editor.putString("proxy_user", "");
editor.putString("proxy_secret", "");
editor.putInt("proxy_port", 1080);
editor.putBoolean("proxy_enabled", false);
editor.putBoolean("proxy_enabled_calls", false);
editor.commit();
if (enabled)
{
ConnectionsManager.setProxySettings(false, "", 0, "", "", "");
}
}
proxyList.remove(proxyInfo);
saveProxyList();
}
public void onCheckedDefaultVideo(boolean isChecked) {
SharedPreferences.Editor editor = sharedPreferences.edit();
isDefaultPlayer.set(isChecked);
if (isChecked) {
editor.putInt(SHP_SETTING.KEY_DEFAULT_PLAYER, 1);
editor.apply();
} else {
editor.putInt(SHP_SETTING.KEY_DEFAULT_PLAYER, 0);
editor.apply();
}
}
private void upSourceSort(int sort) {
SharedPreferences.Editor editor = getPreferences().edit();
editor.putInt("SourceSort", sort);
editor.apply();
upSortMenu();
setDragEnable(sort);
mPresenter.refresh();
}
private void general() {
priceChart.setVisibility(View.INVISIBLE);
chartTitle.setText(TITLE_TEXTS[displayType]);
colorPadding.setBackgroundColor(getResources().getColor(R.color.colorPrimaryLittleDarker));
if (ac != null && ac.getPreferences() != null) {
SharedPreferences.Editor editor = ac.getPreferences().edit();
editor.putInt("displaytype_chart", displayType);
editor.apply();
}
}
@Override
public void save(Context context, SharedPreferences.Editor editor) {
Resources resources = context.getResources();
editor.putBoolean(resources.getString(R.string.prefKey_ADAS_FCW_DetectorStatus), mEnable);
editor.putBoolean(resources.getString(R.string.prefKey_ADAS_FCW_WarningAudio), mEnableWarningAudio);
editor.putBoolean(resources.getString(R.string.prefKey_ADAS_FCW_DetectorMode), mIsDeepLearningMode);
}
@Override
public void onPause() {
super.onPause();
if (mSearchTask != null)
mSearchTask.stop();
if (mFileName != null && mDocView != null) {
SharedPreferences prefs = fragActivity.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("page" + mFileName, mDocView.getDisplayedViewIndex());
edit.apply();
}
}
/**
* Notification - Should notifications be pushed while in quiet hours
*/
public void setNotificationsShowInQuietHours(boolean showInQuietHours) {
SharedPreferences.Editor editor = getEditor();
editor.putBoolean(this.NOTIFICATIONS_SHOW_IN_QUIET_HOURS, showInQuietHours);
editor.commit();
}
@Override
public void sortMethodSelected(int which) {
mCurrentSortIndex = which;
boolean downloadedOnly = mListener.shouldDisplayDownloadedOnly();
Cursor bookListCursor = getCursor(downloadedOnly);
bookListRecyclerViewAdapter.changeCursor(bookListCursor);
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(KEY_BOOKK_LIST_SORT_INDEX_ONLY, which);
editor.apply();
}
@Test
public void testGdprConsentIabUndefined() {
//given
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = pref.edit();
editor.remove(StorageUtils.IABConsent_ConsentStringKey);
editor.remove(StorageUtils.IABTCF_CONSENT_STRING);
editor.apply();
//when
String gdprConsent = StorageUtils.getPbGdprConsent();
//then
assertEquals(null, gdprConsent);
}
public static boolean setSelectedNearbyUserMinage(String min_age){
SharedPreferences.Editor editor = MyApplication.getContext().getSharedPreferences("selected_nearby_user", Context.MODE_PRIVATE).edit();
if(checkNull(min_age)) editor.putString("min_age",min_age);
editor.commit();
return true;
}
public static boolean setNickname(String nickname){
SharedPreferences.Editor editor = MyApplication.getContext().getSharedPreferences("user", Context.MODE_PRIVATE).edit();
if(checkNull(nickname)) editor.putString("nickname",nickname);
editor.commit();
return true;
}
@Override
public void onClick(View v) {
int sort_config=0;
SharedPreferences.Editor editor=settings.edit();
switch (v.getId()){
default:break;
case R.id.sort_ra_default:{
sort_config=0;
}
break;
case R.id.sort_ra_ascending_appname:{
sort_config=1;
}
break;
case R.id.sort_ra_descending_appname:{
sort_config=2;
}
break;
case R.id.sort_ra_ascending_appsize:{
sort_config=3;
}
break;
case R.id.sort_ra_descending_appsize:{
sort_config=4;
}
break;
case R.id.sort_ra_ascending_date:{
sort_config=5;
}
break;
case R.id.sort_ra_descending_date:{
sort_config=6;
}
break;
case R.id.sort_ra_ascending_install_date:{
sort_config=7;
}
break;
case R.id.sort_ra_descending_install_date:{
sort_config=8;
}
break;
case R.id.sort_ra_ascending_package_name:{
sort_config=9;
}
break;
case R.id.sort_ra_descending_package_name:{
sort_config=10;
}
break;
}
editor.putInt(Constants.PREFERENCE_SORT_CONFIG,sort_config);
editor.apply();
cancel();
if(callback!=null)callback.onOptionSelected(sort_config);
}
public static void put(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public void setAlbumGridSizeLand(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(ALBUM_GRID_SIZE_LAND, gridSize);
editor.apply();
}
private void refreshSpotifyToken()
{
try
{
URL apiUrl = new URL("https://accounts.spotify.com/api/token");
HttpsURLConnection urlConnection = (HttpsURLConnection) apiUrl.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
//write POST parameters
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
writer.write("grant_type=refresh_token&");
writer.write("refresh_token=" + SPOTIFY_REFRESH_TOKEN + "&");
writer.write("client_id=" + SPOTIFY_CLIENT_ID + "&");
writer.write("client_secret=" + "3166d3b40ff74582b03cb23d6701c297");
writer.flush();
writer.close();
out.close();
urlConnection.connect();
System.out.println("[BLADE] [AUTH-REFRESH] Result : " + urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage());
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String result = reader.readLine();
reader.close();
result = result.substring(1);
result = result.substring(0, result.length()-1);
String[] results = result.split(",");
for(String param : results)
{
if(param.startsWith("\"access_token\":\""))
{
param = param.replaceFirst("\"access_token\":\"", "");
param = param.replaceFirst("\"", "");
SPOTIFY_USER_TOKEN = param;
spotifyApi.setAccessToken(SPOTIFY_USER_TOKEN);
SharedPreferences pref = LibraryService.appContext.getSharedPreferences(SettingsActivity.PREFERENCES_ACCOUNT_FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("spotify_token", SPOTIFY_USER_TOKEN);
editor.commit();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setArtistGridSize(final int gridSize) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(ARTIST_GRID_SIZE, gridSize);
editor.apply();
}
@Override
public SharedPreferences.Editor putInt(String s, int i) {
putMap.put(s, i);
return this;
}