当前位置:网站首页>Live video source code, realize local storage of search history
Live video source code, realize local storage of search history
2022-07-06 00:52:00 【Cloudleopard network technology】
Live video source , Enable local storage of search history
1、build.gradle:
dependencies {
api 'com.tencent:mmkv-static:1.2.10'
}
2、Application initialization :
//rootDir Is a local storage folder
String rootDir = MMKV.initialize(this);
MMKV kv = MMKV.mmkvWithID("search_history_id", MMKV.MULTI_PROCESS_MODE);
App.kv = kv;
//App It's a public class
public class App {
public static MMKV kv;
}
3、 preservation 、 Get history Util class Put what you used before sp Commented out If you want to use it, you can also refer to
public class SearchHistoryUtil {
// Save search history
public static void savaSearchWord(String keyword){
// String searchHistoryData = TeSharedPreferences.getInstance().getString("search_history");
String searchHistoryData = App.kv.decodeString("search_history");
if(searchHistoryData != null){
String[] tmpHistory = searchHistoryData.split(","); // Comma interception Save in array
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory)); // Convert the array to ArrayList
if (historyList.size() > 0) {
//1. Remove previously added elements
for (int i = 0; i < historyList.size(); i++) {
if (keyword.equals(historyList.get(i))) {
historyList.remove(i);
break;
}
}
historyList.add(0, keyword); // Add the newly entered text to the... Of the set 0 Bit is the front (2. In reverse order )
if (historyList.size() > 10) {
historyList.remove(historyList.size() - 1); //3. Save at most 10 Search records Delete the first search item
}
// Comma concatenation
StringBuilder sb = new StringBuilder();
for (int i = 0; i < historyList.size(); i++) {
sb.append(historyList.get(i) + ",");
}
// Save to sp
// TeSharedPreferences.getInstance().putString("search_history",sb.toString());
App.kv.encode("search_history", sb.toString());
} else {
// Not previously added
// TeSharedPreferences.getInstance().putString("search_history",keyword);
App.kv.encode("search_history", keyword);
}
}else{
App.kv.encode("search_history", keyword);
}
Log.e("search_history", " Search history :" + App.kv.decodeString("search_history"));
}
// Get search records
public static List<String> getSearchHistory() {
// String longHistory = TeSharedPreferences.getInstance().getString("search_history");
String longHistory = App.kv.decodeString("search_history");
if(longHistory != null){
String[] tmpHistory = longHistory.split(","); //split The rear length is 1 There is an empty string object
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));
if (historyList.size() == 1 && historyList.get(0).equals("")) {
// If there is no search record ,split Then the first 0 If the bit is an empty string
historyList.clear(); // Empty the set , This is the key
}
return historyList;
}
return new ArrayList<>();
}
4、sp Code
import android.content.Context;
import android.content.SharedPreferences;
import com.jumei.base.app.AppGlobalVar;
import java.util.HashMap;
import java.util.Map;
public class TeSharedPreferences {
private static final String APP_COMMON = "app_common";
public static final String APP_COOKIES = "app_cookies";
public static final String APP_INIT = "app_init";
private final SharedPreferences sharedPreferences;
private static final HashMap<String, TeSharedPreferences> preferencesManagerHashMap = new HashMap<>();
public static TeSharedPreferences getInstance() {
return getInstance(APP_COMMON);
}
public static TeSharedPreferences getInstance(String fileName) {
TeSharedPreferences instance = preferencesManagerHashMap.get(fileName);
if (instance == null) {
instance = new TeSharedPreferences(fileName);
preferencesManagerHashMap.put(fileName, instance);
}
return instance;
}
private TeSharedPreferences(String fileName) {
sharedPreferences = App.appContext.getSharedPreferences(fileName, Context.MODE_PRIVATE);
}
public void putString(String key, String value) {
sharedPreferences.edit().putString(key, value).apply();
}
public void putBoolean(String key, boolean value) {
sharedPreferences.edit().putBoolean(key, value).apply();
}
public void putInt(String key, int value) {
sharedPreferences.edit().putInt(key, value).apply();
}
public void putFloat(String key, float value) {
sharedPreferences.edit().putFloat(key, value).apply();
}
public String getString(String key) {
return sharedPreferences.getString(key, "");
}
public String getString(String key, String defValue) {
return sharedPreferences.getString(key, defValue);
}
public boolean getBoolean(String key, boolean defValue) {
return sharedPreferences.getBoolean(key, defValue);
}
public int getInt(String key, int defValue) {
return sharedPreferences.getInt(key, defValue);
}
public float getFloat(String key, float defValue) {
return sharedPreferences.getFloat(key, defValue);
}
public Map<String, ?> getAll() {
try {
return sharedPreferences.getAll();
} catch (Exception e) {
return null;
}
}
public void clear() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
That's all Live video source , Enable local storage of search history , More content welcome to follow the article
边栏推荐
- Folding and sinking sand -- weekly record of ETF
- 云导DNS和知识科普以及课堂笔记
- XML Configuration File
- [groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
- [groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
- 如何制作自己的機器人
- 看抖音直播Beyond演唱会有感
- Introduction of motor
- KDD 2022 | EEG AI helps diagnose epilepsy
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
猜你喜欢

I'm interested in watching Tiktok live beyond concert

Cannot resolve symbol error

Introduction of motor

The growth path of test / development programmers, the problem of thinking about the overall situation

免费的聊天机器人API

Arduino hexapod robot

After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?

Spark AQE

Free chat robot API

Keepalive component cache does not take effect
随机推荐
uniapp开发,打包成H5部署到服务器
[Online gadgets] a collection of online gadgets that will be used in the development process
Spark AQE
【第30天】给定一个整数 n ,求它的因数之和
MySQL storage engine
Dynamic programming -- linear DP
Cf:c. the third problem
2020.2.13
Reading notes of the beauty of programming
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
KDD 2022 | EEG AI helps diagnose epilepsy
Ffmpeg captures RTSP images for image analysis
devkit入门
NLP generation model 2017: Why are those in transformer
Spark获取DataFrame中列的方式--col,$,column,apply
Study diary: February 13, 2022
Interview must brush algorithm top101 backtracking article top34
KDD 2022 | 脑电AI助力癫痫疾病诊断