Problem description
Cocos2d-x Game Project Android Project Access SDK, after the successful payment, Java code callback Lua method, generated the ANR.
How to position ANR?
Records and ANR error messages in the Data/anr/traces.txt file. You can use the RE manager to view the file.
In the log information, you can see how the Cocos2d-x audio engine plays the sound effect called in the callback method. This is where the ANR is produced. I'm using cocos2d-x2.1.5 here.
After finding the source of the problem, I commented on the code that played the sound. And then run again, there is no ANR generated.
However, another problem arises ...
An OpenGL error was reported, and the creation of the Ccsprite failed. This problem typically occurs because Ccspirte is not created in the GL thread.
Main thread vs. GL Threads
Main thread (UI thread): Threads created when the app starts, other threads are child threads of the thread, and are primarily used to update the UI's threads.
GL Thread: A child thread of the main thread that is used primarily to update the GUI thread. In Cocos2d-x, a GL thread is separated from the main thread for work related to screen rendering (to ensure smooth graphics).
Under Android, activity has a runonuithread method, which is used to perform a task in the main thread. Note that if the task is time-consuming it will produce ANR.
The declaration of the method is as follows:
public void Runonuithread (Runnable task);
OpenGL's rendering under Android needs to deal with Glsurfaceview. So the cocos2d-x encapsulates a cocos2dxglsurfaceview. The view is related to the GL thread. Cocos2dxactivity contains a cocos2dxglsurfaceview and provides a Runonglthread method for performing a task in a GL thread.
The method declaration is as follows:
public void Runonglthread (final Runnable Task);
Implementation on the Glsurfaceview is called by the Queueevent method to implement communication with the GL thread. The implementation mechanism is also a message poll under Android.
Finally, by using the Runonglthread method to callback the Lua method in the GL thread, the problem is solved completely.
Ctx.runonglthread (New Runnable () {@Overridepublic void run () {paytools.paycallback ();//lua method needs to be called in GL Thread}});
"Android" Locates and resolves the ANR error record