如何在调用列表时解析ANR

时间:2015-07-01 07:44:02

标签: android

我正在开发一个应用程序,其中一个线程将在主线程中运行,并且它将每1秒调用一次用户列表,但我弹出 ANR 。请建议。 如何解决?

以下是我的代码

    super.onCreate(savedInstanceState);
    System.setProperty("java.net.preferIPv4Stack", "true");
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    mHandler = new Handler();

     Runnable runable = new Runnable() { 

            @Override 
            public void run() { 
                try{
                     listofUsers = GcmIntentService.getListOfUsers();
                    if (listofUsers.size() > 0) {
                        for (int index = 0; index < listofUsers.size(); index++) {
                            if (index == 0) {
                                startTime = listofUsers.get(0).getLoggedinDateTime();
                            } else {
                                Calendar calender1 = Calendar.getInstance();
                                calender1.setTime(startTime);
                                calender1.add(Calendar.SECOND, i);
                                newTime = calender1.getTime();
                                listofUsers.get(index).setLoggedinDateTime(newTime);
                                i = i + 72;

                            }
                            ///user time is checked with the current system time.
                            Iterator<UserInformation> userDetailsIter = listofUsers.iterator();
                            Calendar calender2 = Calendar.getInstance();
                            while (userDetailsIter.hasNext()) {
                                UserInformation newUserInfo = userDetailsIter.next();
                                Date userTime=newUserInfo.getLoggedinDateTime();
                                Date systemTime=calender2.getTime();
                                if ( userTime.compareTo(systemTime) < 0 ) {
                                    userDetailsIter.remove();
                                }
                            }
                        }
                    }

                    mHandler.postDelayed(this, 1000);
                }
                catch (Exception e) {
                    // TODO: handle exception
                }
                finally{
                    //also call the same runnable 
                    mHandler.postDelayed(this, 100); 
                }
            } 
        }; 
        mHandler.postDelayed(runable, 100); 
    }

请指导我做错了什么来帮助我

1 个答案:

答案 0 :(得分:0)

您无法在主队列

上进行while循环或任何类型的长时间运行操作

为什么不在重复计时器中制作正常的线程

如下面的代码

Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                check();
            }

        }, 0, 100);

并且在内部检查中创建一个线程并执行您想要的任何操作

public void check(){
    new Thread(new new Runnable() {

            @Override
            public void run() {
                // do what you want here

            }
        }).start();
}