Android Toast 两个 Crash
地址:http://tao93.top/2018/12/22/Android%20Toast%20%E4%B8%A4%E4%B8%AA%20Crash/
Toast 是 Android 系统一种非常简单的提示性小工具,最近我尝试修复 Toast 相关的两种 Crash,所以把相关的原委和过程记录了下来。先来看一下第一种 Crash 的 log:
1 2 3 4 5 6 7 8 9 10 |
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e2815e is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:679) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.widget.Toast$TN.handleShow(Toast.java:459) at android.widget.Toast$TN$2.handleMessage(Toast.java:342) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) |
上面的 stack trace 中的代码全部是关于 UI 线程中处理一个消息的,这个消息需要做的是把 Toast 需要显示的 view 添加到 WindowManager 中,从而可以显示出来。这样的 crash 是没有任何 app 代码牵涉其中的,所以无法确定是 app 何处的代码导致的这个 crash。我们先来看看 Toast 显示的大致过程。
首先是通过 Toast#makeText
方法或者 Toast
构造函数创建 Toast 对象,然后就可以调用它的 show 方法了。不过此方法是异步的,它仅仅是将该 toast 添加到一个队列中,等待显示,即此方法不等 toast 真正显示就已经返回了,而 toast 的显示需要用一个新的 UI 线程消息中的代码来显示出来。
Toast#show
方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Show the view for the specified duration. */ public void show() { if (mNextView == null) { throw new RuntimeException("setView must have been called"); } INotificationManager service = getService(); String pkg = mContext.getOpPackageName(); TN tn = mTN; tn.mNextView = mNextView; try { service.enqueueToast(pkg, tn, mDuration); } catch (RemoteException e) { // Empty } } |
上面代码的第 15 行是一个跨进程调用了 NotificationServiceManager 的方法,而作为参数的 TN 对象是实现了 IInterface 的,所以可以通过 Binder 传给其他进程。Toast 真正的显示,需要等 NotificationServiceManager 回调回来,这个回调也就是调用 Toast 内部类 TN 的 show 方法。而从 api 25 开始,此方法还会将 NotificationServiceManager 产生的一个 window token 传递过来。
api 25 中的 TN#show
方法:
1 2 3 4 5 6 7 8 |
/** * schedule handleShow into the right thread */ @Override public void show(IBinder windowToken) { if (localLOGV) Log.v(TAG, "SHOW: " + this); mHandler.obtainMessage(0, windowToken).sendToTarget(); } |
api 24 中的 TN#show
方法:
1 2 3 4 5 6 7 8 |
/** * schedule handleShow into the right thread */ @Override public void show() { if (localLOGV) Log.v(TAG, "SHOW: " + this); mHandler.post(mShow); } |
此 TN#show
方法是被远程调用的,所以实际会运行在 app 的 Binder 线程池的线程中,所以此方法向主线程发了一个消息,这个消息才是真正让 toast 显示的地方。不同的是 api 25 的代码还会把 window token 也传递到消息中。处理这个消息的代码,会调用 TN#handleShow
方法,这个 handleShow
是下面这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public void handleShow() { if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView + " mNextView=" + mNextView); if (mView != mNextView) { // remove the old view if necessary handleHide(); mView = mNextView; Context context = mView.getContext().getApplicationContext(); String packageName = mView.getContext().getOpPackageName(); if (context == null) { context = mView.getContext(); } mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); // We can resolve the Gravity here by using the Locale for getting // the layout direction final Configuration config = mView.getContext().getResources().getConfiguration(); final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection()); mParams.gravity = gravity; if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) { mParams.horizontalWeight = 1.0f; } if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) { mParams.verticalWeight = 1.0f; } mParams.x = mX; mParams.y = mY; mParams.verticalMargin = mVerticalMargin; mParams.horizontalMargin = mHorizontalMargin; mParams.packageName = packageName; mParams.removeTimeoutMilliseconds = mDuration == Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT; if (mView.getParent() != null) { if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this); mWM.removeView(mView); } if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this); |