2016-07-15 70 views
0

我的問題是,如果地點事件監聽器(內部方法changestatus())在發生單擊事件後繼續執行,或者只調用一次。是繼續「運行」的方法

public void onClick(View v) { 
    if (b1.getText()=="Start" && name!=null) { 
     b1.setText("Stop"); 
     inte.barstatus(); 
     inte.respond("Trying to find buildings near you"); 
     changestatus(); 
     Toast.makeText(getActivity(), "You have to get close to another building.", Toast.LENGTH_LONG).show(); 
    } 
    else if (b1.getText()=="Start"){ 
     b1.setText("Stop"); 
     inte.barstatus(); 
     inte.respond("Trying to find buildings near you"); 
     changestatus(); 
    } 
    else{ 
    b1.setText("Start"); 
    inte.barstatus2(); 
    inte.respond("Press Start button to find buildings near you"); 
      } 
} 
public void changestatus(){ 
     placeEventListener = new PlaceEventListener() { 
      @Override 
      public void onVisitStart(Visit visit) { 
       super.onVisitStart(visit); 
       name = visit.getPlace().getName().toString(); 
       switch (name) { 
        case "test1": { 
         inte.respond(name.toString()); 
         inte.barstatus2(); 
         ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100); 
         toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
         break; 
        } 
        case "test2": { 
         inte.respond(name.toString()); 
         inte.barstatus2(); 
         ToneGenerator toneY = new ToneGenerator(AudioManager.STREAM_ALARM, 100); 
         toneY.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
         break; 
        } 
        case "Joylamp": { 
         inte.respond(name.toString()); 
         inte.barstatus2(); 
         ToneGenerator toneT = new ToneGenerator(AudioManager.STREAM_ALARM, 100); 
         toneT.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
         break; 
        } 
       } 
      } 
     }; 

............................................ ............ 我希望你能理解我的問題。 感謝

編輯 我忘了這部分代碼,這是內部changestatus()方法:

placeManager = PlaceManager.getInstance(); 
     placeManager.addListener(placeEventListener); 
     placeManager.startMonitoring(); 
     CommunicationManager.getInstance().startReceivingCommunications(); 

回答

0

您正在創建一個PlaceEventListener,但你不將其分配給PlaceManager(如placeManager.addListener(placeEventListener);

這樣,在方法changestatus()被執行並完成後,您的placeEventListener可以被垃圾收集器收集(因爲沒有人引用它)。

我相信你的placeEventListener永遠不會收到任何事件。

UPDATE

你更新你的問題......

現在,我們可以看到,placeEventListener意願仍然活着,並接收事件。因此,它會持續運行,直到調用placeManager.getInstance().removeListener()

如果不刪除,它,它就會一直運行的placeManager()仍然活着......所以,我建議,以提高你的代碼添加一些條件,停止監聽事件...類似於:

public class TestClass extends Activity { 
    private PlaceEventListener placeEventListener; 


    public void changestatus(){ 
     if(placeEventListener == null) { 
      placeEventListener = new PlaceEventListener() { 
       @Override 
       public void onVisitStart(Visit visit) { 
        super.onVisitStart(visit); 
        .... 
        ... 
       } 
      } 

      CommunicationManager placeManager = PlaceManager.getInstance(); 
      placeManager.addListener(placeEventListener); 
      placeManager.startMonitoring(); 
      placeManager.startReceivingCommunications(); 
     } 

    } 

    /* 
     Remember to add conditions to stop monitoring... 
     Otherwise, you listener will remains running even when you no longer need. 
     Also, placeManager is a static method.. so, it last longer than your activity/class... 
     If you don't remove your listener when your object/activity is being destroyed, you can induce memory leaks 

     So, if you add a listener, remember to remove it 
    */ 
    @Override 
    public void onDestroy() { 
     if(placeEventListener != null) { 
      CommunicationManager placeManager = PlaceManager.getInstance(); 
      placeManager.removeListener(placeEventListener); 
      placeManager.stopMonitoring(); 
      placeManager.stopReceivingCommunications(); 
     } 

     super.onDestroy(); 
    } 
} 
+0

....看到編輯請...是好的嗎? @Guilherme P –