我制作了一个应用程序来测试直接启动。一项仅存储五个首选项和引导接收器的活动。
MainActivity.java
mSharedPreferences = getSharedPreferences("myapp_preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("pref1", "someStringPref1");
editor.putString("pref2", "someStringPref2");
editor.putString("pref3", "someStringPref3");
editor.putString("pref4", "someStringPref4");
editor.putString("pref5", "someStringPref5");
editor.commit();
final Map<String, ?> map = mSharedPreferences.getAll();
Log.d("BootReceiver", "map size: " + map.size());
Run Code Online (Sandbox Code Playgroud)
比引导接收器代码:
Context storageContext;
final Context deviceContext = context.createDeviceProtectedStorageContext();
if (!unlocked) {
if (!deviceContext.moveSharedPreferencesFrom(context, "myapp_preferences")) {
Log.w(TAG, "Failed to migrate shared preferences.");
}
storageContext = deviceContext;
} else {
if (!context.moveSharedPreferencesFrom(deviceContext, "myapp_preferences")) {
Log.w(TAG, "Failed to migrate shared preferences.");
}
storageContext = context;
}
mSharedPreferences = storageContext.getSharedPreferences("myapp_preferences", Context.MODE_PRIVATE); …Run Code Online (Sandbox Code Playgroud) android ×1