Skip to content

Commit

Permalink
Merge pull request #184 from sdarchy/master
Browse files Browse the repository at this point in the history
fixbug WebView loadurl方法在19以上超过2097152个字符失效
  • Loading branch information
uknownothingsnow authored Nov 4, 2019
2 parents 32242c6 + c3b3e1f commit 506b36d
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.collection.ArrayMap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
Expand All @@ -25,6 +26,11 @@
@SuppressLint("SetJavaScriptEnabled")
public class BridgeWebView extends WebView implements WebViewJavascriptBridge, BridgeWebViewClient.OnLoadJSListener {

private final int URL_MAX_CHARACTER_NUM=2097152;
public static final String toLoadJs = "WebViewJavascriptBridge.js";
Map<String, CallBackFunction> responseCallbacks = new HashMap<String, CallBackFunction>();
Map<String, BridgeHandler> messageHandlers = new HashMap<String, BridgeHandler>();
BridgeHandler defaultHandler = new DefaultHandler();
private Map<String, OnBridgeCallback> mCallbacks = new ArrayMap<>();

private List<Object> mMessages = new ArrayList<>();
Expand Down Expand Up @@ -192,7 +198,11 @@ private void dispatchMessage(Object message) {
String javascriptCommand = String.format(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson);
// 必须要找主线程才会将数据传递出去 --- 划重点
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
this.loadUrl(javascriptCommand);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT&&javascriptCommand.length()>=URL_MAX_CHARACTER_NUM) {
this.evaluateJavascript(javascriptCommand,null);
}else {
this.loadUrl(javascriptCommand);
}
}
}

Expand Down

0 comments on commit 506b36d

Please sign in to comment.