开发者

ANR Exception handling

开发者 https://www.devze.com 2023-04-12 07:27 出处:网络
I have an application, where we are providing the Remote UI(which contains all the butto开发者_JS百科ns to control the Media server).

I have an application, where we are providing the Remote UI(which contains all the butto开发者_JS百科ns to control the Media server).

The problem is when we click any of the button, we are executing the corresponding action, which is very long UPNP network operation.

so when we press the buttons continuously , finally the device comes up with ANR Exception and force close the application. I made some research on this ANR Exception and finally found that, we can use Thread or AsyncTask to solve this problem.

But in my application since we are providing so many buttons, when user presses buttons continuously , it may inturn lead to lot of threads created in the application.

Please give me your suggestions on this.

How to overcome this problem?

Thanks


One of the many advantages of using AsyncTask is that it manages the threading (and thread pooling) for you. So if you use AsyncTask, you shouldn't have the problem of creating too many threads.

In addition, if you're concerned with creating too many AsyncTasks, consider putting the tasks in a member variable (such as a Queue or ArrayList) and keeping track of their state. If one is still processing it might not be necessary to start another. Or you can remove tasks whose results are no longer needed.


Just keeping track of the button click in member variable and using One AsyncTask you can perform this long running operation in queue wise.

If possible just avoid multi-threads for these operations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号