private void init(Context context) {
mContext = context;
//设置默认的SharedPreferencesName
setSharedPreferencesName(getDefaultSharedPreferencesName(context));
}
//我们一般用这个获取default的sp
public static SharedPreferences getDefaultSharedPreferences(Context context) {
return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
getDefaultSharedPreferencesMode());
}
//其实就是application 的packge name啦
private static String getDefaultSharedPreferencesName(Context context) {
return context.getPackageName() + "_preferences";
}
private static int getDefaultSharedPreferencesMode() {
return Context.MODE_PRIVATE;
}
//PreferenceScreen 的处理
public PreferenceScreen inflateFromResource(Context context, int resId,
PreferenceScreen rootPreferences) {
// Block commits
setNoCommit(true);
final PreferenceInflater inflater = new PreferenceInflater(context, this);
rootPreferences = (PreferenceScreen) inflater.inflate(resId, rootPreferences, true);
rootPreferences.onAttachedToHierarchy(this);
// Unblock commits
setNoCommit(false);
return rootPreferences;
}
//剩下的就是一些观察者的实现了.在Activity stop的时候, 广播给观察者. 我擦, 看到这里发现这个类太简单了. 没什么值得研究的.
void dispatchActivityStop() {
List<OnActivityStopListener> list;
synchronized (this) {
if (mActivityStopListeners == null) return;
list = new ArrayList<OnActivityStopListener>(mActivityStopListeners);
}
final int N = list.size();
for (int i = 0; i < N; i++) {
list.get(i).onActivityStop();
}
}
public interface OnActivityStopListener {
/**
* See Activity's onStop.
*/
void onActivityStop();
}
网友评论