Android每秒都会从处理程序更新ui

Android每秒都会从处理程序更新ui,第1张

概述我需要一点帮助,每秒从Runnable / Handler更新我的UI.我正在使用此代码: Runnable runnable = new Runnable() { @Override public void run() { handler.post(new Runnable() { @Over 我需要一点帮助,每秒从Runnable / Handler更新我的UI.我正在使用此代码:
Runnable runnable = new Runnable() {        @OverrIDe        public voID run() {                handler.post(new Runnable() {                    @OverrIDe                    public voID run() {                        prbar.setProgress(myProgress);                        y = (double) ( (double) myProgress/ (double) RPCCommunicator.totalPackets)*100;                        txtInfoSync1.setText(Integer.toString((int)y) + "%");                        prbar.setMax(RPCCommunicator.totalPackets);                        int tmp = totalBytesReceived - timerSaved;                        Log.w("","totalBytesReceived : "+totalBytesReceived + " timerSaved : "+timerSaved );                        Log.w("","tmp : "+tmp);                        if (avgSpeedCalc.size() > 10)                        {                            avgSpeedCalc.remove(0);                        }                        avgSpeedCalc.add(tmp);                        int x = 0;                        for (int y=0;y<avgSpeedCalc.size();y++)                        {                            x += avgSpeedCalc.get(y);                            Log.d("","x : "+x);                        }                        x = Math.round(x/avgSpeedCalc.size());                        Log.e("","x : "+x);                        timerSaved = totalBytesReceived;                        txtSpeed.setText(Integer.toString(x));                    }                });        }    };

我尝试使用handler.postDelayed(runnable,1000);在onCreate()中,但是runnable永远不会启动.或者即使我尝试使用runnable.run();,它仍然无法正常工作.

任何想法我怎么能开始runnable / handler并每秒更新一次ui?

解决方法 为什么要在runnable中创建runnable?

试试这个:

// flag that should be set true if handler should stopboolean mStopHandler = false;Runnable runnable = new Runnable() {    @OverrIDe    public voID run() {        // do your stuff - don't create a new runnable here!        if (!mStopHandler) {            mHandler.postDelayed(this,1000);        }    }};// start it with:mHandler.post(runnable);
总结

以上是内存溢出为你收集整理的Android每秒都会从处理程序更新ui全部内容,希望文章能够帮你解决Android每秒都会从处理程序更新ui所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1139092.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存