继续“运行”的方法

时间:2016-07-15 22:55:43

标签: android methods onclick

我的问题是,如果地方事件监听器(内部方法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;
                    }
                }
            }
        };

............................................... ........ 我希望你能理解我的问题。 感谢

EDIT 我忘记了代码的这一部分,它位于changestatus()方法中:

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

1 个答案:

答案 0 :(得分:0)

您正在创建PlaceEventListener,但未将其分配给PlaceManager(例如placeManager.addListener(placeEventListener);

这样,在执行并完成方法changestatus()之后,垃圾收集器可以收集placeEventListener(因为没有人引用它)。

我相信您的placeEventListener不会再收到任何活动。

<强>更新

您更新了问题...

现在,我们可以看到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();
    }
}