当前位置:网站首页>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

原网站

版权声明
本文为[Cloudleopard network technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060050520978.html