Created
January 5, 2013 09:14
-
-
Save noxi515/4460694 to your computer and use it in GitHub Desktop.
イベント通知サンプル<超てけとー版>。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.noxi.carbuncle.sample; | |
/** | |
* イベント通知用インターフェース | |
*/ | |
public interface EventListener { | |
/** | |
* イベント通知 | |
* | |
* @param key イベントキー | |
* @param obj イベントオブジェクト | |
*/ | |
public void onEventNotify(String key, Object obj); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.noxi.carbuncle.sample; | |
import java.lang.ref.WeakReference; | |
import java.util.HashSet; | |
import java.util.Set; | |
public class EventWatcher implements EventListener { | |
private static EventWatcher sInstance; | |
public static synchronized EventWatcher getInstance() { | |
if (sInstance == null) { | |
sInstance = new EventWatcher(); | |
} | |
return sInstance; | |
} | |
/** | |
* 解放漏れのリークが嫌なのでとりあえずWeakReferenceにしておく | |
*/ | |
private final Set<WeakReference<EventListener>> mTargets = new HashSet<WeakReference<EventListener>>(); | |
private EventWatcher() { | |
} | |
public void addNotifyTarget(EventListener listener) { | |
if (listener == null) return; | |
synchronized (mTargets) { | |
mTargets.add(new WeakReference<EventListener>(listener)); | |
} | |
} | |
public void removeNotifyTarget(EventListener listener) { | |
if (listener == null) return; | |
synchronized (mTargets) { | |
for (WeakReference<EventListener> reference : mTargets) { | |
EventListener target = reference.get(); | |
if (target == listener) { | |
mTargets.remove(reference); | |
return; | |
} | |
} | |
} | |
} | |
@Override | |
public void onEventNotify(String key, Object obj) { | |
synchronized (mTargets) { | |
for (WeakReference<EventListener> reference : mTargets) { | |
EventListener target = reference.get(); | |
if (target != null) { | |
target.onEventNotify(key, obj); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.noxi.carbuncle.sample; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class SampleActivity extends Activity implements EventListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
EventWatcher watcher = EventWatcher.getInstance(); | |
watcher.addNotifyTarget(this); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
EventWatcher watcher = EventWatcher.getInstance(); | |
watcher.removeNotifyTarget(this); | |
} | |
@Override | |
public void onEventNotify(String key, Object obj) { | |
Log.d(key, obj.toString()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment