Help with understanding the issue.

Have MainActivity :

package ru.homeinn.iottank; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private ConnectionMqtt connection = new ConnectionMqtt(); private Button mbtnmqtt; public TextView mtext_mqtt; private String NewString; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } public void onSettingsMenuClick(MenuItem item) { Intent intent = new Intent(MainActivity.this, ConfActivity.class); startActivity(intent); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connection.connect(); mbtnmqtt = (Button) findViewById(R.id.btnmqtt); mtext_mqtt = (TextView) findViewById(R.id.info_text); mbtnmqtt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("Onclick"); connection.send(); } }); } } 

And there is ConnectionMqtt :

 package ru.homeinn.iottank; /** * Created by vit-j on 15.02.2017. */ import android.annotation.SuppressLint; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; public class ConnectionMqtt implements MqttCallback { MqttClient sampleClient = null; public ConnectionMqtt(){ } public void connect(){ try { String broker = "tcp://m13.cloudmqtt.com:17546"; String clientId = "JavaSample"; MemoryPersistence persistence = new MemoryPersistence(); sampleClient = new MqttClient(broker, clientId, persistence); sampleClient.setCallback(new ConnectionMqtt()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setUserName(""); connOpts.setPassword("".toCharArray()); System.out.println("Connecting to broker: "+broker); sampleClient.connect(connOpts); System.out.println("Connected"); //System.out.println("Message published"); sampleClient.subscribe("#"); //sampleClient.disconnect(); //System.out.println("Disconnected"); //System.exit(0); } catch(MqttException me) { //System.out.println("reason "+me.getReasonCode()); //System.out.println("msg "+me.getMessage()); //System.out.println("loc "+me.getLocalizedMessage()); //System.out.println("cause "+me.getCause()); // System.out.println("excep "+me); me.printStackTrace(); } } private static final String TAG = "MQTTService"; public void send(){ int qos = 2; String topic = "MQTT Examples"; String content = "Message from MqttPublishSample"; System.out.println("Publishing message: "+content); MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); try { sampleClient.publish(topic, message); } catch (MqttException e) { e.printStackTrace(); } } @Override public void connectionLost(Throwable throwable) { System.out.println("connectionLost!"); } @Override @SuppressLint("NewApi") public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { System.out.println("Received message: "+topic + " message: " + mqttMessage); //mtext_mqtt.setText("Received message: "+topic + " message: " + mqttMessage); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { System.out.println("deliveryComplete: "+iMqttDeliveryToken); } } 

In ConnectionMqtt you need to receive messageArrived messages messageArrived process received messages and display them in text fields or change the values ​​of visual components in MainActivity .

At least this way:

 mtext_mqtt.setText("Received message: "+topic + " message: " + mqttMessage); 

But mtext_mqtt = (TextView) findViewById(R.id.info_text) don’t quite understand how to correctly declare mtext_mqtt = (TextView) findViewById(R.id.info_text) and transfer the data to it. As I understand this class is not part of the Activity

Teach in which direction to move and how to correctly declare.

Added stack trace:

 02-18 02:37:58.690 16678-16678/ru.homeinn.iottank E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to stop activity {ru.homeinn.iottank/ru.homeinn.iottank.MainActivity}: android.app.SuperNotCalledException: Activity {ru.homeinn.iottank/ru.homeinn.iottank.MainActivity} did not call through to super.onStop() at android.app.ActivityThread.handleSleeping(ActivityThread.java:2937) at android.app.ActivityThread.access$2600(ActivityThread.java:128) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4517) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) at dalvik.system.NativeStart.main(Native Method) Caused by: android.app.SuperNotCalledException: Activity {ru.homeinn.iottank/ru.homeinn.iottank.MainActivity} did not call through to super.onStop() at android.app.Activity.performStop(Activity.java:4610) at android.app.ActivityThread.handleSleeping(ActivityThread.java:2934) at android.app.ActivityThread.access$2600(ActivityThread.java:128) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4517) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) at dalvik.system.NativeStart.main(Native Method) 

Addition:

It does not fall now, but for some reason here:

 @Override protected void onStart() { super.onStart(); connection.setMqttListener(new ConnectionMqtt.MqttListener() { @Override public void onMessageArrived(String topic, MqttMessage mqttMessage) { // Здесь можете работать с полученным сообщением System.out.println("Done!!!"); } }); } 

And here:

 @Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { System.out.println("Received message: "+topic + " message: " + mqttMessage); if (mMqttListener != null) { mMqttListener.onMessageArrived(topic, mqttMessage); System.out.println("Done1"); } } 

The Done message is not displayed in the System.

  • Well, as an option, I think you can make ConnectionMqtt the internal activity class. Then ConnectionMqtt will have access to its elements. - Kirill Malyshev
  • Android studio is roughly a text editor, and you're new to java, in this case. It's like trying to write a sentence correctly in French, and when it doesn't work, say "help me write correctly in French, I'm new to Notepad" - pavlofff
  • @pavlofff I do not argue. But at the same time I try to read and comprehend what they advise or explain to me. And not just insert it into the code. - Vitali

2 answers 2

In the ConnectionMqtt class, declare the MqttListener interface:

 public interface MqttListener { void onMessageArrived(String topic, MqttMessage mqttMessage); } 

there you define the field for the object implementing this interface:

 private MqttListener mMqttListener; 

and setter / resetter to it:

 public void setMqttListener(MqttListener mqttListener) { mMqttListener = mqttListener; } public void resetMqttListener() { mMqttListener = null; } 

Call the created callback in the corresponding callback:

 @Override @SuppressLint("NewApi") public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { if (mMqttListener != null) { mMqttListener.onMessageArrived(topic, mqttMessage); } } 

In the onStart() method, onStart() install list:

 connection.setMqttListener(new ConnectionMqtt.MqttListener() { @Override public void onMessageArrived(String topic, MqttMessage mqttMessage) { // Здесь можете работать с полученным сообщением } }); 

In the onStop() method, you onStop() lister:

 connection.resetMqttListener(); 

The moments in which it is necessary to install / decouple the leafer depend on the specific situation.

  • Thanks for the answer! Added everything as you said. But after some time it crashes and adds: public void messageArrived (String topic, MqttMessage mqttMessage) throws Exception {System.out.println ("Received message:" + topic + "message:" + mqttMessage); if (mMqttListener! = null) {mMqttListener.onMessageArrived (topic, mqttMessage); System.out.println ("Done1"); }} - does not output Done1 when receiving a message. - Vitali
  • Но через какое то время вылетает - show the stack trace. - post_zeew
  • added stack trace to main message header. - Vitali
  • onStop() , Add a call to the onStop() method of a similar method of the superclass: super.onStop() . - post_zeew
  • Added, does not crash, but for some reason onMessageArrived (topic, mqttMessage); not called. - Vitali

Added Callback to MainActivity:

 connection.setCallback( new MqttCallback(){ @Override public void connectionLost(Throwable throwable) { System.out.println("connectionLost!"); } @Override public void messageArrived(final String topic, final MqttMessage mqttMessage) throws Exception { System.out.println("Received message: "+topic + " message: " + mqttMessage); runOnUiThread(new Runnable() { @Override public void run() { switch(topic) { case "iot/light": //mtext_mqtt.setText("light"); break; case "iot/filter": //mtext_mqtt.setText("filter"); break; case "iot/compressor": //mtext_mqtt.setText("compressor"); break; } //mtext_mqtt.setText("Received message: "+topic + " message: " + mqttMessage); } }); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { System.out.println("deliveryComplete: "+iMqttDeliveryToken); } }); 

In ConnectionMqtt:

 public class ConnectionMqtt { MqttClient sampleClient = null; 

...

  public void setCallback(MqttCallback cb){ sampleClient.setCallback(cb); }