I have got some ANRs in my android vitals:
Broadcast of Intent { act=android.intent.action.SCREEN_ON flg=0x50200010 (has extras) } or
Broadcast of Intent { act=android.intent.action.SCREEN_ON flg=0x50000010 }.
I have also have got some ANRs:
Broadcast of Intent { act=android.intent.action.SCREEN_OFF flg=0x50000010 }
It seems that those ANRs happen regularly. From my research i did not found a solution. Specifically i wonder if these ANRs are related with getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
in my onCreate to keep screen on. I do not use any BroadcastReceiver in my app. Any help?
Error logs from android vitals:
"main" tid=1 Waiting "main" prio=5 tid=1 Waiting | group="main" sCount=1 dsCount=0 flags=1 obj=0x73c98680 self=0xee259000 | sysTid=24710 nice=0 cgrp=default sched=0/0 handle=0xf26ff4bc | state=S schedstat=( 2919404633 900616698 9273 ) utm=197 stm=94 core=0 HZ=100 | stack=0xff6b6000-0xff6b8000 stackSize=8MB | held mutexes= at java.lang.Object.wait (Native method) - waiting on <0x03bec179> (a com.myPackageName) at com.myPackageName.framework.impl.GLGame.onPause (GLGame.java:714) - locked <0x03bec179> (a com.myPackageName) at com.myPackageName.onPause (MyApp.java:39) at android.app.Activity.performPause (Activity.java:7399) at android.app.Instrumentation.callActivityOnPause (Instrumentation.java:1414) at android.app.ActivityThread.performPauseActivityIfNeeded (ActivityThread.java:4115) at android.app.ActivityThread.performPauseActivity (ActivityThread.java:4092) at android.app.ActivityThread.performPauseActivity (ActivityThread.java:4066) at android.app.ActivityThread.handlePauseActivity (ActivityThread.java:4040) at android.app.ActivityThread.-wrap16 (ActivityThread.java) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1708) at android.os.Handler.dispatchMessage (Handler.java:105) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6944) at java.lang.reflect.Method.invoke (Native method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
I'm pretty sure this ANR is caused by a synchronized block, unless line 714 points to super.onPause()
. In your GLGame.onPause (GLGame.java:714)
you either wait on an object in the main thread intentionally, or using a synchronized final variable (that can be a synchronized list, set, map etc...) Since you haven't shared any source code, I cannot tell what is actually causing it without looking.
In a main thread, using this kind of approach may block your thread which is something you definitely don't want (assuming the lock is also used in another thread)
// One example @Override public void onPause() { synchronized(mLock) { // access the variable } } // Another example @Override public void onPause() { synchronizedList.add(new Object()) }
Both of these approaches will be blocked if there is another thread that, at the moment, accessing the lock or the synchronized list, map etc... and doesn't give up the lock. For a solution, I could recommend using ReentrantLock with Lock
interface using like Lock mLock = new ReentrantLock()
and turn your calls into something like this:
@Override public void onPause() { if (mLock.tryLock()){ try{ // do your work here } finally { mLock.unlock(); } } } // Another example @Override public void onPause() { if (mLock.tryLock()){ try{ // The list or set should not be synchronized. // The synchronization must be managed with lock's // "tryLock" method. The "tryLock" also has an // overload with a timeout which you can use // in your background thread, but you must not // use on the main thread. normalList.add(new Object()); } finally { mLock.unlock(); } } }
The variables that are accessed on main thread should not block, or the worker thread should block them as little as possible so main thread can acquire the lock immediately after if you want to ensure synchronization. So I suggest checking your entire code about shared variables and references between main thread and other threads, and only synchronize the changes on the variables instead of an entire code block that takes long to execute.
If you are using following code snippet in activity's onCreate method
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.main);
Just replace these with below lines of code
View view = getLayoutInflater().inflate(R.layout.main, null); view.setKeepScreenOn(true); setContentView(view);
I hope it will help you to get solution.
Happy Coding :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With