Use SharedPreferences on multi-process mode

前端 未结 7 889
不思量自难忘°
不思量自难忘° 2020-12-13 14:50

I\'ve defined an instance of SharedPreferences that used on multi-process mode.

public class Prefs {

    private static SharedPreferences pref         


        
7条回答
  •  無奈伤痛
    2020-12-13 15:15

    I've worked around this by combining:

    • Providing each process mutually-exclusive access to the SharedPreferences file (such as by using a socket-based locking mechanism)
    • Re-initialising the SharedPreferences with the MODE_MULTI_PROCESS flag every time you want to use it to bypass in-memory caching

    This seems to work OK, but it hasn't been thoroughly tested in the real world, so I don't know if it's perfectly reliable.

    You can see a working example I wrote here.

    Warning: Looks like MODE_MULTI_PROCESS has been deprecated in Android M. It might stop working in the future.

提交回复
热议问题