当前位置:网站首页>视频直播源码,实现本地存储搜索历史记录
视频直播源码,实现本地存储搜索历史记录
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();
}
}
以上就是 视频直播源码,实现本地存储搜索历史记录,更多内容欢迎关注之后的文章
边栏推荐
- The population logic of the request to read product data on the sap Spartacus home page
- [simple implementation of file IO]
- 数据分析思维分析方法和业务知识——分析方法(三)
- logstash清除sincedb_path上传记录,重传日志数据
- 数据分析思维分析方法和业务知识——分析方法(二)
- 关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号
- 免费的聊天机器人API
- [groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
- 《编程之美》读书笔记
- 小程序容器可以发挥的价值
猜你喜欢

Introduction of motor

XML配置文件

免费的聊天机器人API

如何制作自己的機器人

BiShe - College Student Association Management System Based on SSM

Model analysis of establishment time and holding time
![Cf:h. maximum and [bit operation practice + K operations + maximum and]](/img/c2/9e58f18eec2ff92e164d8d156629cf.png)
Cf:h. maximum and [bit operation practice + K operations + maximum and]

Ffmpeg captures RTSP images for image analysis

Browser reflow and redraw

Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
随机推荐
Why can't mathematics give machine consciousness
Classic CTF topic about FTP protocol
Getting started with devkit
Analysis of the combination of small program technology advantages and industrial Internet
Yolov5、Pycharm、Anaconda环境安装
面试必刷算法TOP101之回溯篇 TOP34
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
esxi的安装和使用
curlpost-php
MCU realizes OTA online upgrade process through UART
STM32 key chattering elimination - entry state machine thinking
看抖音直播Beyond演唱会有感
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
devkit入门
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
OpenCV经典100题
【线上小工具】开发过程中会用到的线上小工具合集
激动人心,2022开放原子全球开源峰会报名火热开启
Model analysis of establishment time and holding time
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?