Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

multithreading - Using a Thread to run Code and update UI Android

How do I use a thread to run some code continuously whilst an Apps running, using the information it gives to periodically update the UI of the App.

Specifically the thread would run some code that searches through a text file in order to find co-ordinates which would then be plotted over a PNG on the UI. This would update automatically say every second maybe every half second, and would clear the image then redraw the points.

How do i first of all set up the thread then second of all send information from the thread back to the UI and have it update?

Any example code would be great or any information you've come across that gives example code. I'm not trying to do it the best way at the moment, just trying to hack it together, so if you know easy and quick (but awful) ways of doing this don't feel afraid to share.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may help u...

//on create

Thread currentThread = new Thread(this); currentThread.start();

after on create

public void run() {
    try {
        Thread.sleep(4000);

        threadHandler.sendEmptyMessage(0);
    } catch (InterruptedException e) {
        //don't forget to deal with the Exception !!!!!
    }
} 

private Handler threadHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {      


        Intent in = new Intent(getApplicationContext(),****.class);
        startActivity(in);

    }
};  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...