public class SharedPreferencesUtil {
private static SharedPreferences preferences;
//需要初始化,可在application里初始化
public static void init(Context context) {
preferences = context.getSharedPreferences("video", Context.MODE_PRIVATE);
}
public static void save(String key, String value) {
preferences.edit()
.putString(key, value)
.apply();
}
public static String get(String key) {
return preferences.getString(key, null);
}
}
需要在清单文件中注册
android:name=".app.App"
/**
* 自定义application
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化
SharedPreferencesUtil.init(this);
}
}