Open IceDcap opened 6 years ago
尝试在退出时显示的调用Choreographer的静态方法releaseInstance
将不会产生警告信息sending message to a Handler on a dead thread
以及上述严格模式抛出的dispose not called
的问题,具体代码如下
case QUIT:
if (what == QUIT) {
removeCallbacksAndMessages(null);
}
quitFlag = true;
syncTimerIfNeeded();
pausedPosition = timer.currMillisecond;
if (mUpdateInSeparateThread) {
notifyRendering();
quitUpdateThread();
}
if (mFrameCallback != null) {
if (Build.VERSION.SDK_INT >= VERSION_CODES.N) {
Choreographer.getInstance().removeFrameCallback(mFrameCallback);
try {
Method method = Choreographer.class.getMethod("releaseInstance");
method.invoke(null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN){
Choreographer.getInstance().removeFrameCallback(mFrameCallback);
Choreographer old = Choreographer.getInstance();
try {
Field sThreadInstance = Choreographer.class.getDeclaredField("sThreadInstance");
sThreadInstance.setAccessible(true);
ThreadLocal<Choreographer> instance = (ThreadLocal<Choreographer>)sThreadInstance.get(null);
instance.remove();
Field field = Choreographer.class.getDeclaredField("mDisplayEventReceiver");
field.setAccessible(true);
Object object = field.get(old);
Method method = object.getClass().getMethod("dispose");
method.invoke(object, null);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
if (what == QUIT){
if (this.drawTask != null){
this.drawTask.quit();
}
if (mParser != null) {
mParser.release();
}
if (this.getLooper() != Looper.getMainLooper())
this.getLooper().quit();
}
break;