Skip to content

Commit

Permalink
添加各方法使用示例
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Sep 5, 2014
1 parent b276731 commit 33bb646
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ Safe Java-JS Bridge In Android WebView

achieve a safe and reliable solution for webview calling java method in js, meanwhile calling js function in java

![image](https://github.com/pedant/safe-java-for-webview-js/raw/master/app-sample-screenshot.png)

more detail: http://www.pedant.cn/2014/07/04/webview-js-java-interface-research/
Binary file added app-sample-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 44 additions & 8 deletions assets/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,39 @@ <h3>Java Interface 可用接口列表测试</h3>
<button onclick="HostApp.alert(JSON.stringify(HostApp.retBackPassJson({'b1': 'back-value1', 'b2': 'back-value2'})));">测试</button>
</li>

<br/>
<li>
传入回调的js函数给Java方法,设定3秒后回调<br/>
代码:<br/>
<div>
HostApp.delayJsCallBack(3, 'call back haha', function (msg) {
HostApp.alert(msg);
});
</div>
<button onclick="HostApp.delayJsCallBack(3, 'call back haha', function (msg) {HostApp.alert(msg);});">测试</button>
</li>

<br/>
<li>
注册浏览器滚动到底部时的JS回调函数<br/>
代码:<br/>
<div>
if (HostApp.existVerticalScrollbar()) {
HostApp.setOnScrollBottomListener (40, function (contentHeight) {
HostApp.toast("reach content(height " + contentHeight + ") bottom");
});
}
</div>
<button style="width: 150px;" onclick="HostApp.resetContentHeight();window.scroll(0, window.document.body.scrollHeight);">直接滚动到底部</button>
</li>

<br/>
<li>
重载方法测试-执行整型定义<br/>
代码:<br/>
<div>
HostApp.alert(
HostApp.overloadMethod(1)
HostApp.overloadMethod(1)
);
</div>
<button onclick="HostApp.alert(HostApp.overloadMethod(1));">测试</button>
Expand All @@ -137,22 +163,32 @@ <h3>Java Interface 可用接口列表测试</h3>
代码:<br/>
<div>
HostApp.alert(
HostApp.overloadMethod('1212')
HostApp.overloadMethod('12adadad')
);
</div>
<button onclick="HostApp.alert(HostApp.overloadMethod('12adadad'));">测试</button>
</li>

<br/>
<li>
从Java层返回Java对象<br/>
代码:<br/>
<div>
HostApp.alert(
HostApp.retJavaObject()[0].intField
);
</div>
<button onclick="HostApp.alert(HostApp.overloadMethod('1212'));">测试</button>
<button onclick="HostApp.alert(HostApp.retJavaObject()[0].intField);">测试</button>
</li>
</ul>


<script type="text/javascript">
$(function () {
//HostApp.alert("document ready now");
if (HostApp.existVerticalScrollbar()) {
HostApp.setOnScrollBottomListener (40, function (contentHeight) {
HostApp.toast("reach content(height " + contentHeight + ") bottom");
});
}
HostApp.setOnScrollBottomListener (40, function (contentHeight) {
HostApp.toast("reach content(height " + contentHeight + ") bottom");
});
});
</script>

Expand Down
4 changes: 4 additions & 0 deletions src/cn/pedant/SafeJava4WebviewJS/webview/BaseWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ protected void onScrollChanged(int newX, int newY, int oldX, int oldY) {
}
}

public void resetContentHeight () {
mCurrContentHeight = 0;
}

public void setOnVerticalScrolledListener(OnVerticalScrolledListener listener) {
mVScrolledListener = listener;
}
Expand Down
32 changes: 32 additions & 0 deletions src/cn/pedant/SafeJava4WebviewJS/webview/bridge/HostJsScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import cn.pedant.SafeJava4WebviewJS.R;
import cn.pedant.SafeJava4WebviewJS.util.EnvironUtil;
import cn.pedant.SafeJava4WebviewJS.util.Log;
import cn.pedant.SafeJava4WebviewJS.util.TaskExecutor;
import cn.pedant.SafeJava4WebviewJS.webview.BaseWebView;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//HostJsScope中需要被JS调用的函数,必须定义成public static,且必须包含WebView这个参数
public class HostJsScope {
Expand Down Expand Up @@ -122,6 +125,10 @@ public static void goBack (WebView view) {
}
}

public static void resetContentHeight (WebView view) {
((BaseWebView)view).resetContentHeight();
}

/**
* 注册浏览器滚动到底部时的回调对象
* @param view 浏览器
Expand Down Expand Up @@ -180,4 +187,29 @@ public static int overloadMethod(WebView view, int val) {
public static String overloadMethod(WebView view, String val) {
return val;
}

public static class RetJavaObj {
public int intField;
public String strField;
public boolean boolField;
}

public static List<RetJavaObj> retJavaObject(WebView view) {
RetJavaObj obj = new RetJavaObj();
obj.intField = 1;
obj.strField = "mine str";
obj.boolField = true;
List<RetJavaObj> rets = new ArrayList<RetJavaObj>();
rets.add(obj);
return rets;
}

public static void delayJsCallBack(WebView view, int ms, final String backMsg, final JsCallback jsCallback) {
TaskExecutor.scheduleTaskOnUiThread(ms*1000, new Runnable() {
@Override
public void run() {
jsCallback.apply(backMsg);
}
});
}
}

0 comments on commit 33bb646

Please sign in to comment.