当前位置:网站首页>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
边栏推荐
- C language programming (Chapter 6 functions)
- An understanding of & array names
- Gartner发布2022-2023年八大网络安全趋势预测,零信任是起点,法规覆盖更广
- Getting started with devkit
- Arduino六足机器人
- For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
- MySQL storage engine
- 2022-02-13 work record -- PHP parsing rich text
- 看抖音直播Beyond演唱会有感
- golang mqtt/stomp/nats/amqp
猜你喜欢
程序员搞开源,读什么书最合适?
Recoverable fuse characteristic test
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
Cannot resolve symbol error
Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
测试/开发程序员的成长路线,全局思考问题的问题......
The inconsistency between the versions of dynamic library and static library will lead to bugs
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
Spark AQE
随机推荐
RAID disk redundancy queue
I'm interested in watching Tiktok live beyond concert
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
几百行代码实现一个 JSON 解析器
MCU realizes OTA online upgrade process through UART
devkit入门
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
Spark SQL空值Null,NaN判断和处理
Leetcode 44 Wildcard matching (2022.02.13)
The growth path of test / development programmers, the problem of thinking about the overall situation
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
curlpost-php
[simple implementation of file IO]
新手入门深度学习 | 3-6:优化器optimizers
vSphere实现虚拟机迁移
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
Extension and application of timestamp
How to make your own robot
Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
Spark获取DataFrame中列的方式--col,$,column,apply