Android: ANR Input dispatching timed out - android

I am getting following error logs on some phone which as follow:
Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 20. Wait queue head age: 5509.1ms.)
I am using Retrofit 2 for networking calls in which I use async method, Using database as realm where I used async transaction for writing stuff.
Using Glide for image loading.
On using Strict Mode I found out I get penalty log for shared preferences nothing else. Any other pointers to see and debug issue

Any types of time expensive procedure like data syncing through network shouldn't do in main thread. Android has some default built-in methods like AsyncTask, IntentService etc. those are on worker thread. Personally I have not good idea about Retrofit 2. If it works on main thread, that case you should use it in a separate thread.

Related

How to use RxJava for asynchronously executing a dynamic list of consecutive dependent operations?

I'm working on an Android Service Library (AAR) that has to execute a variable number of processes consecutive/pipelined on an input (JSON) in the background.
Thereby, the next process takes the output of the previous process as input.
The processes can involve literally every kind of possibly long running tasks (http requests, IO/DB requests, heavy data crunching, ...)
It shall be possible to log the progress between every process and get the final output of the last process e.g. in a subscriber (library service thread).
The processes should run stable, in case internet connection is lost or parent application state is changing.
I'm currently using Robospice in my library to achieve stability for multiple requests...
This question: How to implement a sequence of consecutive operations using rxjava
is related to my question, except I ask for a variable sequence of operations.
Is it possible achieve this with Rxjava? If yes, how? If not, what are other options?
My idea how to do it somehow without Rxjava:
Keep Process count for every request
Processes[counter].execute(result, callback)
Callback-OnSuccess(result): increase process counter and start Processes[counter] with result
But I'm not experienced with thread handling and think this is not very robust and maybe it doesn't even work or blocks the calling thread (what makes the library not usable for this time)

Waiting for a server task to complete

I have a situation where I have to do a server call from Android and based on the result of the call ( result would be either true or false) I want to proceed ahead in the code flow and pop a Dialog based on this result. Till the call is complete I don’t wish to proceed ahead in the code flow or logic. In short, I want to carry out a sync operation
At the server side, I will receive a HTTP Post request from android client and based on the parameters in the POST I do some processing by fetching values from the DB and return back true or false to android client.
To carry this out correctly on Android side, I researched and got two options.
Using Handler with AsyncTask does not work in my case because if
I use AsyncTask with Handler, the task result will arrive
sometime in future in Handler and the calling function will return
immediately whereas I want the calling function invoking
AsyncTask to finish i.e. wait till the server returns backs before I
proceed ahead .
The other option that I came across was to use
AsyncTask get() method . Since I am not carrying out a long running
task and rather performing a simple calculation on serverside, this
may not cause a an ANR situation even if I block the UI thread.
Are there any better options of carrying this out ? Is AsyncTask get() here actually one of the correct uses of get() ?

asynchronous volley HTTP requests

As all requests in Volley are executed asynchronously on a different thread without blocking the “main thread”, is there any way to wait for the request to complete and then continue the main thread execution?
You should design your app such that it keeps the main thread live at all times. You can then have blocks of code run when the response has been received using listeners or async task. Check out my answer using listeners here. Or look at onPostExecute for AsyncTask.
is there any way to wait for the request to complete and then continue
the main thread execution?
An alternative is to show a loading dialog box while volley is working. This way you can prevent the user from interacting with your app until the request is completed (just don't forget to give him a chance to cancel).
Volley is meant to work in parallel with your main thread and tell you when the request has completed (what all apps should do) if you don't desire that, don't use Volley. But you'll get a NetworkOnMainThreadException and if you manage to bypass it, you'll end up with an ANR exception.

Android: Send a signal from a thread in execution

Situation: I make a call to a thread, which in turn pings a server with a serialized object. Immediately after that, a function call pings the server with some parameters.
Problem : The thread indeed get called before the function, but the function finishes execution before the thread does. Result is that the parameters are sent before the serialized object.
How do I make the function not start its execution, until the thread has finished ? In other words, can the thread send a "signal" that its finished, so that I can begin execution of the remainder of the code ?
I think this is a book example of a situation where CountDownLatch should be used. Indeed, if you go to the documentation page you'll find a example of what you seem to need (I mean the CountDownLatch(1) example).

Debugging Event code

If I wish to debug some code for a UI event, e.g.
public boolean onTouchEvent(MotionEvent me)
{
// code to be debugged
}
... and I hold onto this thread (UI):
After 5 seconds I will get this warning: Key dispatching timed out sending to com.hos/com.hos.MyActivity ... null to Window ...
After 20 seconds I will get: Key dispatching timed out sending to com.hos/com.hos.MyActivity ... null to Window ... Continuing to wait for key to be dispatched
After 35 seconds I will get: Key dispatching timed out sending to com.hos/com.hos.MyActivity ... null to Window ... timed out expired process next Key & find new target
At this point, not only is my application frozen but so is the phone. Quite often I need to wait for the ANR and sometimes hard restart the phone.
Is there a way to debug this code for more than 35 seconds without freezing the app / phone?
If your processing may take more than milliseconds to process you may want to consider launching the processing in another thread which has access to a handler in the main thread.
Once you are done processing, you can then pass a message over to your handler which will then execute on the UI thread. This will help prevent those errors from coming up as well as make debugging your code easier.
If you are looking for an automated UI testing framework, perhaps you could look into integrating Robotium into your project.

Categories

Resources