Keeping Your App Responsive
2013-10-26 14:01
232 查看
阅读:http://developer.android.com/training/articles/perf-anr.htm
总的来说,要避免程序停止响应,就是不要再UI线程里进行工作,如果有需要长时间操作的,例如IO请求,数据库操作,下载等等,应该放在work threak.
避免APP无响应的方法,最简便的就是使用AsyncTask。
The most effecive way to create a worker thread for longer operations is with the
在onProgressUpdate可以获取当前进度。
当然,你也可以使用Thread。
Although it's more complicated than
If you implement
The specific constraint on
如果你需要这么做,请记得修改Thread的优先级别,以及提供一个Handler使得UI线程与work thread能够进行信息交互,因为所有空间的操作都必须在UI线程里。
总的来说,要避免程序停止响应,就是不要再UI线程里进行工作,如果有需要长时间操作的,例如IO请求,数据库操作,下载等等,应该放在work threak.
避免APP无响应的方法,最简便的就是使用AsyncTask。
The most effecive way to create a worker thread for longer operations is with the
AsyncTaskclass. Simply extend
AsyncTaskand implement the
doInBackground()method to perform the work. To post progress changes to the user, you can call
publishProgress(), which invokes the
onProgressUpdate()callback method. From your implementation of
onProgressUpdate()(which runs on the UI thread), you can notify the user.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { // Do the long-running work in here protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } // This is called each time you call publishProgress() protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } // This is called when doInBackground() is finished protected void onPostExecute(Long result) { showNotification("Downloaded " + result + " bytes"); } }
在onProgressUpdate可以获取当前进度。
当然,你也可以使用Thread。
Although it's more complicated than
AsyncTask, you might want to instead create your own
Threador
HandlerThreadclass. If you do, you should set the thread priority to "background" priority by calling
Process.setThreadPriority()and passing
THREAD_PRIORITY_BACKGROUND. If you don't set the thread to a lower priority this way, then the thread could still slow down your app because it operates at the same priority as the UI thread by default.
If you implement
Threador
HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call
Thread.wait()or
Thread.sleep(). Instead of blocking while waiting for a worker thread to complete, your main thread should provide a
Handlerfor the other threads to post back to upon completion. Designing your application in this way will allow your app's UI thread to remain responsive to input and thus avoid ANR dialogs caused by the 5 second input event timeout.
The specific constraint on
BroadcastReceiverexecution time emphasizes what broadcast receivers are meant to do: small, discrete amounts of work in the background such as saving a setting or registering a
Notification. So as with other methods called in the UI thread, applications should avoid potentially long-running operations or calculations in a broadcast receiver. But instead of doing intensive tasks via worker threads, your application should start an
IntentServiceif a potentially long running action needs to be taken in response to an intent broadcast.
如果你需要这么做,请记得修改Thread的优先级别,以及提供一个Handler使得UI线程与work thread能够进行信息交互,因为所有空间的操作都必须在UI线程里。
相关文章推荐
- Android如何避免ANR 增加相应速度 Keeping Your App Responsive
- Android - 《Keeping Your App Responsive》
- Keeping Your App Responsive(ANR)
- Packaging your Shiny App as an Windows desktop app
- iOS Programming 101: How To Send Email in Your iPhone App
- 5 Tips when Scaling Your Website or App to 1,000,000 Users
- Looks like the Spring listener was not configured for your web app
- Build Your First Mobile App With Ionic 2 & Angular 2 - Part 6
- Redesign Your App for iOS 7 之 页面布局
- Redesign Your App for iOS 7 之 页面布局
- Redesign Your App for iOS 7 之 页面布局
- 25 Creative Ways to Promote Your App for Free
- Redesign Your App for iOS 7 之 页面布局
- "Because an app is obscuring a permission request, Settings can't verify your response"
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file
- Best Practices for performence - Managing your app's memory
- Installing your app on your Windows RT device
- Your First iOS App(3)——App Development Process
- Your First iOS app(14)--Finding Information
- Your app must be published under a seller name and company name that reflects the XXXXX brand解决办法