提交 745ecbdc 编写于 作者: C cgspine

add demo to handle webview scheme.

上级 6896d3da
......@@ -12,6 +12,11 @@
<div>
<input type="button" id="enter" value="发消息给Native" onclick="testClick();"/>
<div>
<a href="qmui://tab?mode=2&name=你好">scheme 跳转新界面</a>
</div>
</div>
<div id="message_console">
......
......@@ -124,7 +124,8 @@ public class QDTabSegmentScrollableModeFragment extends BaseFragment {
Bundle args = getArguments();
if(args != null){
int mode = args.getInt("mode");
Toast.makeText(getContext(), "mode = " + mode, Toast.LENGTH_SHORT).show();
String name = args.getString("name");
Toast.makeText(getContext(), "mode = " + mode + "; name = " + name, Toast.LENGTH_SHORT).show();
}
}
}
......
......@@ -16,9 +16,12 @@
package com.qmuiteam.qmuidemo.fragment.lab;
import android.annotation.TargetApi;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.Toast;
import com.qmuiteam.qmui.skin.QMUISkinManager;
......@@ -34,6 +37,7 @@ import com.qmuiteam.qmuidemo.R;
import com.qmuiteam.qmuidemo.fragment.QDWebExplorerFragment;
import com.qmuiteam.qmuidemo.lib.Group;
import com.qmuiteam.qmuidemo.lib.annotation.Widget;
import com.qmuiteam.qmuidemo.manager.QDSchemeManager;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -93,25 +97,42 @@ public class QDWebViewBridgeFragment extends QDWebExplorerFragment {
@Override
protected QMUIWebViewClient getWebViewClient() {
return new QMUIBridgeWebViewClient(needDispatchSafeAreaInset(), false,
new QMUIWebViewBridgeHandler(mWebView) {
@Override
protected JSONObject handleMessage(String message) {
try {
JSONObject json = new JSONObject(message);
String id = json.getString("id");
String info = json.getString("info");
Toast.makeText(getContext(), "id = " + id + "; info = " + info, Toast.LENGTH_SHORT).show();
JSONObject result = new JSONObject();
result.put("code", 100);
result.put("message", "Native 的执行结果");
return result;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
});
QMUIWebViewBridgeHandler handler = new QMUIWebViewBridgeHandler(mWebView) {
@Override
protected JSONObject handleMessage(String message) {
try {
JSONObject json = new JSONObject(message);
String id = json.getString("id");
String info = json.getString("info");
Toast.makeText(getContext(), "id = " + id + "; info = " + info, Toast.LENGTH_SHORT).show();
JSONObject result = new JSONObject();
result.put("code", 100);
result.put("message", "Native 的执行结果");
return result;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
};
return new QMUIBridgeWebViewClient(needDispatchSafeAreaInset(), false, handler){
@Override
@TargetApi(21)
protected boolean onShouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(QDSchemeManager.getInstance().handle(request.getUrl().toString())){
return true;
}
return super.onShouldOverrideUrlLoading(view, request);
}
@Override
protected boolean onShouldOverrideUrlLoading(WebView view, String url) {
if(QDSchemeManager.getInstance().handle(url)){
return true;
}
return super.onShouldOverrideUrlLoading(view, url);
}
};
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册