Android Thread problem - android

I already know how to stop thread but my present scenario is somewhat different, I am listening for data from server in thread
while (connected)
{
byte[] buffer = new byte[1024];
if(in.read(buffer) > 0)// waits on this line
{
//do something
}
}
now the problem is that although I set connected = false; then also it not stopping because it waits on in.read so , I also tried to Thread.interrupt but no luck

You should probably switch to using a SelectableChannel approach to reading/writing to sockets where non-blocking I/O is used and you can manage multiple connections by listening for 'events'. You then avoid being blocked in calls like read().
This, unfortunetly, is a fundamentally different model to the one you are currently using, so the switch over won't be quick or easy, but it will be worth the effort.
Check out these resources:
http://developer.android.com/reference/java/nio/channels/SocketChannel.html
http://www.exampledepot.com/egs/java.nio/NbClientSocket.html

We need a little more info here:
How do you declare and modify connected?
Do you have a try-catch that is "eating" the InterruptedException?
How do you create the thread?
How are you starting the thread?
How do you (attempt to) terminate the thread?
Provide a sscce compliant example which we can copy & paste, compile and see your exact problem.
In general your code should look like this:
while (connected)
{
try
{
byte[] buffer = new byte[1024];
if(in.read(buffer) > 0)// waits on this line
{
//do something
}
}
catch(InterruptedException ie)
{
// there are several ways which you can exit the loop:
// 1. you can break
// 2. set the connected flag to false
}
}
Verify that you are actually catching the interrupt exception! If you're not catching it, then built a small sscce compliant example in which you're only reproducing the specific issue (should be pretty easy to do).

I modified the code in following way
while (connected)
{
byte[] buffer = new byte[1024];
if(in.available() > 0)// changed this line
{
in.read(buffer)
//do something
}
}
and it worked fine for me since in.available() statement is non- blocking I suppose

Related

Android sequential execution of functions

I know Android UI is not really meant for executing functions and waiting for them to finish, however, I think there are use cases were it is required, like networking.
My problem is, I want to run a series of network operations that rely on each other and take a bit more time than the split second it takes to the next execution, so some waiting is in order:
Start hotspot
Get network interfaces and IP
Start socket
Initially I tested that all is working using buttons, then it waited between my button presses. But now I'd like to automatize it. I googled but all I found are solutions with Async task, which is deprecated. I tried with threads and join, but that usually causes weird crashes in the runnable, and it is not very elegant. I wonder if there is another solution?
The best thing you can do with SDK it's use Executors to run your work in background sequentially
val newSingleThreadExecutor = Executors.newSingleThreadExecutor()
newSingleThreadExecutor.execute {
// 1...
}
newSingleThreadExecutor.execute {
// 2...
}
But if you want to touch the UI from background should create handler check if view's not null
val handler = Handler(Looper.myLooper()!!)
newSingleThreadExecutor.execute {
handler.post {
view?.visibility = View.GONE
}
}
How about something like this?
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startHotspot();
getNetworkInterfaceAndIP();
startSocket();
}
}, 300);

Android application doing too much work on main thread

I call function processImage() on a button click and could't get the exact output needed.
processImage() function,
private void processImage() {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (textRecognizer.isOperational()) {
Log.d("IMAGE-PROCESS", "started");
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
final SparseArray<TextBlock> items = textRecognizer.detect(frame);
runOnUiThread(new Runnable(){
public void run() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.size(); i++) {
TextBlock textBlock = items.valueAt(i);
stringBuilder.append(textBlock.getValue());
stringBuilder.append("\n");
}
try {
Log.d("IMAGE-PROCESS", "finished");
Log.d("OUTPUT", stringBuilder.toString());
textView.setText(stringBuilder.toString());
} catch (final Exception ex) {
Log.i("EXC","Exception in thread");
}
}
});
} else {
Log.d("IMAGE-PROCESS", "not operational");
}
}
It logs
I/Choreographer: Skipped 86 frames! The application may be doing too much work on its main thread.
Do I need to change anything in my function?
Help me to fix this
You need to make sure that the processImage method runs on a background (non-ui) thread. It is probably running on the UI thread now, and you get the warning because it is blocking the UI thread.
There are a number of ways to do work on a background thread, from simply spawning a new thread to creating a service - you will need to research this, and decide on the best method for your particular situation. Just remember that when your background processing is complete, you need to transfer the data to the UI thread, and update the UI from the UI thread, as attempting an update from another thread will cause a crash.
My only specific advice is to not use an AsyncTask. They sound great in theory, but unless you really know how they work, they can get you into a lot of trouble. And if you really know how they work, you're fully capable of doing something more reliable.
Where is the posted code located?
Usually, even if the hard work is done on the UI thread, the screen will freeze/stutter, but eventually, the desired output is produced. I'd guess 86 skipped frames is ~1.25 s of freezing.
If your processImage() method is inside a Runnable, Callable<>, or the like, then one could understand the use of Activity.runOnUiThread(), but..
Since you are getting this message, and you did not mention this imporant detail, I'll assume that your code runs on the UI thread.
Image processing is an expensive task, and should be carried out on a background thread.
To do an kind of background work, you should use some of the ready-made classes (e.g. AsyncTask, Service, etc.), or create ones on your own (with the help of Executors or the like).
Find out how to do that here

Read logs from Logcat without killing the tablet

I have to read the logs from Logcat and send them to a server through UDP.
For this task I have used this code: https://github.com/chemik/logcatudp
The main problem of that code is that the async Thread that is launched enters a while(true) loop that drains the tablet's battery on the long run.
Is there a way to get the logs in real time but without using a busy wait like that? Hopefully without adding some sleep(some_milliseconds) to reduce the problem?
It would be great to use some sort of event listener but I haven't found one. I have searched in every similar library but without any success.
The code is the following:
while (true) {
String sendingLine = "";
// assume that log writes whole lines
if (bufferedReader.ready()) {
logLine = bufferedReader.readLine();
sendingLine += logLine + System.getProperty("line.separator");
DatagramPacket packet = new DatagramPacket(sendingLine.getBytes(), sendingLine.length(),
InetAddress.getByName(mConfig.mDestServer), mConfig.mDestPort);
try {
mSocket.send(packet);
...
Any idea? Thanks.
Finally the answer was to put a Thread.sleep(10) in the while(true) loop.
It may seem really strange, but also with only 10ms of sleep it reduces the battery usage from almost 40% to 1%.

Android usb host: asynchronous interrupt transfer

I'm trying to connect a USB-Device ( build by myself ) to communicate with my development board ( ODROID-X )
Unfortunately, the examples are very little, as far as the asynchronous communication. I'd some problems with the interrupt driven data exchange - how to build the connection by using the asynchronous interrupt mode?
In one direction, the transmission was possible ... but in both it doesn't work. Is there an example like this:
send a ByteBuffer with endpoint_OUT
get a message from device on endpoint_IN
both in interrupt mode.
Thanks a lot for your support.
Hardy
Perhaps I am misunderstanding the question here.
The sample missile lanucher app that is part of the API package from level 12 onwards uses the queue() and requestWait() methods to handle interrupt type endpoints.
Requests are either In or Out and depend on the direction of the EndPoint.
The code for a pretty noddy request->reply looks something like this. You would want to structure real code differently but this gives you the gist of what needs to happen (I hope)
public void run() {
int bufferMaxLength=mEndpointOut.getMaxPacketSize();
ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
UsbRequest request = new UsbRequest(); // create an URB
request.initialize(mConnection, mEndpointOut);
buffer.put(/* your payload here */;
// queue the outbound request
boolean retval = request.queue(buffer, 1);
if (mConnection.requestWait() == request) {
// wait for confirmation (request was sent)
UsbRequest inRequest = new UsbRequest();
// URB for the incoming data
inRequest.initialize(mConnection, mEndpointIn);
// the direction is dictated by this initialisation to the incoming endpoint.
if(inRequest.queue(buffer, bufferMaxLength) == true){
mConnection.requestWait();
// wait for this request to be completed
// at this point buffer contains the data received
}
}
}
If you are actually looking for a way to run this IO in an asynchronous manner without binding a thread to it, then I think you need to consider using the DeviceConnection.getFilehandle() method to return a standard file handle which in theory you can then use as if it were any other file type resource. I would note however that I have not tried this.
If neither of these addresses the issue please revise the question to clarify what you are struggling to find examples of.
I hope this helps.

How to abort LocalServerSocket.accept

How to abort/interrupt a LocalServerSocket waiting, in a background thread, for a connection in method LocalServerSocker.accept() ?
I've tried to call close() method from another thread, but it does not seem to work.
There is a ticket opened for it in Android project: http://code.google.com/p/android/issues/detail?id=29939.
Seems the problem is confirmed by Google, as the first comment is:
i think we should rewrite this to use the same underlying libcore
stuff, and get the interruption behavior for free.
The workaround can be to send a custom shutdown command to the LocalServerSocket from another thread to unblock accept.
Instead of localSocket.close, use this: (requires API 21+)
try {
Os.shutdown(localSocket.fileDescriptor, OsConstants.SHUT_RDWR)
} catch (e: ErrnoException) {
if (e.errno != OsConstants.EBADF) throw e // suppress fd already closed
}

Categories

Resources