因爲app採用了多進程的方式,踩了SharedPreferences的坑。因此決定用ContentProvider來實現一個本身的SharedPreferences。 github項目地址html
描述問題:我在A進程中寫入UserId,在B進程中讀取UserId進行網絡訪問,有的時候會出現莫名其妙的問題,讀取的UserId不是null就是上一次的UserId。android
查閱官方文檔:Android在api11的時候加入MODE_MULTI_PROCESS來支持多進程,可是在api23的時候廢棄了這個flag,理由是在某些版本的android系統上,它沒法可靠的運行,並且對於跨進程的併發修改沒有提供任何機制來保證。應該使用ContentProvider來代替它進程多進程的數據修改。git
描述問題:我在A進程中反覆寫入SharedPreferences,在B進程中同時寫入同一個name的SharedPreferences,致使B進程沒法寫入。github
官方文檔上描述:對於跨進程的併發修改沒有提供任何機制來保證。api
這個我就再也不這裏寫了,網上有大神寫的很是詳細點擊這裏網絡
因爲之後要調整架構採用組件化,每一個組件須要讀寫一些app的公用信息例如登錄信息等,因此我作了擴展SharedProvider中除了基本信息還能夠保存對象和集合架構
sharedProvider.edit().putObject(KEY_6, articleK, ArticleK.class.getName()); sharedProvider.edit().putList(KEY_7, articleKList); sharedProvider.edit().putMap(KEY_8, kArticleVMap); sharedProvider.edit().putSet(KEY_9, articleKSet);
綜合以上問題因此決定用ContentProvider來實現一個本身的SharedPreferences。併發
項目地址點擊這裏app
<dependency> <groupId>com.andrjhf.sharedprovider</groupId> <artifactId>sharedprovider-library</artifactId> <version>1.0.2</version> <type>pom</type> </dependency>
compile 'com.andrjhf.sharedprovider:sharedprovider-library:1.0.2'
android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { minSdkVersion 9 targetSdkVersion 22 ... } }
在AndroidManifest.xml中增長以下方法,標準的provider *android:authorities="com.andrjhf.sharedprovider"*不能修改ide
<provider android:name="com.andrjhf.sharedprovider.PreferencesContentProvider" android:authorities="com.andrjhf.sharedprovider" android:enabled="true" android:exported="false"></provider>
public class MainActivity extends Activity { public static final String TAG = "MainActivity"; public static final String SHARED_NAME = "sharedName1"; public static final String SHARED_NAME2 = "sharedName2"; private static final String KEY_1 = "key1"; private static final String KEY_2 = "key2"; private static final String KEY_3 = "key3"; private static final String KEY_4 = "key4"; private static final String KEY_5 = "key5"; private static final String KEY_6 = "key6"; private static final String KEY_7 = "key7"; private static final String KEY_8 = "key8"; private static final String KEY_9 = "key9"; private static final String KEY_10 = "key10"; private static final String KEY_11 = "key11"; private static final String KEY_12 = "key12"; private static final String KEY_13 = "key13"; private SharedProvider sharedProvider; private SharedProvider sharedProvider2; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<ArticleK> articleKList = new ArrayList<>(); Map<ArticleK, ArticleV> kArticleVMap = new HashMap<>(); Set<ArticleK> articleKSet = new HashSet<>(); ArticleK articleK = new ArticleK(); articleK.setTitle("標題"); articleK.setDesc("我是一個簡介"); articleK.setImg("http://www.baidu.com/"); articleK.setUrl("http://www.google.com/"); articleKList.add(articleK); articleK = new ArticleK(); articleK.setTitle("標題11111"); articleK.setDesc("我是一個簡介11111"); articleK.setImg("http://www.baidu.com/11111"); articleK.setUrl("http://www.google.com/11111"); articleKList.add(articleK); ArticleV articleV = new ArticleV(); articleK.setTitle("標題vvvvv"); articleK.setDesc("我是一個簡介vvvvv"); articleK.setImg("http://www.baidu.com/vvvvv"); articleK.setUrl("http://www.google.com/vvvvv"); kArticleVMap.put(articleK, articleV); articleKSet.add(articleK); sharedProvider = new SharedProviderImpl(getApplicationContext(), SHARED_NAME); sharedProvider.edit().putString(KEY_1, "value1"); sharedProvider.edit().putInt(KEY_2, 100); sharedProvider.edit().putLong(KEY_3, 100000000L); sharedProvider.edit().putFloat(KEY_4, 66.66F); sharedProvider.edit().putBoolean(KEY_5, false); sharedProvider.edit().putObject(KEY_6, articleK, ArticleK.class.getName()); sharedProvider.edit().putList(KEY_7, articleKList); sharedProvider.edit().putMap(KEY_8, kArticleVMap); sharedProvider.edit().putSet(KEY_9, articleKSet); sharedProvider.edit().putString(KEY_10, "value10"); sharedProvider.edit().remove(KEY_10); // sharedProvider.edit().clear(); sharedProvider2 = new SharedProviderImpl(getApplicationContext(), SHARED_NAME2); sharedProvider2.edit().putString(KEY_1, "value1"); sharedProvider2.edit().putInt(KEY_2, 100); sharedProvider2.edit().putLong(KEY_3, 100000000L); sharedProvider2.edit().putFloat(KEY_4, 66.66F); sharedProvider2.edit().putBoolean(KEY_5, true); sharedProvider2.edit().putObject(KEY_6, articleK, ArticleK.class.getName()); sharedProvider2.edit().putList(KEY_7, articleKList); sharedProvider2.edit().putMap(KEY_8, kArticleVMap); sharedProvider2.edit().putSet(KEY_9, articleKSet); sharedProvider2.edit().putString(KEY_10, "value10"); Log.e(TAG, KEY_1 + " : " + sharedProvider2.getString(KEY_1, "default_key_1")); Log.e(TAG, KEY_2 + " : " + sharedProvider2.getInt(KEY_2, -100)); Log.e(TAG, KEY_3 + " : " + sharedProvider2.getLong(KEY_3, -1000L)); Log.e(TAG, KEY_4 + " : " + sharedProvider2.getFloat(KEY_4, -55.55F)); Log.e(TAG, KEY_5 + " : " + sharedProvider2.getBoolean(KEY_5, false)); Log.e(TAG, KEY_6 + " : " + sharedProvider2.getObject(KEY_6, ArticleK.class.getName())); Log.e(TAG, KEY_7 + " : " + sharedProvider2.getList(KEY_7)); Log.e(TAG, KEY_8 + " : " + sharedProvider2.getMap(KEY_8)); Log.e(TAG, KEY_9 + " : " + sharedProvider2.getSet(KEY_9)); Log.e(TAG, KEY_10 + " : " + sharedProvider2.getString(KEY_10, "default10")); Log.e(TAG, "ALL" + " : " + sharedProvider2.getAll()); //若是獲取類型錯誤會返回默認值,沒有類型轉換 Log.e(TAG, "Default " + KEY_2 + " : " + sharedProvider2.getLong(KEY_2, -1000L)); Log.e(TAG, "Default " + KEY_3 + " : " + sharedProvider2.getInt(KEY_3, -100)); sharedProvider2.contains(KEY_1); // Intent intent = new Intent(MainActivity.this, MyService.class); // startService(intent); // new Thread(new Runnable() { // @Override // public void run() { // while (true) { // try { // TimeUnit.MILLISECONDS.sleep(500); // } catch (InterruptedException e) { // e.printStackTrace(); // } // int keyInt7 = sharedProvider.getInt(KEY_7, -1); // Log.e(TAG, KEY_7 + " : " + keyInt7); //// sharedProvider.edit().putInt(KEY_7, count++); // } // } // }).start(); new Thread(new Runnable() { @Override public void run() { String string = getFromAssets("test.txt"); SharedPreferences sharedPreferences = getSharedPreferences("app", Context.MODE_PRIVATE); long start = System.currentTimeMillis(); // sharedPreferences.edit().putString(string, "value").commit(); // Log.e(TAG, "set preferences end - start = " + (System.currentTimeMillis() - start)); // // start = System.currentTimeMillis(); // sharedProvider2.edit().putString(string, "value"); // Log.e(TAG, "set provider end - start = " + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); sharedPreferences.edit().putString("key", "1111111111111111111111111111111").commit(); Log.e(TAG, "set preferences end - start = " + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); sharedProvider2.edit().putString("key", "1111111111111111111111111111111"); Log.e(TAG, "set provider end - start = " + (System.currentTimeMillis() - start)); // start = System.currentTimeMillis(); // String valuepre = sharedPreferences.getString("key", ""); // Log.e(TAG, "get preferences end - start = " + (System.currentTimeMillis() - start)); // // start = System.currentTimeMillis(); // valuepre = sharedProvider2.getString("key", ""); // Log.e(TAG, "get provider end - start = " + (System.currentTimeMillis() - start)); } }).start(); } public void onClick(View view) { sharedProvider.edit().putString(KEY_8, "sdfsdfsdfsdf"); Toast.makeText(this, KEY_8, Toast.LENGTH_SHORT).show(); } public String getFromAssets(String fileName) { try { InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName)); BufferedReader bufReader = new BufferedReader(inputReader); String line = ""; String Result = ""; while ((line = bufReader.readLine()) != null) Result += line; return Result; } catch (Exception e) { e.printStackTrace(); } return null; } }
ContentProvider多進程訪問的時候有時會致使Client端進程被kill。
感謝@Shawn_Dut提出的問題。
例子:Service進程在寫數據,Client進程在讀取數據,若是Service進程因爲各類緣由掛掉了,Client進程有可能被kill,這個問題沒法規避。 具體緣由請點擊這裏QQ音樂技術團隊的帖子。