发布的较新版本的Android与新设备不兼容

时间:2013-03-06 06:58:53

标签: android performance

我的应用程序在Android 2.3及更低版本上成功运行,但它在android 4.0和4.2中给出了响应null。在google上搜索后,我发现HoneyComb和Ice Cream Sandwich对于滥用UI线程更为严格。

  

ICS和HoneyComb不允许您在UI线程上执行的其他操作的一些示例是:

Opening a Socket connection (i.e. new Socket()).
HTTP requests (i.e. HTTPClient and HTTPUrlConnection).
Attempting to connect to a remote MySQL database.
Downloading a file (i.e. Downloader.downloadFile()).

If you are attempting to perform any of these operations on the UI thread, you must wrap them in a worker thread. The easiest way to do this is to use of an AsyncTask, which allows you to perform asynchronous work on your user interface. An AsyncTask will perform the blocking operations in a worker thread and will publish the results on the UI thread, without requiring you to handle threads and/or handlers yourself.

这意味着我必须更改旧代码吗?我在我的项目中调用HTTP请求超过50次。所以我必须在每个类中手动更改它,因此它将在HTTP请求上运行吗?

任何建议都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

这是因为您在主线程上编写了网络调用。只需使用Asynctask即可。它会减少工作流程,在Android中也称为无痛线程,这意味着用户无需担心线程管理。

答案 1 :(得分:0)

,您必须从主线程中移动所有与网络相关的呼叫。最简单的解决方案是创建AsyncTask并在您拨打HTTPRequest电话的所有地方拨打该电话。让你的AsyncTask在后​​台执行这些操作。

相关问题