Home Stopwatch on Android and CloudMQTT
Reply: 0

Stopwatch on Android and CloudMQTT

Rio Ikhtiar
1#
Rio Ikhtiar Published in 2018-01-13 00:06:50Z

I'm currently stuck at using some stopwatch code to my app,
here's my problem :
CloudMQTT is sending 1 and 0 to my stopwatch app, 1 to start the count and 0 to stop the count,

what i want is when the mqtt keep sending 1, the counter is not restarted as my app did but keep counting until 0 is received

here's my code

private void startMqtt() {
    MqttHelper mqttHelper = new MqttHelper(getApplicationContext());
    mqttHelper.setCallback(new MqttCallbackExtended() {
        @Override
        public void connectComplete(boolean b, String s) {

        }

        @Override
        public void connectionLost(Throwable throwable) {

        }

        @Override
        public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
            Log.w("Debug",mqttMessage.toString());
            dataReceived.setText(mqttMessage.toString());
            if (mqttMessage.toString().equals("1"))
            {
                condition = 1;
                do{
                    condition=1;
                    StartTime = SystemClock.uptimeMillis();
                    handler.postDelayed(runnable, 0);
                } while (condition!=1);
            }
            else if (mqttMessage.toString().equals("0"))
            {
                condition= 0;
                do{
                    condition= 0;

                    ListElementsArrayList.add(textView.getText().toString() + "  STATUS GUNUNG : " + statusHistory);

                    adapter.notifyDataSetChanged();

                }while (condition!=0);
            }
        }
        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        }

    });
}

here's my runnable

  public Runnable runnable = new Runnable() {

    public void run() {

        MillisecondTime = SystemClock.uptimeMillis() - StartTime;

        UpdateTime = TimeBuff + MillisecondTime;

        Seconds = (int) (UpdateTime / 1000);

        Minutes = Seconds / 60;

        Seconds = Seconds % 60;

        MilliSeconds = (int) (UpdateTime % 1000);

        textView.setText("" + Minutes + ":"
                + String.format("%02d", Seconds) + ":"
                + String.format("%03d", MilliSeconds));

        handler.postDelayed(this, 0);
    }

};
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303355 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO