当前位置:网站首页>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
边栏推荐
- Classic CTF topic about FTP protocol
- 激动人心,2022开放原子全球开源峰会报名火热开启
- MCU通过UART实现OTA在线升级流程
- Kotlin core programming - algebraic data types and pattern matching (3)
- Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
- 从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
- DD's command
- Introduction of motor
- Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
- How spark gets columns in dataframe --column, $, column, apply
猜你喜欢

Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike

MYSQL GROUP_ The concat function realizes the content merging of the same ID

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

Beginner redis

ubantu 查看cudnn和cuda的版本

anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败

MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统

cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】

Starting from 1.5, build a micro Service Framework - call chain tracking traceid

Opencv classic 100 questions
随机推荐
【线上小工具】开发过程中会用到的线上小工具合集
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
Free chat robot API
Fibonacci number
云导DNS和知识科普以及课堂笔记
Opencv classic 100 questions
RAID disk redundancy queue
Problems and solutions of converting date into specified string in date class
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
How to use the flutter framework to develop and run small programs
Ffmpeg captures RTSP images for image analysis
2020.2.13
Common API classes and exception systems
Extension and application of timestamp
XML Configuration File
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Leetcode Fibonacci sequence
synchronized 和 ReentrantLock
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
免费的聊天机器人API