当前位置:网站首页>视频直播源码,实现本地存储搜索历史记录
视频直播源码,实现本地存储搜索历史记录
2022-07-06 00:51:00 【云豹网络科技】
视频直播源码,实现本地存储搜索历史记录
1、build.gradle:
dependencies {
api 'com.tencent:mmkv-static:1.2.10'
}
2、Application初始化:
//rootDir是本地存储文件夹
String rootDir = MMKV.initialize(this);
MMKV kv = MMKV.mmkvWithID("search_history_id", MMKV.MULTI_PROCESS_MODE);
App.kv = kv;
//App是一个公共类
public class App {
public static MMKV kv;
}
3、 保存、获取历史记录Util类 把之前用的sp注释掉了 如果想用也可以参考
public class SearchHistoryUtil {
//保存搜索历史记录
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(","); //逗号截取 保存在数组中
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory)); //将该数组转换成ArrayList
if (historyList.size() > 0) {
//1.移除之前重复添加的元素
for (int i = 0; i < historyList.size(); i++) {
if (keyword.equals(historyList.get(i))) {
historyList.remove(i);
break;
}
}
historyList.add(0, keyword); //将新输入的文字添加集合的第0位也就是最前面(2.倒序)
if (historyList.size() > 10) {
historyList.remove(historyList.size() - 1); //3.最多保存10条搜索记录 删除最早搜索的那一项
}
//逗号拼接
StringBuilder sb = new StringBuilder();
for (int i = 0; i < historyList.size(); i++) {
sb.append(historyList.get(i) + ",");
}
//保存到sp
// TeSharedPreferences.getInstance().putString("search_history",sb.toString());
App.kv.encode("search_history", sb.toString());
} else {
//之前未添加过
// TeSharedPreferences.getInstance().putString("search_history",keyword);
App.kv.encode("search_history", keyword);
}
}else{
App.kv.encode("search_history", keyword);
}
Log.e("search_history", "搜索历史记录:" + App.kv.decodeString("search_history"));
}
//获取搜索记录
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后长度为1有一个空串对象
List<String> historyList = new ArrayList<String>(Arrays.asList(tmpHistory));
if (historyList.size() == 1 && historyList.get(0).equals("")) {
//如果没有搜索记录,split之后第0位是个空串的情况下
historyList.clear(); //清空集合,这个很关键
}
return historyList;
}
return new ArrayList<>();
}
4、sp代码
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();
}
}
以上就是 视频直播源码,实现本地存储搜索历史记录,更多内容欢迎关注之后的文章
边栏推荐
- Pointer pointer array, array pointer
- [groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
- 如何制作自己的机器人
- Study diary: February 13, 2022
- XML配置文件
- Questions about database: (5) query the barcode, location and reader number of each book in the inventory table
- VSphere implements virtual machine migration
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
- 程序员成长第九篇:真实项目中的注意事项
- C language programming (Chapter 6 functions)
猜你喜欢

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

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

The relationship between FPGA internal hardware structure and code

小程序技术优势与产业互联网相结合的分析

uniapp开发,打包成H5部署到服务器

如何制作自己的机器人

Questions about database: (5) query the barcode, location and reader number of each book in the inventory table

MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning

Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates

XML Configuration File
随机推荐
Leetcode Fibonacci sequence
小程序技术优势与产业互联网相结合的分析
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Free chat robot API
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
MCU通过UART实现OTA在线升级流程
Cloud guide DNS, knowledge popularization and classroom notes
Curlpost PHP
新手入门深度学习 | 3-6:优化器optimizers
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
esxi的安装和使用
synchronized 和 ReentrantLock
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
XML Configuration File
Kotlin core programming - algebraic data types and pattern matching (3)
可恢复保险丝特性测试
Cannot resolve symbol error
Ffmpeg captures RTSP images for image analysis