patternjavaMinor
Socket handling in a thread
Viewed 0 times
handlingsocketthread
Problem
I am building an app for android to control VLC (mediaplayer) that runs on my computer.
At the moment it's just a prototype that works, but I was wondering if I am correctly managing my HandlerThread and Socket.
My fear is that the thread keeps running too long or that I cause memory leaks. I have looked at a lot of examples and questions on Stack Overflow, but I want to be sure before I complete the app in this way.
The Java program that runs on the desktop runs only on the main thread, so I will just post my Android code.
VlcRemoteActivity.java:
```
package be.zweetinc.PcController;
import android.app.Activity;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class VlcRemoteActivity extends Activity {
private ImageButton volumeUpButton;
private ImageButton volumeDownButton;
private EditText messageText;
private Message message;
private Button sendButton;
private Button killServerButton;
private HandlerThread connectionHandlerThread;
private ConnectionHandler connectionHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
volumeDownButton = (ImageButton) findViewById(R.id.downButton);
volumeUpButton = (ImageButton) findViewById(R.id.upButton);
messageText = (EditText) findViewById(R.id.message);
sendButton = (Button) findViewById(R.id.sendButton);
killServerButton= (Button) findViewById(R.id.killServerButton);
connectionHandlerThread = new HandlerThread("ConnectionThread");
connectionHandlerThread.start();
connectionHandler = new ConnectionHandler(connectionHandlerThread.getLooper());
message = Message.obtain(connectionHandler);
message.what = MessageCode.CLASS_CONNECTION; // EventClass C
At the moment it's just a prototype that works, but I was wondering if I am correctly managing my HandlerThread and Socket.
My fear is that the thread keeps running too long or that I cause memory leaks. I have looked at a lot of examples and questions on Stack Overflow, but I want to be sure before I complete the app in this way.
The Java program that runs on the desktop runs only on the main thread, so I will just post my Android code.
VlcRemoteActivity.java:
```
package be.zweetinc.PcController;
import android.app.Activity;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class VlcRemoteActivity extends Activity {
private ImageButton volumeUpButton;
private ImageButton volumeDownButton;
private EditText messageText;
private Message message;
private Button sendButton;
private Button killServerButton;
private HandlerThread connectionHandlerThread;
private ConnectionHandler connectionHandler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
volumeDownButton = (ImageButton) findViewById(R.id.downButton);
volumeUpButton = (ImageButton) findViewById(R.id.upButton);
messageText = (EditText) findViewById(R.id.message);
sendButton = (Button) findViewById(R.id.sendButton);
killServerButton= (Button) findViewById(R.id.killServerButton);
connectionHandlerThread = new HandlerThread("ConnectionThread");
connectionHandlerThread.start();
connectionHandler = new ConnectionHandler(connectionHandlerThread.getLooper());
message = Message.obtain(connectionHandler);
message.what = MessageCode.CLASS_CONNECTION; // EventClass C
Solution
From what I can see, it seems like you are doing it correctly.
On some exceptions, you might want to alert your user with a message about what went wrong (for example, "Connection failed"). Few users can read stack traces while using an app.
I only have a few comments regarding your code style:
You're inconsistent in putting spaces around
As in,
On some exceptions, you might want to alert your user with a message about what went wrong (for example, "Connection failed"). Few users can read stack traces while using an app.
I only have a few comments regarding your code style:
You're inconsistent in putting spaces around
{-characters, and also with if-statements. There should always be a space after if and before a {As in,
private void makeConnection(){ <-- insert space here.Context
StackExchange Code Review Q#22778, answer score: 6
Revisions (0)
No revisions yet.