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.