Android应用无响应(ANR) [英] Android app not responding (ANR)

查看:244
本文介绍了Android应用无响应(ANR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,它侦听服务器上的套接字并以json格式检索数据并将数据保存在数据库中.资源游标适配器用于在列表中显示数据.当应用程序闲置10分钟或更长时间时,它没有响应.有解决方案吗?

I have an android app, which listens to a socket from the server and retrieves data in json format and save data in the database. Resource cursor adapter is used to display the data in a list. When the app is idle for 10 minutes or more, it is not reponding. Any solutions?

推荐答案

ANR在主线程被阻塞几次时发生.具体来说,在Activity中为5秒,在BroadcastReceiver中为10秒,在Service中为20秒.因此,为避免使用ANR,您需要确保在UI线程中不要执行以下操作:reading or writing filesconnecting the internetoperating databases等,这会花费大量时间.因此,如果您想执行上述操作,则可以启动一个新线程来执行此操作.具体来说,AsyncTask ThreadHandler之类.

ANR occurs when the main thread is blocked for a few time. Specifically, 5 seconds in an Activity, 10 seconds in a BroadcastReceiver and 20 seconds in a Service. So, to avoid ANR, you need to ensure that you don't do something like these in you UI thread: reading or writing files, connecting the internet, operating databases and so on, which spend a lot of time. So, if you want to do things above, you may start a new thread to do that. Specifically, AsyncTask ThreadHandler and something like that.

我希望这会对您有所帮助.

I hope this will help you.

这篇关于Android应用无响应(ANR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆