安卓:onFinish()在CountDownTimer之前延迟 [英] Android: Delay before onFinish() in CountDownTimer
问题描述
我试图创建一个简单的点击计数器的应用程序,但我在最后得到一个明显的滞后它移动到onFinish()之前,给用户一对额外的水龙头停止柜台前。
下面是MainActivity.java
包com.example.tapcounter;进口android.os.Bundle;
进口android.os.CountDownTimer;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;
进口android.app.Activity;公共类MainActivity扩展活动
{
TextView的时间;
TextView的水龙头;
按钮B;INT标志= 0;
诠释计数= 0,finalTap = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
时间=(的TextView)findViewById(R.id.textView1);
水龙头=(的TextView)findViewById(R.id.textView2);
B =(按钮)findViewById(R.id.button1); b.setOnClickListener(新OnClickListener()
{ @覆盖
公共无效的onClick(查看arg0中)
{
如果(finalTap == 0)
{
如果(标志== 0)
{
beginTimer();
标志= 1;
}
使用UpdateCount();
}
}
});
}私人无效beginTimer()
{
新CountDownTimer(10000,1000)
{ 公共无效onTick(长millisUntilFinished)
{
time.setText(时间:+ millisUntilFinished / 1000);
} 公共无效onFinish()
{
time.setText(超时!!!);
finalTap ++;
}
}。开始();
}私人无效使用UpdateCount()
{
taps.setText(水龙头+ Integer.toString(++计数));
}
}
这是第一个你是如何运行你的应用程序?我注意到,当我在Debugmode运行一个应用程序,调试器吃我的移动设备性能的50%。所以,如果你只是运行应用程序的onFinish工作快得多。
第二点是手动检测超时在onTick方法的块一段时间后,水龙头用布尔
私人布尔tapBlock = FALSE;私人无效beginTimer()
{
新CountDownTimer(10100,1000)
{
公共无效onTick(长millisUntilFinished)
{
如果(!tapBlock)
{
time.setText(时间:+ millisUntilFinished / 1000);
如果(millisUntilFinished&小于100)
{
tapBlock = TRUE;
}
}
} 公共无效onFinish()
{
time.setText(超时!!!);
finalTap ++;
tapBlock = FALSE;
}
}。开始();
}
这是一个有点绕,但它也许更快,你必须在tapBlock添加到更新方法
I was trying to create a simple Tap Counter app but i am getting a noticeable lag at the end before it moves to the onFinish(), giving user a couple of extra taps before stopping the counter.
Here is the MainActivity.java
package com.example.tapcounter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity
{
TextView time;
TextView taps;
Button b;
int flag = 0;
int count = 0, finalTap = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.textView1);
taps = (TextView) findViewById(R.id.textView2);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
if(finalTap==0)
{
if(flag==0)
{
beginTimer();
flag=1;
}
updateCount();
}
}
});
}
private void beginTimer()
{
new CountDownTimer(10000, 1000)
{
public void onTick(long millisUntilFinished)
{
time.setText("Time: "+millisUntilFinished/1000);
}
public void onFinish()
{
time.setText("Timeout!!!");
finalTap++;
}
}.start();
}
private void updateCount()
{
taps.setText("Taps: " + Integer.toString(++count));
}
}
on the first how did you run your App? I noticed that when i run an App in Debugmode, the Debugger eats 50% of the performance of my mobile Device. So if you just run your App the onFinish works much faster.
A second point is to detect the timeout manually in the onTick Method an block the taps after some time with a boolean
private boolean tapBlock = false;
private void beginTimer()
{
new CountDownTimer(10100, 1000)
{
public void onTick(long millisUntilFinished)
{
if (!tapBlock)
{
time.setText("Time: "+millisUntilFinished/1000);
if (millisUntilFinished<100)
{
tapBlock = true;
}
}
}
public void onFinish()
{
time.setText("Timeout!!!");
finalTap++;
tapBlock = false;
}
}.start();
}
This is a bit around but its maybe faster and you have to add the "tapBlock" to the update method
这篇关于安卓:onFinish()在CountDownTimer之前延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!