Ca y'est je crois que j'ai compris la question....
Je pense qu'il y a un problème de conception, parce que normalement tu n'as pas besoin de faire cela...
Si tu veux vraiment faire comme cela, voici un exemple d'implémentation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
class MyReceiver extends BroadcastReceiver
{
public static interface Listener
{
public void doSomething(...);
}
static WeakReference<MyReceiver> _instance = null;
public MyReceiver() {
super();
_instance = new WeakReference<MyReceiver>(this);
}
public static synchronized MyReceiver getInstance()
{
MyReceiver ret = (_instance == null) ? null : _instance.get();
return ret;
}
HashSet<Listener> listeners = new HashSet<Listener>();
public static void register(Listener l)
{
MyReceiver rec = getInstance();
if (rec != null) {
syncrhonized (rec) {
rec.listeners.put(l);
}
}
}
public static void unregister(Listener l)
{
MyReceiver rec = getInstance();
if (rec != null) {
syncrhonized (rec) {
rec.listeners.remove(l);
}
}
}
public void onReceive(....)
{
for (Listener l : this.listeners)
l.doSomething();
}
} |
Mais bon, c'est encore une fois un peu tiré par les cheveux...
Partager