美文网首页
Android 计时器Timer和TimerTask Illeg

Android 计时器Timer和TimerTask Illeg

作者: bytomz | 来源:发表于2022-05-10 17:49 被阅读0次

    原文:Timer任务IllegalStateException异常处理

    本文仅作记录学习使用

    /***************1.解决IllegalStateException:Task already scheduled or cancelled.*****************/

                Field field = null;

                try {

                    field = TimerTask.class.getDeclaredField("state");

                } catch (NoSuchFieldException e) {

                    e.printStackTrace();

                }

                field.setAccessible(true);

                try {

                    field.set(task, 0);

                } catch (IllegalAccessException e) {

                    e.printStackTrace();

                }

                timer.schedule(timerTask,0,100);

    /***************1.解决IllegalStateException:Task already scheduled or cancelled.*****************/

    使用完成之后销毁timer

    if (timer!=null){

                timer.cancel();

                timer=null;

            }

            if (timerTask!=null){

                timerTask.cancel();

                timerTask=null;

            }

    相关文章

      网友评论

          本文标题:Android 计时器Timer和TimerTask Illeg

          本文链接:https://www.haomeiwen.com/subject/rzalurtx.html