├── Readme.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── java │ └── cn │ │ └── droidlover │ │ └── xsharedpref │ │ └── demo │ │ ├── MainActivity.java │ │ └── SecondActivity.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── activity_second.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── library ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── droidlover │ │ └── xsharedpref │ │ ├── SharedPrefProvider.java │ │ └── XSharedPref.java │ └── res │ └── values │ └── strings.xml └── settings.gradle /Readme.md: -------------------------------------------------------------------------------- 1 | # XSharedPref,适用于多进程的SharedPreferences 2 | 3 | SharedPreferences底层是操作xml文件。在多进程中,每个进程都有一份SharedPreferences,因此SharedPreferences不能直接在多进程通信。为了解决此问题,将SharedPreferences的操作放在一个单独的进程中,其他进程使用ContentProvider对它进行操作,[XSharedPref](https://github.com/limedroid/XSharedPref)由此产生。 4 | 5 | ## 两个部分 6 | 7 | * SharedPrefProvider 继承了ContentProvider,实现对SharedPreferences的基本操作。 8 | * XSharedPref SharedPreferences使用工具类,其Api类似SharedPreferences。主要实现了: 9 | * getString 10 | * getInt 11 | * getLong 12 | * getFloat 13 | * getBoolean 14 | * putString 15 | * putInt 16 | * putLong 17 | * putFloat 18 | * putBoolean 19 | * remove 20 | * clear 21 | * contains 22 | 等基本操作,您可以根据自己的实际情况进行扩展。 23 | 24 | ## 使用用法 25 | 26 | 1. gradle配置 27 | compile 'cn.droidlover:xsharedpref:1.0.0' 28 | 29 | 2. AndroidManifest.xml配置ContentProvider 30 | ```xml 31 | 35 | ``` 36 | process配置进程名称 37 | 38 | 3. 使用XSharedPref提供的api 39 | ```java 40 | XSharedPref.putString(this, "github_name", "https://github.com/limedroid"); 41 | String gitName = XSharedPref.getString(this, "github_name", null); 42 | ``` 43 | 其他的api操作也如上所述. 44 | 45 | 46 | 47 | 48 |    49 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | defaultConfig { 7 | applicationId "cn.droidlover.xsharedpref.demo" 8 | minSdkVersion 15 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile project(':library') 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wanglei/DevTools/Android/SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/java/cn/droidlover/xsharedpref/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.droidlover.xsharedpref.demo; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import cn.droidlover.xsharedpref.XSharedPref; 10 | 11 | public class MainActivity extends Activity { 12 | 13 | Button bt_loadUi; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | 20 | bt_loadUi = (Button) findViewById(R.id.bt_loadUi); 21 | bt_loadUi.setOnClickListener(new View.OnClickListener() { 22 | @Override 23 | public void onClick(View v) { 24 | startActivity(new Intent(MainActivity.this, SecondActivity.class)); 25 | } 26 | }); 27 | 28 | XSharedPref.putString(this, "user_name", "https://github.com/limedroid"); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/cn/droidlover/xsharedpref/demo/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package cn.droidlover.xsharedpref.demo; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | 7 | import cn.droidlover.xsharedpref.XSharedPref; 8 | 9 | public class SecondActivity extends Activity { 10 | 11 | TextView tv_val; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_second); 17 | 18 | tv_val = (TextView) findViewById(R.id.tv_val); 19 | 20 | String spValue = XSharedPref.getString(this, "user_name", null); 21 | tv_val.setText(spValue); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 14 | 15 |