我创建了一个测试你的反射速度的Android应用程序。
起初应用还在 按(状态0)时,应用程序会启动一个等待几秒钟的线程,然后再显示绿屏。 显示绿色屏幕(状态1)时,您必须尽可能快地按下。 如果按绿色(状态2),则会显示反射时间
我遇到的问题当一个人太快点击时,我必须停止导致应用程序崩溃的线程(thread.stop())。 有什么建议吗?
public class MainActivity extends Activity implements OnClickListener{
TextView tvs,tvas;
RelativeLayout rl;
int state; //0=red 1=green 2=tapped-green
long end,start,result;
Thread th;
Boolean dont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RelativeLayout) findViewById(R.id.rl);
tvs = (TextView) findViewById(R.id.tvs);
tvas = (TextView) findViewById(R.id.tvas);
dont=true;
state=0;
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"RobotoCondensed-Bold.ttf");
tvs.setTypeface(myTypeface);
tvas.setTypeface(myTypeface);
rl.setOnClickListener(this);
tvs.setOnClickListener(this);
tvas.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (state) {
case 0:
tvs.setText("Any Min Now");
rl.setBackgroundColor(Color.rgb(255, 68, 68));
final Handler mHandler = new Handler();
state=1;
dont=true;
th = new Thread((new Runnable() {public void run() {while (true){try {Thread.sleep(randInt());mHandler.post(new Runnable() {public void run() {
if(dont){ //not helping
rl.setBackgroundColor(Color.GREEN);
start = SystemClock.uptimeMillis();
state=2;
}
}});} catch (Exception e) {}}}}));
th.start();
break;
case 1:
tvs.setText("Too Soon!");
tvas.setText("Tap AnyWhere To restart");
dont=false;
rl.setBackgroundColor(Color.rgb(255, 68, 68));
state=0;
break;
case 2:
end = SystemClock.uptimeMillis();
result=end-start;
tvs.setText("UR Reaction Time= "+result+"ms");
tvas.setText("Tap AnyWhere To restart");
state=0;
dont=false;
th.interrupt(); //giving me wrong numbers
break;
}
}
public static int randInt() { //creates a random number
Random rand = new Random();
int randomNum = rand.nextInt((6000 - 2000) + 1) + 2000;
return randomNum;
}
}
logcat的:
03-07 22:16:05.575: I/WindowState(840): WIN DEATH: Window{45b08280 u0 com.afm.reflects/com.afm.reflects.MainActivity}
03-07 22:16:05.575: I/ActivityManager(840): Process com.afm.reflects (pid 32055) (adj 11) has died.
03-07 22:16:52.391: V/SmartFaceService - 3rd party pause(840): onReceive
[android.intent.action.ACTIVITY_STATE/com.afm.reflects/create]
03-07 22:17:03.471: E/AndroidRuntime(864): Process: com.afm.reflects, PID: 864
03-07 22:17:03.471: E/AndroidRuntime(864): at com.afm.reflects.MainActivity.onClick(MainActivity.java:73)
03-07 22:17:03.482: W/ActivityManager(840): Force finishing activity com.afm.reflects/.MainActivity
03-07 22:17:03.522: V/SmartFaceService - 3rd party pause(840): onReceive
[android.intent.action.ACTIVITY_STATE/com.afm.reflects/pause]
03-07 22:17:03.562: D/CrashAnrDetector(840): processName: com.afm.reflects
03-07 22:17:03.562: D/CrashAnrDetector(840): broadcastEvent : com.afm.reflects data_app_crash
03-07 22:17:05.634: I/ActivityManager(840): Process com.afm.reflects (pid 864) (adj 9) has died.
03-07 22:17:05.644: I/WindowState(840): WIN DEATH: Window{45514be0 u0 com.afm.reflects/com.afm.reflects.MainActivity}
03-07 22:18:53.869: V/SmartFaceService - 3rd party pause(840): onReceive
[android.intent.action.ACTIVITY_STATE/com.afm.reflects/create]
03-07 22:19:11.867: E/AndroidRuntime(1649): Process: com.afm.reflects, PID: 1649
03-07 22:19:11.867: E/AndroidRuntime(1649): at com.afm.reflects.MainActivity.onClick(MainActivity.java:73)
03-07 22:19:11.877: W/ActivityManager(840): Force finishing activity com.afm.reflects/.MainActivity
03-07 22:19:11.917: D/CrashAnrDetector(840): processName: com.afm.reflects
03-07 22:19:11.917: V/SmartFaceService - 3rd party pause(840): onReceive
[android.intent.action.ACTIVITY_STATE/com.afm.reflects/pause]
03-07 22:19:11.927: D/CrashAnrDetector(840): broadcastEvent : com.afm.reflects data_app_crash