开发者

Thread in Android method onCreate() still stops the UiThread in 10% of the cases

This is my onCreate method of my first activity of my app. It initalizes all Widgets and then starts a Thread to load model data from the internet. In some cases, when the network is really slow or not responding, the "initializeModelThread" seems to take the whole CPU and doesnt allow the onCreate Methode to finish.

In one case I saw that Log.v("Time...) was called after 300 ms, but the screen remained black 开发者_高级运维for about 20 seconds until a HttpException stopped the initalizeModelThread. You already can see that I set the Priority of the Thread to low.

Would the use of AsyncTask help in this case? Do u have any other thoughts about that issue?

public void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);

        Long time = System.currentTimeMillis();

        initializeWidgets();

        Thread threadInstance = new Thread(initializeModelThread);
        threadInstance.setPriority(Thread.MIN_PRIORITY);
        threadInstance.start();

        Log.v("Time", "Difference from start " + Long.toString(System.currentTimeMillis() - time));

      }


AsyncTask would not make a difference here.

Instead of creating the thread on the onCreate() method, do it on the onStart() method.


the problem was that I used the google geo reverse coding feature (GeoCoder) in "initializeWidgets", which downloaded data from the web. It was really fast in 90 % of the cases. Now there never was such a delay again while starting after moving that download into a AsyncTask

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜