2. Articles Android - utilisez SharedPreferences pour encapsuler les outils de stockage local

problème:

Parfois, nous devons demander plusieurs fois une interface de requête. Bien que le résultat renvoyé soit le même à chaque fois, nous devons encore faire plusieurs demandes. À ce stade, nous pouvons enregistrer les données dans notre première requête. Dans le cache, les requêtes suivantes peut appeler directement les données qui n'ont pas expiré dans le cache. Ici, nous fournissons le code de l'outil:

package com.zjxf.utils;

import android.content.SharedPreferences;


public class SharedPrefUtils {

	private static SharedPrefUtils instance;
	private SharedPreferences sp;

	private SharedPrefUtils() {
		sp = BDApplication.getInstance().getSharedPreferences(
				BDApplication.getInstance().getPackageName(), 0);
	}

	public static SharedPrefUtils getInstance() {
		if (instance == null) {
			synchronized (SharedPrefUtils.class) {
				if (instance == null) {
					instance = new SharedPrefUtils();
				}
			}
		}
		return instance;
	}

	public void putStr2SP(String key, String value) {
		sp.edit().putString(key, value).commit();
	}

	public String getStrBykey(String key, String defValue) {
		return sp.getString(key, defValue);
	}

	public void putInt2SP(String key, Integer value) {
		sp.edit().putInt(key, value).commit();
	}

	public Integer getIntByKey(String key, Integer defValue) {
		return sp.getInt(key, defValue);
	}

	public void putFloat2SP(String key, Float value) {
		sp.edit().putFloat(key, value).commit();
	}

	public Float getFloatByKey(String key, Float defValue) {
		return sp.getFloat(key, defValue);
	}

	public void putLong2SP(String key, Long value) {
		sp.edit().putLong(key, value).commit();
	}

	public Long getLongByKey(String key, Long defValue) {
		return sp.getLong(key, defValue);
	}
	
	public void putBoolean2SP(String key, boolean value) {
		sp.edit().putBoolean(key, value).commit();
	}
	
	public Boolean getBooleanByKey(String key, boolean defValue) {
		return sp.getBoolean(key, defValue);
	}

	public void remove(String key) {
		try {
			sp.edit().remove(key).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
package com.zjxf.utils;

import android.app.Application;

public class BDApplication extends Application {

	private static BDApplication instance;
	
	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		CrashHandler.getInstance().init(this.getApplicationContext());
	}
	
	public static BDApplication getInstance(){
		return instance;
	}
}

La classe globale ci-dessus doit être définie dans AndroidManifest.xml

Ensuite, il peut être utilisé, l'utilisation est la suivante:

 SharedPrefUtils.getInstance().putStr2SP(CacheKeys.LOGIN_SESSION_Id, sessionId); //添加值
 String schoolName = SharedPrefUtils.getInstance().getStrBykey(CacheKeys.THIS_SCHOOL_NAME, StringUtil.EMPTY_STRING); //获取值

 

Je suppose que tu aimes

Origine blog.csdn.net/qq_38821574/article/details/113519406
conseillé
Classement