Coder Social home page Coder Social logo

interprocesssharedpreferences's Introduction

#跨进程的SharedPreferences 解决android中的SharedPreferences不能跨进程读写的问题.

用法

  • Android Studio

    选择添加:

     compile project(':interprocesssharedpreferences')
    

    或者

    compile 'jone.common.android.data.sharedPreferences:interprocesssharedpreferences:1.0.0'
    
    如果获取不到,则加入 maven { url 'http://dl.bintray.com/sunjoner7/maven' }
    

    在AndroidManifest.xml注册provider

    <!--authorities 规则:应用的包名 + ".InterProcessContentProvider"-->
    <provider
        android:name="jone.common.android.data.sharedPreferences.InterProcessContentProvider"
        android:authorities="jone.common.android.data.sharedPreferences.sample.InterProcessContentProvider"
        android:enabled="true"
        android:exported="true" />
    
  • Eclipse

    自行copy源码。

##用法示例

普通读写

InterProcessSharedPreferences interProcessSharedPreferences = InterProcessSharedPreferences.getInstance(getApplication());
interProcessSharedPreferences.putString("testStr", edit_value.getText().toString()); //写入
interProcessSharedPreferences.getString("testStr", "empty"); //读取
interProcessSharedPreferences.remove("testStr"); //删除

监听

 InterProcessSharedPreferences interProcessSharedPreferences = InterProcessSharedPreferences.getInstance(getApplication());
 
 ISharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener = new ISharedPreferences.OnSharedPreferenceChangeListener() {
         @Override
         public void onSharedPreferenceChanged(ISharedPreferences sharedPreferences, String key) {
             Log.e(TAG, "interProcessSharedPreferences--onSharedPreferenceChanged>>key: " + key + " value: " + sharedPreferences.getString(key, "empty"));
         }
     };
     
 //监听(onCreate)
 interProcessSharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener); 
 
 //取消监听(onDestroy 不需要监听时一定要取消监听)
 interProcessSharedPreferences.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);

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.