changing from runnable to async task - java

I've got an app reading from a socket. I've tested this on a phone with API 18 on. If I try it on a phone with API 24 it throws a NetworkOnMainThread exception.
I've read that I need to do this in AsyncTask for the later API's, or I'm doing something else wrong. I'm unsure what I need to do with my code to convert it to an AsyncTask for the way I'm waiting for input stream data?
This is my read input stream class.
class receiveData extends Thread {
private volatile boolean exit = false;
DataInputStream in;
byte[] fullBuffer = new byte[7];
byte[] buffer = new byte[100];
int bytes; // bytes returned from read()
int bytesCount = 0;
public void run(){
try {
if (socket.isConnected()) {
in = new DataInputStream(socket.getInputStream());
}
} catch(Exception e) {
Log.d(TAG, "in receiveData - run exception - " + e.toString());
}
while(!exit) {
try {
bytes = in.read(buffer);
System.arraycopy(buffer, 0, fullBuffer, bytesCount, bytes);
bytesCount = bytesCount + bytes;
if (bytesCount >= 7) {
h.obtainMessage(NOW_DATA_RECEIVED, bytesCount, -1, fullBuffer).sendToTarget(); // Send to message queue Handler
bytesCount = 0;
} else {
Log.d(TAG, "Receive Error");
bytesCount = 0;
}
} catch(Exception e) {
Log.d(TAG, "Read Error - " + e.toString());
}
}
}
public void stopRdThread() {
exit = true;
try {
socket.close();
} catch(Exception e) {
Log.d(TAG, "error closing socket - " + e.toString());
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] message) {
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.write(message);
out.flush();
} catch (IOException e) {
Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
}
}
}
I'm starting it like this.
private receiveData rd;
// start receiver thread
rd = new receiveData();
rd.start();
Like I said it's working great in API 18 but won't in API 24. All the examples I've seen are for one off tasks in the background, such as downloading a picture, not threads that are left running waiting for data.

Well all newest androids API's will throw exceptions when you try connect network in ui thread.
The only way is to use AsyncTask or use Handler;
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
//Do something
}
});
Note: If you want to use network and then display things on UI then you must use AsyncTask.

Related

Send messages to Handler from other activity

I Have a code that connects to a bluetooth device, opens a bluetooth socket that communicates with a running thread which operates functions running in main activity.
I would like to move all the connecting sequence to another activity, and then operate the thread from the main one as done now. The problem is they are all connected.
I would like to have the option of sending a message between these activities(meaning remaining the socket operating from the other activity), i.e this message:
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
.sendToTarget();
because it is impossible to pass handler between activities I don't know how/if possible to do so.
What is the best way of doing such a thing?
added part of the code.
Thanks.
mHandler = new Handler(){
public void handleMessage(android.os.Message msg){
if(msg.what == MESSAGE_READ){
String readMessage = null;
try {
readMessage = new String((byte[]) msg.obj, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
RxMessage = readMessage.split(" ");
if (sH.isStringInCorrectOrder(RxMessage,Weight))
populateListView(RxMessage);
mReadBuffer.setText(readMessage);
}
if(msg.what == CONNECTING_STATUS){
if(msg.arg1 == 1)
mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
else
mBluetoothStatus.setText("Connection Failed");
}
}
};
private void connectBT (){
mBluetoothStatus.setText("Connecting...");
// Get the device MAC address, which is the last 17 chars in the View
final String address = "98:D3:31:30:39:75";
final String name = "HC-06";
// Spawn a new thread to avoid blocking the GUI one
new Thread()
{
public void run() {
boolean fail = false;
BluetoothDevice device = mBTAdapter.getRemoteDevice(address);
try {
mBTSocket = createBluetoothSocket(device);
} catch (IOException e) {
fail = true;
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
}
// Establish the Bluetooth socket connection.
try {
mBTSocket.connect();
} catch (IOException e) {
try {
fail = true;
mBTSocket.close();
mHandler.obtainMessage(CONNECTING_STATUS, -1, -1)
.sendToTarget();
} catch (IOException e2) {
//insert code to deal with this
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_SHORT).show();
}
}
if(fail == false) {
mConnectedThread = new ConnectedThread(mBTSocket);
mConnectedThread.start();
mHandler.obtainMessage(CONNECTING_STATUS, 1, -1, name)
.sendToTarget();
}
}
}.start();
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.available();
if(bytes != 0) {
SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
bytes = mmInStream.available(); // how many bytes are ready to be read?
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget(); // Send the obtained bytes to the UI activity
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(String input) {
byte[] bytes = input.getBytes(); //converts entered String into bytes
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
Just declare mHandler as static and you can access it from all other activities. This will create a small temporary memory leak, but don't worry about it.

Android - PC usb connection - do it without wifi

I wanted to ask how to change following code, which needs USB connection and WIFI to work... (and I don't know why wifi...), to code, which needs only USB cable and NO WIFI!, because I don't want to be dependent on wifi...
Could you please help me? Some changes or additions in code? Thanks.
Code for Android:
private final Runnable connectToServer = new Thread()
{
#Override
public void run()
{
try
{// Get the server address from a dialog box.
String serverAddress = "192.168.0.23";
// Make connection and initialize streams
Socket socket = new Socket(serverAddress, 38300);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Consume the initial welcoming messages from the server
for (int i = 0; i < 3; i++) {
System.out.println(in.readLine());
}
solveCube();
} catch (IOException e) {
e.printStackTrace();
}
}
};
private final Runnable initializeConnection = new Thread()
{
#Override
public void run()
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(generateCubeString());
out.println(generateCubeString());
String response ="";
try {
response = in.readLine();
if (response == null || response.equals("")) {
System.exit(0);
}
} catch (IOException ex) {
}
if (response.contains("Error")) {
} else {
solveCubeAnimate(response);
}
System.out.println(response);
final String finalResponse = response;
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(finalResponse);
}
});
}
};
Code for PC
private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;
public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Send a welcome message to the client.
out.println("Hello, you are client #" + clientNumber + ".");
out.println("Enter a line with only a period to quit\n");
// Get messages from the client, line by line; return them
// capitalized
while (true) {
String input = in.readLine();
if (input == null || input.equals(".")) {
break;
}
out.println(solveCube(input));
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
/**
* Logs a simple message. In this case we just write the
* message to the server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}
private static class Connecter extends Thread {
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(38300);
try {
while (true) {
new Capitalizer(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Multi-threaded server socket in android to receive images from connected clients - not working

When clients are connected to the server they should be able to send images to server, so the server can display the received images. I have used an AsyncTask at the server-end to do the task.
when one client is connected to the server everything seems fine, images are receiving from that client but when the next client connected it's not working, sometimes the first client gets disconnected or the images are not receiving from one client or both.
Can someone help me with this? am i doing anything wrong?
Server-end AsyncTask
public static class FileServerAsyncTask extends AsyncTask {
private Context context;
private TextView statusText;
public FileServerAsyncTask(Context context, View statusText) {
this.context = context;
this.statusText = (TextView) statusText;
}
#Override
protected String doInBackground(Void... params) {
class ClientWorker implements Runnable {
private Socket client;
private final File f;
//Constructor
ClientWorker(Socket client, File f) {
this.client = client;
this.f = f;
}
public void run(){
try{
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
Log.d(WiFiDirectActivity.TAG, "server: copying files " + f.toString());
InputStream inputstream = client.getInputStream();
copyFile(inputstream, new FileOutputStream(f));
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
try {
ServerSocket serverSocket = new ServerSocket(8988);
Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
boolean check = true;
while(check){
ClientWorker w;
try{
//server.accept returns a client connection
final File f = new File(Environment.getExternalStorageDirectory() + "/"
+ context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
+ ".jpg");
w = new ClientWorker(serverSocket.accept(),f);
Thread t = new Thread(w);
t.start();
return f.getAbsolutePath();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
serverSocket.close();
return null;
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
return null;
}
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
statusText.setText("File copied - " + result);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + result), "image/*");
context.startActivity(intent);
}
}
#Override
protected void onPreExecute() {
statusText.setText("Opening a server socket");
}
}
public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.close();
inputStream.close();
} catch (IOException e) {
Log.d(WiFiDirectActivity.TAG, e.toString());
return false;
}
return true;
}
I think all you need is just changing these lines:
w = new ClientWorker(serverSocket.accept(),f);
Thread t = new Thread(w);
t.start();
return f.getAbsolutePath();
your problem is after the first client is connect you go out of doInbackground. you must use onProgressPublish() to send the intermidiate result and stay in loop and do not close server.
so after you connect to first client and sending him to socket you created, you go out of main loop by return f.getAbsolutePath(); and go out from server. all your code is correct except that for sending out intermediate result use function(onProgressPublish()).

android threading and handler - Changing UI objects from a background thread

I've recently written a small android application to send and receive UDP messages over a local network.I have a UDP receiver thread that runs to listen for UDP packets, what I want to happen is a button to become enabled on the UI when a packet is received that contains a certain string of data. I know that this has to be done with a handler, the problem being that I have a small amount of knowledge about threads and very little knowledge about handlers. would someone be able to shed some light on how a handler could be put into my code? thanks
code:
public void startUDPlistener() {
// Creates the listener thread
LISTEN = true;
Thread listenThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Log.i(LOG, "Listener started!");
DatagramSocket socket = new DatagramSocket(BROADCASTPORT);
socket.setSoTimeout(1500);
byte[] buffer = new byte[BUFFERSIZE];
DatagramPacket packet = new DatagramPacket(buffer, BUFFERSIZE);
while(LISTEN) {
try {
Log.i(LOG, "Listening for packets");
socket.receive(packet);
String data = new String(buffer, 0, packet.getLength());
Log.i(LOG, "UDP packet received from "+ packet.getAddress() +" packet contents: " + data);
}
catch(IOException e) {
Log.e(LOG, "IOException in Listener " + e);
}
}
Log.i(LOG, "Listener ending");
socket.disconnect();
socket.close();
return;
}
catch(SocketException e) {
Log.e(LOG, "SocketException in Listener " + e);
}
}
});
listenThread.start();
}
You basically just add one and send either a Message or a Runnable.
private final Handler mUiThreadHandler = new Handler(Looper.getMainLooper());
void fromAnyThread() {
final String importantDataFromBackGroundThread = "!!!";
mUiThreadHandler.post(new Runnable() {
#Override
public void run() {
System.out.println("Hi from Ui Thread:" + importantDataFromBackGroundThread);
}
});
}
The Handler handles all messages / runnables within the Thread it runs in. If you specify Looper.getMainLooper() you are guaranteed that this is the main thread.

sending packet over 3G network

I am trying to write a method that will send a message over a 3G network with a base station to the server. IM trying to send the message multiple times until I decide to stop. But when I tested this, it always stops after a short time and stops sending the message. Anyone know why?
private Runnable commRunnable = new Runnable() {
public void run() {
try {
String message = "Just saying hello!";
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);
String startReceivingMessage = "Begin sending me data.";
String stopReceivingMessage = "Stop sending me data.";
startSend = false;
stopSend = false;
startReceive = false;
stopReceive = false;
while (!shouldDisconnect) {
if (startSend) {
sendData = true;
startSend = false;
}
if (stopSend) {
sendData = false;
stopSend = false;
}
// Send a message that the server should start transmitting data
// back to us. We only need to transmit this message once.
if (startReceive) {
out.println(startReceivingMessage);
startReceive = false;
receiveData = true;
Thread receiveThread = new Thread(receiveRunnable);
receiveThread.start();
// Tell the server to stop transmitting data.
} else if (stopReceive) {
out.println(stopReceivingMessage);
stopReceive = false;
receiveData = false;
}
if (sendData) {
out.println(message);
}
Thread.sleep(20);
}
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
} finally {
try {
socket.close();
connected = false;
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
}
}
}
};
private Runnable receiveRunnable = new Runnable() {
#Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String reply = "";
Log.d("PowerMonitor", "Starting to receive");
while (receiveData) {
Log.d("PowerMonitor", "Listening...");
reply = in.readLine();
Log.d("PowerMonitor", "Got message: " + reply);
}
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
}
}
};
We need more of your code for better understanding of your question, but from the code you posted here its seems like the socket is being closed by the
socket.close();
call in the finally block.
Also tell about any errors you are getting.

Categories

Resources