The application may be doing too much work on its main thread

前端 未结 17 1694
挽巷
挽巷 2020-11-21 07:34

I am new to Android SDK/API environment. It\'s the first I am trying to draw a plot/chart. I tried running different kinds of sample codes the emulator using 3 different fre

相关标签:
17条回答
  • 2020-11-21 07:59

    Optimize your images ... Dont use images larger than 100KB ... Image loading takes too much CPU and cause your app hangs .

    0 讨论(0)
  • 2020-11-21 08:01

    Another common cause of delays on UI thread is SharedPreferences access. When you call a PreferenceManager.getSharedPreferences and other similar methods for the first time, the associated .xml file is immediately loaded and parsed in the same thread.

    One of good ways to combat this issue is triggering first SharedPreference load from the background thread, started as early as possible (e.g. from onCreate of your Application class). This way the preference object may be already constructed by the time you'd want to use it.

    Unfortunately, sometimes reading a preference files is necessary during early phases of startup (e.g. in the initial Activity or even Application itself). In such cases it is still possible to avoid stalling UI by using MessageQueue.IdleHandler. Do everything else you need to perform on the main thread, then install the IdleHandler to execute code once your Activity have been fully drawn. In that Runnable you should be able to access SharedPreferences without delaying too many drawing operations and making Choreographer unhappy.

    0 讨论(0)
  • 2020-11-21 08:01

    First read the warning. It says more load on main thread. So what you have to do is just run functions with more work in a thread.

    0 讨论(0)
  • 2020-11-21 08:03

    In my case, it was because I had accidentally set a breakpoint on a method. Once I cleared it, the message went away and performance improved a lot.

    0 讨论(0)
  • 2020-11-21 08:03

    My app had same problem. But it was not doing other than displaying list of cards and text on it. Nothing running in background. But then after some investigation found that the image set for card background was causing this, even though it was small(350kb). Then I converted the image to 9patch images using http://romannurik.github.io/AndroidAssetStudio/index.html.
    This worked for me.

    0 讨论(0)
  • 2020-11-21 08:04

    this usually happens when you are executing huge processes in main thread. it's ok to skip frames less than 200. but if you have more than 200 skipped frames, it can slow down you're application UI thread. what you can do is to do these processes in new thread called worker thread and after that, when you want to access and do sth with UI thread(ex: do something with views, findView etc...) you can use handler or runOnUiThread(I like this more) in order to display the processing results. this absolutely solves the problem. using worker threads are very useful or even must be used when it comes to this cases.

    0 讨论(0)
提交回复
热议问题