線程
AOS要求WebView的調(diào)用線程必須一致忽孽,否則會拋出異常,提示"All WebView methods must be called on the same thread. "裤翩。WebView在初始化時(shí),mWebViewThread會記錄當(dāng)前的初始化線程蔬崩,因?yàn)閣ebview是一個(gè)UI控件绣版,我們一般會在主線程中初始化,所以很多情況下這個(gè)成員變量就是當(dāng)前的主線程辐脖。之后饲宛,在調(diào)用webview的相關(guān)方法時(shí)皆愉,都會首先調(diào)用checkThread()方法檢測線程是否一致嗜价,即檢測兩個(gè)線程的looper對象。如果不相同幕庐,會有異常拋出久锥。
private final Looper mWebViewThread = Looper.myLooper();
private void checkThread() {
// Ignore mWebViewThread == null because this can be called during in the super class
// constructor, before this class's own constructor has even started.
if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) {
Throwable throwable = new Throwable(
"A WebView method was called on thread '" +
Thread.currentThread().getName() + "'. " +
"All WebView methods must be called on the same thread. " +
"(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() +
", FYI main Looper is " + Looper.getMainLooper() + ")");
Log.w(LOGTAG, Log.getStackTraceString(throwable));
StrictMode.onWebViewMethodCalledOnWrongThread(throwable);
if (sEnforceThreadChecking) {
throw new RuntimeException(throwable);
}
}
}