Possible to compensate for Anr error?

mistermentality

Active Member
Licensed User
Longtime User
I rewrote an app in B4A and it works great, no errors so I put it on the market (it is a free app) and got my first ever error report saying "Anr key dispatching timed out".

I researched this and found the following solution:
The ANR, or Application Not Responding, error occurs when a process on the main thread takes too long (something like 5 seconds). Android kills that process and any related by design to spare the device's resources.

The solution is to run resource-expensive tasks on another thread, and then post, or update the main thread accordingly. A common use for this is network calls

The app does use a webview which would come under the heading of network calls, and indeed the error report itself mentions the webview a few times.

So, is there a way I can run the webview tasks on another thread in B4A somehow as this solution suggests?

The error report itself is shown below but not being an expert I had to google a lot as I had no idea what the error meant. I only include the error message in case it helps answer my question, and because it shows the webview references I mentioned.

Please disregard the references in it to app inventor, this is a B4A app but as it replaced an appinventor one I could only publish it as an upgrade if I kept the package name the same hence the appinventor references.

The code itself works fine, I will include the apk in this post if it will help, and it is only one error report but if it is something I can correct with programming code I hope someone more experienced than I can point me in the right direction.

The error report is (for the installed apk):
B4X:
DALVIK THREADS:
"main" prio=5 tid=1 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x40025b50 self=0xcdb8
  | sysTid=7155 nice=0 sched=0/0 cgrp=default handle=-1345017804
  | schedstat=( 952728295 799621571 1829 )
  at org.apache.harmony.luni.platform.OSFileSystem.readImpl(Native Method)
  at org.apache.harmony.luni.platform.OSFileSystem.read(OSFileSystem.java:118)
  at java.io.FileInputStream.read(FileInputStream.java:312)
  at java.io.FileInputStream.read(FileInputStream.java:267)
  at anywheresoftware.b4a.phone.Phone.Shell(Phone.java:99)
  at appinventor.ai_mygt_dh.Is_it_safe.main._checkscanpresent(main.java:570)
  at appinventor.ai_mygt_dh.Is_it_safe.main._activity_create(main.java:339)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)
  at appinventor.ai_mygt_dh.Is_it_safe.main.afterFirstLayout(main.java:83)
  at appinventor.ai_mygt_dh.Is_it_safe.main.access$100(main.java:15)
  at appinventor.ai_mygt_dh.Is_it_safe.main$WaitForLayout.run(main.java:71)
  at android.os.Handler.handleCallback(Handler.java:587)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:143)
  at android.app.ActivityThread.main(ActivityThread.java:5068)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  at dalvik.system.NativeStart.main(Native Method)

"java.lang.ProcessManager" daemon prio=5 tid=10 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x472784a8 self=0x32bdc8
  | sysTid=7173 nice=0 sched=0/0 cgrp=default handle=2855160
  | schedstat=( 518797 3784178 12 )
  at java.lang.ProcessManager.watchChildren(Native Method)
  at java.lang.ProcessManager$1.run(ProcessManager.java:87)

"CookieSyncManager" prio=5 tid=9 TIMED_WAIT
  | group="main" sCount=1 dsCount=0 s=N obj=0x47232908 self=0x2b9138
  | sysTid=7164 nice=10 sched=0/0 cgrp=default handle=3315768
  | schedstat=( 518798 3631592 5 )
  at java.lang.Object.wait(Native Method)
  - waiting on <0x471efd10> (a android.os.MessageQueue)
  at java.lang.Object.wait(Object.java:326)
  at android.os.MessageQueue.next(MessageQueue.java:142)
  at android.os.Looper.loop(Looper.java:120)
  at android.webkit.WebSyncManager.run(WebSyncManager.java:90)
  at java.lang.Thread.run(Thread.java:1102)

"WebViewWorkerThread" prio=5 tid=8 WAIT
  | group="main" sCount=1 dsCount=0 s=N obj=0x472741d8 self=0x329268
  | sysTid=7162 nice=1 sched=0/0 cgrp=default handle=3314600
  | schedstat=( 8087158 38146972 46 )
  at java.lang.Object.wait(Native Method)
  - waiting on <0x471da0e8> (a android.os.MessageQueue)
  at java.lang.Object.wait(Object.java:288)
  at android.os.MessageQueue.next(MessageQueue.java:146)
  at android.os.Looper.loop(Looper.java:120)
  at android.os.HandlerThread.run(HandlerThread.java:60)

"WebViewCoreThread" prio=5 tid=7 WAIT
  | group="main" sCount=1 dsCount=0 s=N obj=0x47265360 self=0x2f0378
  | sysTid=7161 nice=0 sched=0/0 cgrp=default handle=3081400
  | schedstat=( 167083743 198028570 529 )
  at java.lang.Object.wait(Native Method)
  - waiting on <0x47265640> (a android.os.MessageQueue)
  at java.lang.Object.wait(Object.java:288)
  at android.os.MessageQueue.next(MessageQueue.java:146)
  at android.os.Looper.loop(Looper.java:120)
  at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:645)
  at java.lang.Thread.run(Thread.java:1102)

"Binder Thread #2" prio=5 tid=6 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x471da458 self=0x13d488
  | sysTid=7160 nice=0 sched=0/0 cgrp=default handle=1220832
  | schedstat=( 3662112 11169434 19 )
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=5 NATIVE
  | group="main" sCount=1 dsCount=0 s=N obj=0x471d9418 self=0x12ace8
  | sysTid=7159 nice=0 sched=0/0 cgrp=default handle=1223832
  | schedstat=( 5401609 11230471 26 )
  at dalvik.system.NativeStart.run(Native Method)

"Compiler" daemon prio=5 tid=4 VMWAIT
  | group="system" sCount=1 dsCount=0 s=N obj=0x471d22a8 self=0x12aaa8
  | sysTid=7158 nice=0 sched=0/0 cgrp=default handle=1296024
  | schedstat=( 110321031 109588633 810 )
  at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
  | group="system" sCount=0 dsCount=0 s=N obj=0x471d21e8 self=0x13c558
  | sysTid=7157 nice=0 sched=0/0 cgrp=default handle=1306864
  | schedstat=( 1739502 8941652 8 )
  at dalvik.system.NativeStart.run(Native Method)

"HeapWorker" daemon prio=5 tid=2 VMWAIT
  | group="system" sCount=1 dsCount=0 s=N obj=0x45c34de0 self=0x12bb18
  | sysTid=7156 nice=0 sched=0/0 cgrp=default handle=1223256
  | schedstat=( 51849367 39154054 59 )
  at dalvik.system.NativeStart.run(Native Method)

Thank you for any help in advance, and apologies for the long post.

Dave
 

agraham

Expert
Licensed User
Longtime User
It looks like it has timed out or otherwise gone wrong in a Shell command at Java line 99 in your code. You can find the actual code by finding the java files at the bottom of your project folder Objects/src hierarchy and opening them in a line numbering editor. I use Programmers Notepad2.

EDIT:- Sorry, line 99 is in the Phone library, the error is at line 570 in your CheckScanPresent Sub in your main activity

I know nothing about how the Market works but out of interest how did you get an error report in that format? Is it automatic or does the user have to instigate it somehow? I guess its from a user with 2.2 is it?
 
Last edited:
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
It looks like it has timed out or otherwise gone wrong in a Shell command at Java line 99 in your code. You can find the actual code by finding the java files at the bottom of your project folder Objects/src hierarchy and opening them in a line numbering editor. I use Programmers Notepad2.

EDIT:- Sorry, line 99 is in the Phone library, the error is at line 570 in your CheckScanPresent Sub in your main activity

I know nothing about how the Market works but out of interest how did you get an error report in that format? Is it automatic or does the user have to instigate it somehow? I guess its from a user with 2.2 is it?

Thank you I shall go and look into that, and forgive my apparent stupidity but how could you tell it was line 570. Is it because that line is the first mentioned?

In regard to the error format, users with Android 2.2 or higher get offered to send an error report to the developer and that is the format it comes in. But it only gets sent if the user allows it, it isn't automatically sent.

I put in the market write up for the app that it was a complete code rewrite and asked if any one did get an error would they please report it and they did.

The Market page for developers also shows things like what device models, Android OS versions and countries use your apps, which is handy, but the error reports are in the format shown earlier and additionally you can see how many freezes and how many crashes each app has generated.

I have four apps on the market and only ever had the one error so I don't know if many people use the feature but according to the market page 57% of people using my apps have the error report ability so I doubt many choose to use it.

Thanks

Dave
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
B4X:
  at appinventor.ai_mygt_dh.Is_it_safe.main._checkscanpresent(main.java:570)
This line tells you it is at line 570 in the Java code file named main.java which is in your Sub CheckScanPresent in module Main in your application name Is_it_safe in the appinventor.ai_mygt_dh package.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
B4X:
  at appinventor.ai_mygt_dh.Is_it_safe.main._checkscanpresent(main.java:570)
This line tells you it is at line 570 in the Java code file named main.java which is in your Sub CheckScanPresent in module Main in your application name Is_it_safe in the appinventor.ai_mygt_dh package.

I did wonder, I see what you mean but was not sure as there are quite a few things that say at. But I see that you're right. And indeed the part around that says:
B4X:
at anywheresoftware.b4a.phone.Phone.Shell(Phone.java:99)
at appinventor.ai_mygt_dh.Is_it_safe.main._checkscanpresent(main.java:570)

just reiterates you are right, as I used the shell command from the library mentioned to check installed apps for the zxing app so if I remove that check the problem should be fixed.

Thank you :)

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
I didn't realise how to at the time and so adapted the example code you posted in another thread:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim p As Phone
    Dim sb As StringBuilder
    sb.Initialize
    p.Shell("pm", Array As String("list", "packages"), sb, Null)
    Msgbox(sb.ToString, "Installed packages:")
End Sub

and then checked the resulting string for an occurence of zxing but didn't know at the time that the barcode library appears to have its own checks and so I didn't need to check for zxing being present.

But I shall look into package manager if I need to next time. Thanks :)

Dave
 
Upvote 0
Top