Giter Site home page Giter Site logo

chapter12's Introduction

Chapter12

今天我们的示例是如何替换系统SharedPreferences的实现,实现这个主要为了替换一些第三方库的实现。

如果是自己应用内部的代码,还是建议直接使用类似MMKV等其他方案。

实现提示

替换Application的getSharedPreferences方法:

public class MyApplication extends Application {
  @Override
  public SharedPreferences getSharedPreferences(String name, int mode)
  {
     return SharedPreferencesImpl.getSharedPreferences(name, mode);
  }
}

记得还需要替换Activity的getSharedPreferences方法:

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
    return this.getApplicationContext().getSharedPreferences(name, mode);
}

核心优化

我们可以将项目中的SharedPreferencesImpl与系统的SharedPreferencesImpl做比较。

可以发现大部分代码都是一致的,核心的优化在于修改了apply的实现,将多个apply方法在内存中合并,而不是多次提交。

事实上这个只是一个简单的示例,通过自定义SharedPreferences实现,我们可以替换它的存储结构、增加加密等其他自定义功能。

chapter12's People

Contributors

shwenzhang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.