Code Monkey home page Code Monkey logo

xsharedpref's Introduction

XSharedPref,适用于多进程的SharedPreferences

SharedPreferences底层是操作xml文件。在多进程中,每个进程都有一份SharedPreferences,因此SharedPreferences不能直接在多进程通信。为了解决此问题,将SharedPreferences的操作放在一个单独的进程中,其他进程使用ContentProvider对它进行操作,XSharedPref由此产生。

两个部分

  • SharedPrefProvider 继承了ContentProvider,实现对SharedPreferences的基本操作。
  • XSharedPref SharedPreferences使用工具类,其Api类似SharedPreferences。主要实现了:
    • getString
    • getInt
    • getLong
    • getFloat
    • getBoolean
    • putString
    • putInt
    • putLong
    • putFloat
    • putBoolean
    • remove
    • clear
    • contains 等基本操作,您可以根据自己的实际情况进行扩展。

使用用法

  1. gradle配置 compile 'cn.droidlover:xsharedpref:1.0.0'

  2. AndroidManifest.xml配置ContentProvider

     <provider
            android:name=".SharedPrefProvider"
            android:authorities="cn.droidlover.xsharedpref.sp"
            android:process=":xSharedPref" />

    process配置进程名称

  3. 使用XSharedPref提供的api

     XSharedPref.putString(this, "github_name", "https://github.com/limedroid");
     String gitName = XSharedPref.getString(this, "github_name", null);

    其他的api操作也如上所述.

  

xsharedpref's People

Contributors

limedroid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.