public class PreferenceManager{ private PreferenceManager mInstance; private List<PreferenceListener> mListenerList = new ArrayList<>(PreferenceListener); private Preference(){ } //單例模式 public static PreferenceManager getInstance{ if(mInstance == null){ mInstance = new Preference(); } } //通知全部的 public void notifyPreferenceChanged(String changedKeyHint) { if (changedKeyHint == null) { return; } for (PreferenceListener listener : mListenerList) { listener.onPreferenceChanged(changedKeyHint); } } public void addPreferenceListener(PreferenceListener listener) { if (!mListenerList.contains(listener)) { mListenerList.add(listener); } } public void removePrerenceListener(PreferenceListener listener) { mListenerList.remove(listener); } public interface Preference(){ public void onPreferenceChanged(String changedKeyHint); } }
public class A implements PreferenceListener{ PreferenceManager.getInstance().addPreferenceListener(); @override public void onPreferenceChanged(String changedKeyHint){ } }