At the moment the code below is giving me exceptions for networking in the main thread. I've compared it to a previous app that this is going to replace and I can't see where I went wrong (maybe just need to take a break and uncross my eyes). Another thing I would like to do is create a thread class in a separate file to help clean up the main class a little. I had things working on different levels over the last few days, but never fully what I wanted.
Code:
package com.flipperbuilt.trailerlights3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_message_handler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
Bundle bundle_in = msg.getData();
Message message;
if (bundle_in.getString("exception why").length() > 0) {
Toast toast = Toast.makeText(getApplicationContext(),bundle_in.getString("exception where")+"\n"+bundle_in.getString("exception why"),Toast.LENGTH_LONG);
toast.show();
}
}
};
new socket_thread_class().start();
}
// ------------------------------------------------------------------------------------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
// ------------------------------------------------------------------------------------------------------
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
return true;
}
// ------------------------------------------------------------------------------------------------------
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_disable_switches:
connect();
break;
}
return true;
}
// ------------------------------------------------------------------------------------------------------
private void connect() {
Log.e("MainActivity","connecting");
Message msg = thread_message_handler.obtainMessage();
msg.arg1 = SOCKET_CONNECT;
Bundle b = new Bundle();
b.putString("ip","192.168.1.31");
b.putInt("port",5010);
msg.setData(b);
thread_message_handler.dispatchMessage(msg);
}
// ------------------------------------------------------------------------------------------------------
class socket_thread_class extends Thread {
@Override
public void run() {
keep_running = true;
this.setName(socket_thread_class.class.getName());
this.setPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
Looper.prepare();
thread_message_handler = new Handler(new Handler.Callback() {
public boolean handleMessage(Message msg) {
Bundle bundle_in = msg.getData();
Message message = main_message_handler.obtainMessage();
Bundle bundle_out = new Bundle();
message.setData(bundle_out);
switch(msg.arg1) {
case SOCKET_CONNECT: {
Log.e("SocketThread", "socketConnect() -- IP -> " + bundle_in.getString("ip") + ":" + bundle_in.getInt("port"));
try {
out_socket = new Socket();
out_socket.connect(new InetSocketAddress(bundle_in.getString("ip"), bundle_in.getInt("port")), 2000);
} catch (Exception e) {
bundle_out.putString("exception why", e.toString());
bundle_out.putString("exception where","connect()");
} finally {
if (out_socket.isConnected()) {
Log.e("SocketThread", "socket good");
try {
//bundle_out.putCharSequence("LastCommand","getOutputStream()");
stream_out = out_socket.getOutputStream();
} catch (IOException e) {
bundle_out.putString("exception where","getOutputStream()");
bundle_out.putString("exception why", e.toString());
}
try {
//bundle_out.putCharSequence("LastCommand","getOutputStream()");
stream_in = out_socket.getInputStream();
} catch (Exception e) {
bundle_out.putString("exception why", e.toString());
bundle_out.putString("exception where","getInputStream()");
}
}
}
}
break;
case SOCKET_DISCONNECT: {
Log.e("SocketThread", "DISCONNECT");
keep_running = false;
}
break;
}
main_message_handler.dispatchMessage(message);
return true;
}
private Socket out_socket;
private OutputStream stream_out;
private InputStream stream_in;
});
if (keep_running) Looper.loop();
Log.e("SocketThread","at the end");
}
private boolean keep_running;
}
// ------------------------------------------------------------------------------------------------------
private Handler main_message_handler;
private Handler thread_message_handler;
private final int SOCKET_CONNECT = 1;
private final int SOCKET_DISCONNECT = 2;
}