本文介绍了Android的开机画面造成ANR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的闪屏code与onTouchEvent方法。我越来越ANR的,不知道该怎么办。该应用程序完美的作品,除了偶尔它与一个对不起,活动XXX没有响应锁定。任何想法?

  _splashTime = 5000; //,5秒
   公共布尔onTouchEvent(MotionEvent事件){
    如果(event.getAction()== MotionEvent.ACTION_DOWN){
        _active = FALSE;
    }
    返回true;
   }  螺纹splashTread =新主题(){
  @覆盖
  公共无效的run(){
    尝试{
        INT等待= 0;
        而(_active&安培;及(等待< _splashTime))
        {
            睡眠(_waitsecs);
            如果(_active){
                等待+ = _waitsecs;
            }
        }
    }
    赶上(InterruptedException的E){
        //什么也不做与抓
    }
    最后{
        start_program();
        完();
    }
}
 };
splashTread.start();


解决方案

这是基于计时器任务非常简单的解决方案。通过它去。

 的TimerTask TimerTask的;
    定时器定时器;    TimerTask的=新的TimerTask(){        @覆盖
        公共无效的run(){
            //3秒这句话后excecutes
            //写$ C $这里c键移动到下一个画面或
            //做下一步处理。
        }
    };    定时器=新定时器();
    timer.schedule(TimerTask的,3 * 1000); //3秒

如果您使用它有道这种方式不会产生ANR。

- 谢谢

I am using the following splash screen code with an onTouchEvent method. I am getting ANR's and do not know what to do. The app works perfectly, except occasionally it locks up with a Sorry, Activity xxx is not responding. Any ideas?

   _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();
解决方案

This is very simpler solution for timer based task. go through it.

    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

This way won't create ANR if you use it proper way.

-Thanks

这篇关于Android的开机画面造成ANR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 09:47