Skip to content

Commit ed62412

Browse files
html和navite交互
通过java反射和WebChromeClient的onJsPrompt方法
0 parents  commit ed62412

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1694
-0
lines changed

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

Diff for: .idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/misc.xml

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Diff for: app/build.gradle

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
defaultConfig {
6+
applicationId "com.yue.jsbridge"
7+
minSdkVersion 15
8+
targetSdkVersion 26
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(include: ['*.jar'], dir: 'libs')
23+
implementation 'com.android.support:appcompat-v7:26.1.0'
24+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
27+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28+
implementation files('libs/tbs_sdk_thirdapp_v3.5.0.1004_43500_sharewithdownload_withoutGame_obfs_20170801_113025.jar')
29+
}

Diff for: app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.yue.jsbridge;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.yue.jsbridge", appContext.getPackageName());
25+
}
26+
}

Diff for: app/src/main/AndroidManifest.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.yue.jsbridge">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
<activity android:name=".activity.SimpleTestActivity" />
20+
<activity android:name=".activity.SimpleTest01Activity"></activity>
21+
</application>
22+
23+
</manifest>

Diff for: app/src/main/assets/index01.html

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE HTML>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
7+
</head>
8+
9+
<body>
10+
<div class="blog-header">
11+
<h3>JSBridge</h3>
12+
</div>
13+
<ul class="entry">
14+
15+
<br/>
16+
<li>
17+
toast展示<br/>
18+
<button onclick="JsBridge.call('JSBridge','toast',{'message':'我是气泡','isShowLong':0},function(res){});">toast</button>
19+
</li>
20+
21+
<br/>
22+
<li>
23+
异步任务<br/>
24+
<button onclick="JsBridge.call('JSBridge','plus',{'data':1},function(res){console.log(JSON.stringify(res))});">plus</button>
25+
</li>
26+
<br/>
27+
<br/>
28+
</ul>
29+
<script src="file:///android_asset/jsBridg.js" type="text/javascript"></script>
30+
</body>
31+
</html>

Diff for: app/src/main/assets/jsBridg.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
(function (win, lib) {
2+
var doc = win.document;
3+
var hasOwnProperty = Object.prototype.hasOwnProperty;
4+
var JsBridge = win.JsBridge || (win.JsBridge = {});
5+
var inc = 1;
6+
var LOCAL_PROTOCOL = 'hybrid';
7+
var CB_PROTOCOL = 'cb_hybrid';
8+
var CALLBACK_PREFIX = 'callback_';
9+
10+
alert("哈哈哈");
11+
//核心功能,对外暴露
12+
var Core = {
13+
14+
call: function (obj, method, params, callback, timeout) {
15+
var sid;
16+
17+
if (typeof callback !== 'function') {
18+
callback = null;
19+
}
20+
21+
sid = Private.getSid();
22+
23+
Private.registerCall(sid, callback);
24+
Private.callMethod(obj, method, params, sid);
25+
26+
},
27+
28+
//native代码处理 成功/失败 后,调用该方法来通知js
29+
onComplete: function (sid, data) {
30+
Private.onComplete(sid, data);
31+
}
32+
};
33+
34+
//私有功能集合
35+
var Private = {
36+
params: {},
37+
chunks: {},
38+
calls: {},
39+
40+
getSid: function () {
41+
return Math.floor(Math.random() * (1 << 50)) + '' + inc++;
42+
},
43+
44+
buildParam: function (obj) {
45+
if (obj && typeof obj === 'object') {
46+
return JSON.stringify(obj);
47+
} else {
48+
return obj || '';
49+
}
50+
},
51+
52+
parseData: function (str) {
53+
var rst;
54+
if (str && typeof str === 'string') {
55+
try {
56+
rst = JSON.parse(str);
57+
} catch (e) {
58+
rst = {
59+
status: {
60+
code: 1,
61+
msg: 'PARAM_PARSE_ERROR'
62+
}
63+
};
64+
}
65+
} else {
66+
rst = str || {};
67+
}
68+
69+
return rst;
70+
},
71+
72+
//根据sid注册calls的回调函数
73+
registerCall: function (sid, callback) {
74+
if (callback) {
75+
this.calls[CALLBACK_PREFIX + sid] = callback;
76+
}
77+
},
78+
79+
//根据sid删除calls对应的回调函数,并返回call对象
80+
unregisterCall: function (sid) {
81+
var callbackId = CALLBACK_PREFIX + sid;
82+
var call = {};
83+
84+
if (this.calls[callbackId]) {
85+
call.callback = this.calls[callbackId];
86+
delete this.calls[callbackId];
87+
}
88+
89+
return call;
90+
},
91+
92+
//生成URI,调用native功能
93+
callMethod: function (obj, method, params, sid) {
94+
// hybrid://objectName:sid/methodName?params
95+
params = Private.buildParam(params);
96+
97+
var uri = LOCAL_PROTOCOL + '://' + obj + ':' + sid + '/' + method + '?' + params;
98+
99+
var value = CB_PROTOCOL + ':';
100+
window.prompt(uri, value);
101+
},
102+
103+
onComplete: function (sid, data) {
104+
var callObj = this.unregisterCall(sid);
105+
var callback = callObj.callback;
106+
107+
data = this.parseData(data);
108+
109+
callback && callback(data);
110+
}
111+
};
112+
113+
for (var key in Core) {
114+
if (!hasOwnProperty.call(JsBridge, key)) {
115+
JsBridge[key] = Core[key];
116+
}
117+
}
118+
})(window);

Diff for: app/src/main/assets/wwconjstest.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>webviewchormclient onjs方法测试</title><br />
6+
7+
</head>
8+
9+
<body>
10+
<input type="text" id="input" value="我是默认值"/><br />
11+
<button onclick="call()">弹出alert</button><br />
12+
<button onclick="onJsAlert()">onJsAlert 警告窗口</button><br />
13+
<button onclick="onJsConfirm()">onJsConfirm 确认框</button><br />
14+
<button onclick="onJsPrompt()">onJsPrompt 提示框</button><br />
15+
16+
<script src="file:///android_asset/wwconjstest.js" type="text/javascript">
17+
</script>
18+
</body>
19+
</html>

Diff for: app/src/main/assets/wwconjstest.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function call(){
2+
var value = document.getElementById("input").value;
3+
alert(value);
4+
}
5+
6+
function onJsAlert(){
7+
alert("onJsAlert");
8+
}
9+
10+
function onJsConfirm(){
11+
var b = confirm("are you sure to login?");
12+
//alert("your choice is "+b);
13+
}
14+
15+
function onJsPrompt(){
16+
<!--//prompt的执行 会调用webveiwchormclient 的onJsPrompt方法执行,b 是prompt执行后的返回结果,由onJsPrompt的JsPromptResult.confirm方法执行并返回结果-->
17+
var b = prompt("please input your password","我擦");
18+
//b:是prompt执行完的返回结果
19+
//alert("your input is "+b);
20+
document.getElementById("input").value = b;
21+
}
22+
function callJS(){
23+
alert("android 调用js");
24+
}

0 commit comments

Comments
 (0)