androidlistenermqtt

How Can I Code Custom Variable Change Listener With Mqtt


Mqtt Client send data every 2 second to me. I can get data that's no problem. I have Global Class, when I get data, I set the class's variables. I want to use this data on my other class so I use that method for access data :

handler = new Handler();
runnable = new RefreshData(handler, ...);
handler.postDelayed(runnable, 2000);
public RefreshData(...) {
    ...
    }
    private class RefreshData implements Runnable {
    @Override
    public void run() {
        GlobalClass globalClass = (GlobalClass) getApplicationContext();
            this.lat = globalClass.getLat();
            this.lng = globalClass.getLng();
            ... }
    }

And that codes about how can I get the data :

public class MqttMessageService extends Service {   
private MqttAndroidClient mqttAndroidClient;
public MqttMessageService() {
}

@Override
public void onCreate() {
        mqttAndroidClient.setCallback(new MqttCallbackExtended() {
        @Override
        public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            GlobalClass globalClass = (GlobalClass) getApplicationContext();
            globalClass.setMessage(mqttMessage.toString());
        }
...}

So I want code a variable change listener for mqttMessage 'cause that method is -RefreshData- working but that's not useful. I didn't code and use any custom listener and I haven't so much experience about that. How can I code it?


Solution

  • I found the solution, I created global class and set listener the class.