How can I share a SharedPreferences file across two different android apps?

后端 未结 2 481
傲寒
傲寒 2020-11-27 05:40

I\'ve been struggling with this for a while. Basically, I want to have two applications (which will always be installed together) share preferences, with one of them being j

2条回答
  •  青春惊慌失措
    2020-11-27 06:02

    It is better to set private mode for the file. App needs to be signed with same set of certificates to share this file.

    Set sharedUserId in both apps to be same.

    
    ....
    

    Get Context from other package:

    mContext = context.createPackageContext(
                            "com.example.otherapp",
                            Context.MODE_PRIVATE);
     mPrefs = mContext.getSharedPreferences("sameFileNameHere", Activity.MODE_PRIVATE);
    

    Get items as usual from SharedPreference. You can access it now.

提交回复
热议问题