如何暂停在Android /延迟? [英] How to pause/delay on Android?

查看:162
本文介绍了如何暂停在Android /延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在学习如何开发Android移动设备的应用程序。

我写了一个测试应用程序,以显示在设备屏幕上数字0-9。我创建了一个简单的函数来拖延数的变化。

然而,在运行的应用程序,则仅显示最终数目。还有一个延迟这最后的数字显示之前。我假设暂停的长度是我定义的延迟乘以的位数来显示。

我如何创建与延迟改变号码的应用程序?

 公共类AndroidProjectActivity延伸活动{
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        主要();
    }无效延迟(INT秒){
    时间长= 0;
    时间= System.currentTimeMillis的();
    而(System.currentTimeMillis的()<时间+(秒* 1000));
}无效的主要(){
    串ConvertedInt;
    TextView的电视=新的TextView(本);
    的setContentView(电视);    对于(INT NewInt = 0;!NewInt = 9; NewInt ++){
        ConvertedInt = Character.toString((炭)(NewInt + 48));
        tv.setText(ConvertedInt);
        延迟(5);
    }
}


解决方案

这样做的一种方法是创建一个可运行的更新您的视图。这仍然会在UI线程上更新,但等待在后台。有可能在低于code错误,但它应该有小的调整运行。

在任何系统的阻塞调用到你的活动是不好的,因为你阻塞UI线程。您的应用程序将被强制关闭,与应用程序没有响应的消息。这里是另一个很好的例如

 公共类AndroidProjectActivity延伸活动{
    私人处理器mHandler;
    私人TextView的mTextView;
    私人Runnable接口mCountUpdater =新的Runnable(){
        私人INT mCount = 0;
        跑() {
           如果(mCount> 9)
               返回;
           mTextView.setText(将String.valueOf(mCount + 48));
           mCount ++;
           //重新安排自己。
           mHandler.postDelayed(这一点,5000);
        }
    }
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //清洁加载从布局的图..
        TextView的电视=新的TextView(本);
        的setContentView(电视);
        mTextView =电视;
        //创建在UI线程处理器。
        mHandler =新的处理程序();
        mHandler.post(mCountUpdater);
    }
}

I am currently learning how to develop applications for Android mobile devices.

I wrote a test application to display numbers 0-9 on the device screen. I created a simple function to delay the number change.

However, upon running the application, only the final number is displayed. There is also a delay before this final number shows. I'm assuming that the length of the pause is my defined delay multiplied by the number of digits to be shown.

How do I create an app that changes the numbers with a delay?

public class AndroidProjectActivity extends Activity {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Main();
    }

void Delay(int Seconds){
    long Time = 0;
    Time = System.currentTimeMillis();
    while(System.currentTimeMillis() < Time+(Seconds*1000));
}

void Main() {
    String ConvertedInt;
    TextView tv = new TextView(this);
    setContentView(tv);

    for(int NewInt = 0; NewInt!= 9; NewInt++){
        ConvertedInt = Character.toString((char)(NewInt+48));
        tv.setText(ConvertedInt);
        Delay(5);
    }
}

解决方案

One way of doing this is to create a runnable that updates your view. This will still update on the UI thread, but wait in the background. There might be mistakes in the below code, but it should run with minor tweaks..

Blocking in any of the system calls into your activity is not good, since you're blocking the UI thread. Your app will be force closed, with an Application Not Responding message. Here is another good example.

public class AndroidProjectActivity extends Activity {
    private Handler mHandler;
    private TextView mTextView;
    private Runnable mCountUpdater = new Runnable() {
        private int mCount = 0;
        run() {
           if(mCount > 9)
               return;
           mTextView.setText(String.valueOF(mCount+48));
           mCount++;
           // Reschedule ourselves.
           mHandler.postDelayed(this, 5000);
        }
    }
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        // Cleaner to load a view from a layout..
        TextView tv = new TextView(this);
        setContentView(tv);
        mTextView = tv;
        // Create handler on UI thread.
        mHandler = new Handler();
        mHandler.post(mCountUpdater);
    }
}

这篇关于如何暂停在Android /延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆