Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Android] Activité implementant observer

    4 juillet 2012 à 16:49:14

    Bonjour à tous,

    je débute sous android et j'essaye de réaliser un centre de notification un peu comme sur IOS avec le NSNotification DefaultCenter.

    Seulement mon activité n'est pas notifié. Quelqu'un aurait une idée d'ou pourrais venir l'erreur ? Voici une partie de mon code.

    Le singleton qui stocke les notifications et les objets observable.
    public enum ObservingService {
    	SINGLETON;
    	
        private HashMap<String, Observable> observables;
            
        private ObservingService() {
            observables = new HashMap<String, Observable>();
        }
    
        public void addObserver(String notification, Observer observer) {
            Observable observable = observables.get(notification);
            if (observable==null) {
                observable = new Observable();
                observables.put(notification, observable);
            }
            observable.addObserver(observer);
        }
    
        public void removeObserver(String notification, Observer observer) {
            Observable observable = observables.get(notification);
            if (observable!=null) {         
                observable.deleteObserver(observer);
            }
        }       
    
        public void postNotification(String notification, Object object) {
            Observable observable = observables.get(notification);
            if (observable!=null) {
                if (object==null) {
                    System.out.println("notifyObservers");
                    observable.notifyObservers();
                } else {
                	System.out.println("notifyObservers+object");
                	System.out.println(observable.countObservers());
                    observable.notifyObservers(object);
                }
            } else {
            	System.out.println("WithoutnotifyObservers");
            }
        }
    }
    


    Mon activité qui implémente observer:
    public class GreiActivity extends Activity implements Observer {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            // ON OBSERVE TOUTES LES NOTIFICATION NOMME XMLManagerChannelUpdatedNotification 
            ObservingService.SINGLETON.addObserver(XMLManager.XMLManagerChannelUpdatedNotification, this);
            
            // LANCEMENT D'UNE REQUETE HTTP ASYNCHRONE, UNE FOIS ARRIVEE A TERME ELLE ENVOIT UNE NOTIFICATION NOMME XMLManagerChannelUpdatedNotification
            XMLManager xml = XMLManager.getInstance(getApplicationContext());   
            xml.getChannel("http://www.grei.be/webservices/1.2/getContent.php");        	           
        }
    
        // LA METHODE UPDATE QUI RECUPERE LES NOTIFICATIONS ENVOYE.
    	public void update(Observable observable, Object data) {
    		Log.v("debugYan", "methode update");
    		System.out.println("ICI");
    	}    
    }
    


    La méthode qui poste une notification:
    private void updateCache(String response, String url) {
        	Map<String, Object> data = XMLParser.dictionaryForXML(response);    
        	
        	if(data == null) {
        		data = new HashMap<String, Object>();
        	}
        	
        	mainCache.put(url, data);
            // ON ENVOI UNE NOTIFICATION NOMME XMLManagerChannelUpdatedNotification
            ObservingService.SINGLETON.postNotification(XMLManagerChannelUpdatedNotification, url);
      }
    


    Pas moyen d'avoir un résultat dans la fonction update de mon activité :'(

    Merci :)

    EDIT : Bon ... xD C'est parce que je n'appelais pas la methode setChanged() mais vu que j'utilise directement un objet observable, je ne savais pas comment l'appeler.

    J'ai réglé le soucis en crée une classe héritant de observable, comme suit :
    public class ObservableService extends Observable {
    
    	public void postNotification(Object object) {
    		 setChanged();
    		 
    		 if(object == null) {
    			 notifyObservers();
    		 } else {
    			 notifyObservers(object);
    		 }
    	}
    }
    


    Je sais pas si c'est propre mais pour le moment ca marche, si d'autres ont des idées, je suis preneur :)
    • Partager sur Facebook
    • Partager sur Twitter

    [Android] Activité implementant observer

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown