7
int delay = 1000; // delay for 1 sec. 
int period = 10000; // repeat every 10 sec. 
Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run() 
        { 
            displayData();  // display the data
        } 
    }, delay, period);  

和别的:

while(needToDisplayData)
{
    displayData(); // display the data
    Thread.sleep(10000); // sleep for 10 seconds
}   

它们都不起作用(应用程序被强制关闭)。我可以尝试哪些其他选择?

4

5 回答 5

27

您的代码失败,因为您在后台线程中执行睡眠,但显示数据必须在 UI 线程中执行。

您必须从 runOnUiThread(Runnable) 运行 displayData 或定义处理程序并向其发送消息。

例如:

(new Thread(new Runnable()
        {

            @Override
            public void run()
            {
                while (!Thread.interrupted())
                    try
                    {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() // start actions in UI thread
                        {

                            @Override
                            public void run()
                            {
                                displayData(); // this action have to be in UI thread
                            }
                        });
                    }
                    catch (InterruptedException e)
                    {
                        // ooops
                    }
            }
        })).start(); // the while thread will start in BG thread
于 2012-07-27T11:50:51.280 回答
9

使用onPostDelayed()从您的任何ViewHandler访问。您可以通过不创建 aTimer或 new来节省内存Thread

private final Handler mHandler = new Handler();

private final Runnable mUpdateUI = new Runnable() {
    public void run() {
        displayData();
        mHandler.postDelayed(mUpdateUI, 1000); // 1 second
        }
    }
};

mHandler.post(mUpdateUI);
于 2012-07-27T12:01:24.177 回答
1

试试这个 :

@Override                    
    public void run() {   
            TextView tv1 = (TextView) findViewById(R.id.tv);
            while(true){
               showTime(tv1);                                                                
               try {
                   Thread.sleep(1000);
               }catch (Exception e) {
                   tv1.setText(e.toString());
               }           
            } 
    }       

你也可以试试这个

您还可以使用另一种方法在特定时间间隔内更新 UI。以上两个选项都是正确的,但取决于您可以使用替代方法在特定时间间隔更新 UI 的情况。

首先为 Handler 声明一个全局变量以从 Thread 更新 UI 控件,如下所示

处理程序 mHandler = new Handler(); 现在创建一个 Thread 并使用 while 循环使用线程的 sleep 方法定期执行任务。

new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();
于 2012-07-27T11:47:56.080 回答
0

你犯了几个错误:

  1. 你不应该在主线程上调用 Thread.sleep() (你也不应该长时间阻塞它)。一旦主线程被阻塞超过 5 秒,就会发生 ANR(应用程序无响应)并强制关闭。

  2. 你应该避免在 android 中使用 Timer。改用处理程序。handler 的好处是它是在主线程上创建的 -> 可以访问 Views(不像 Timer,它在自己的线程上执行,不能访问 Views)。

    class MyActivity extends Activity {
    
        private static final int DISPLAY_DATA = 1;
        // this handler will receive a delayed message
        private Handler mHandler = new Handler() {
            @Override
            void handleMessage(Message msg) {
                if (msg.what == DISPLAY_DATA) displayData();
            }
         };
    
         @Override
         void onCreate(Bundle b) {
             //this will post a message to the mHandler, which mHandler will get
             //after 5 seconds
             mHandler.postEmptyMessageDelayed(DISPLAY_DATA, 5000);
         }
     } 
    
于 2012-07-27T12:15:16.090 回答
0

当我试图解决不能在 Android 的 DigitalClock 小部件中隐藏秒数的问题时,我遇到了这个线程。DigitalClock 现在已弃用,现在推荐使用的小部件是 TextClock。这对旧 API 不起作用......因此我不得不编写自己的 24 小时制时钟。我不知道这是否是一个好的实现,但它似乎有效(并且每秒更新一次):

    import java.util.Calendar;

    import android.os.Handler;
    import android.view.View;
    import android.widget.TextView;


    /**
     * A 24 hour digital clock represented by a TextView
     * that can be updated each second. Reads the current
     * wall clock time.
     */
    public class DigitalClock24h {

            private TextView mClockTextView; // The textview representing the 24h clock
            private boolean mShouldRun = false; // If the Runnable should keep on running

            private final Handler mHandler = new Handler();

            // This runnable will schedule itself to run at 1 second intervals
            // if mShouldRun is set true.
            private final Runnable mUpdateClock = new Runnable() {
                    public void run() {
                            if(mShouldRun) {
                                    updateClockDisplay(); // Call the method to actually update the clock
                                    mHandler.postDelayed(mUpdateClock, 1000); // 1 second
                            }
                    }
            };


            /**
             * Creates a 24h Digital Clock given a TextView.
             * @param clockTextView
             */
            public DigitalClock24h(View clockTextView) {
                    mClockTextView = (TextView) clockTextView;
            }

            /**
             * Start updating the clock every second.
             * Don't forget to call stopUpdater() when you
             * don't need to update the clock anymore.
             */
            public void startUpdater() {
                    mShouldRun = true;
                    mHandler.post(mUpdateClock);
            }

            /**
             * Stop updating the clock.
             */
            public void stopUpdater() {
                    mShouldRun = false;
            }


            /**
             * Update the textview associated with this
             * digital clock.
             */
            private void updateClockDisplay() {

                    Calendar c = Calendar.getInstance();
                    int hour = c.get(Calendar.HOUR_OF_DAY); // 24 hour
                    int min = c.get(Calendar.MINUTE); 

                    String sHour;
                    String sMin;

                    if(hour < 10) {
                            sHour = "0" + hour;
                    } else sHour = "" + hour;

                    if(min < 10) {
                            sMin = "0" + min;
                    } else sMin = "" + min;

                    mClockTextView.setText(sHour + ":" + sMin);
            }

    }

谢谢你 biegleux 指点我,我想,正确的方向!

于 2013-12-13T19:43:12.753 回答