当前位置:网站首页>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
边栏推荐
- 数据分析思维分析方法和业务知识——分析方法(三)
- Analysis of the combination of small program technology advantages and industrial Internet
- [groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
- 几百行代码实现一个 JSON 解析器
- Programmer growth Chapter 9: precautions in real projects
- [groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
- 2020.2.13
- Set data real-time update during MDK debug
- 小程序容器可以发挥的价值
- Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
猜你喜欢

MCU通过UART实现OTA在线升级流程

Idea remotely submits spark tasks to the yarn cluster

Uniapp development, packaged as H5 and deployed to the server

数据分析思维分析方法和业务知识——分析方法(三)

Building core knowledge points

Arduino hexapod robot

【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)

I'm interested in watching Tiktok live beyond concert
![[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)](/img/dd/bffe27b04d830d70f30df95a82b3d2.jpg)
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
![[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)](/img/e4/a41fe26efe389351780b322917d721.jpg)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
随机推荐
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
如何制作自己的机器人
Dynamic programming -- linear DP
Building core knowledge points
Folding and sinking sand -- weekly record of ETF
Cf:c. the third problem
The population logic of the request to read product data on the sap Spartacus home page
Leetcode Fibonacci sequence
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
logstash清除sincedb_path上传记录,重传日志数据
Synchronized and reentrantlock
直播系统代码,自定义软键盘样式:字母、数字、标点三种切换
毕设-基于SSM高校学生社团管理系统
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
How spark gets columns in dataframe --column, $, column, apply
Introduction of motor
Idea remotely submits spark tasks to the yarn cluster
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
Cloud guide DNS, knowledge popularization and classroom notes