The storage location of the SharePreference established by createDeviceProtectedStorageContext

For the introduction of Direct Boot Mode [DBM], please refer to the following article
http://blog.csdn.net/huluboy/article/details/53397582

The SharePreference of general applications is stored in the /data/data/[package name]/shared_prefs directory, but through The SharePreference created by createDeviceProtectedStorageContext is stored in the /data/user_de/0/[package name]/shared_prefs directory.

When we can't find where SharePreference is saved, we can get its path through this method
final File source = deviceContext.getSharedPreferencesPath(PreferenceManager.getDefaultSharedPreferencesName(deviceContext));
String absolutePath = source.getAbsolutePath();


An example of use is DeskClock
http://androidxref.com/7.1.1_r6/xref/packages/apps/DeskClock/src/com/android/deskclock/Utils.java

/**
* Return the default shared preferences.
*/
public static SharedPreferences getDefaultSharedPreferences(Context context) {
    final Context storageContext;
    if (isNOrLater()) {
        / All N devices have split storage areas, but we may need to
        // migrate existing preferences into the new device protected
        // storage area, which is where our data lives from now on.
        final Context deviceContext = context.createDeviceProtectedStorageContext();
            if (!deviceContext.moveSharedPreferencesFrom(context,
                    PreferenceManager.getDefaultSharedPreferencesName(context))) {
                LogUtils.wtf("Failed to migrate shared preferences");
            }
           storageContext = deviceContext;
        } else {
            storageContext = context;
        }

        return PreferenceManager.getDefaultSharedPreferences(storageContext);
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071604&siteId=291194637