注:本内容翻译于《android应用性能优化》英文版
SharedPreferences异步加载
使用场景:用户保存基本资料
public class SharedPreferencesUtils { private static final Method sApplyMethod = findApplyMethod(); private static Method findApplyMethod() { // TODO Auto-generated method stub Class cls = SharedPreferences.Editor.class; try { return cls.getMethod("apply"); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void apply(SharedPreferences.Editor editor){ if(sApplyMethod!=null){ try { sApplyMethod.invoke(editor); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } editor.commit(); } }