我使用以下启动画面代码和onTouchEvent方法。我得到了ANR,不知道该怎么做。该应用程序运行完美,除了偶尔它锁定对不起,活动xxx没有响应。有什么想法吗?
_splashTime = 5000 ; // approx 5 seconds
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime ))
{
sleep( _waitsecs );
if ( _active ) {
waited += _waitsecs ;
}
}
}
catch(InterruptedException e) {
// do nothing with catch
}
finally {
start_program();
finish();
}
}
};
splashTread.start();
答案 0 :(得分:2)
当活动未在5秒内完成操作时,活动没有响应问题。这可能是它有时会出现的原因,有时也不是。你应该看一下http://developer.android.com/guide/practices/design/responsiveness.html的ANR问题。
但一般来说,你不应该从UI启动等待线程。首选解决方案是使用警报等待后台任务或服务。
警报的简短示例可在以下位置找到: Android: How to use AlarmManager
另一种可能的解决方案是启动后台服务,该服务可以完成应用程序的所有初始化。完成后,闪屏被解雇。
答案 1 :(得分:2)
对于基于计时器的任务,这是一个非常简单的解决方案。通过它。
TimerTask timerTask;
Timer timer;
timerTask = new TimerTask() {
@Override
public void run() {
// after 3 seconds this statement excecutes
// write code here to move to next screen or
// do next processing.
}
};
timer = new Timer();
timer.schedule(timerTask, 3 * 1000); // 3 seconds
如果你正确使用它,这种方式不会产生ANR。
-Thanks