Skip to content

Commit ecbcfdf

Browse files
committed
Android - 拦截跳转、JavaScriptInterface两种方式OK
1 parent 0828467 commit ecbcfdf

Some content is hidden

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

45 files changed

+1254
-66
lines changed

Android/app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,36 @@ android {
1616
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1717
}
1818
}
19+
20+
sourceSets {
21+
main {
22+
jniLibs.srcDirs = ['libs']
23+
res.srcDirs = [
24+
'src/main/res',
25+
'src/main/res/layout',
26+
'src/main/res/layout/common',
27+
'src/main/res/layout/home',
28+
'src/main/res/layout/intercept',
29+
'src/main/res/layout/jsbridge'
30+
]
31+
}
32+
}
1933
}
2034

2135
dependencies {
2236
implementation fileTree(dir: 'libs', include: ['*.jar'])
37+
38+
implementation 'com.android.support:support-v4:26.1.0'
39+
implementation 'com.android.support:palette-v7:26.1.0'
40+
implementation 'com.android.support:cardview-v7:26.1.0'
41+
implementation 'com.android.support:gridlayout-v7:26.1.0'
2342
implementation 'com.android.support:appcompat-v7:26.1.0'
43+
implementation 'com.android.support:support-v13:26.1.0'
44+
implementation 'com.android.support:design:26.1.0'
45+
implementation 'com.android.support:multidex:1.0.2'
2446
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
47+
48+
2549
testImplementation 'junit:junit:4.12'
2650
androidTestImplementation 'com.android.support.test:runner:1.0.1'
2751
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
@@ -35,4 +59,10 @@ dependencies {
3559

3660
// gson
3761
implementation 'com.google.code.gson:gson:2.8.2'
62+
63+
// base quick adapter
64+
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
65+
66+
// Logger
67+
implementation 'com.orhanobut:logger:2.1.1'
3868
}

Android/app/src/androidTest/java/com/ganwenpeng/jsbridge/ExampleInstrumentedTest.java renamed to Android/app/src/androidTest/java/com/ganwenpeng/jsinterface/ExampleInstrumentedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ganwenpeng.jsbridge;
1+
package com.ganwenpeng.jsinterface;
22

33
import android.content.Context;
44
import android.support.test.InstrumentationRegistry;
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.ganwenpeng.jsbridge">
3+
package="com.ganwenpeng.jsinterface">
44

55
<!--网络-->
6-
<uses-permission android:name="android.permission.INTERNET"/>
6+
<uses-permission android:name="android.permission.INTERNET" />
77

88

99
<application
10+
android:name=".JSApplication"
1011
android:allowBackup="true"
1112
android:icon="@mipmap/ic_launcher"
1213
android:label="@string/app_name"
@@ -15,15 +16,29 @@
1516
android:theme="@style/AppTheme">
1617

1718
<!--首页-->
18-
<activity android:name=".MainActivity">
19+
<activity android:name="com.ganwenpeng.jsinterface.page.home.HomeActivity">
1920
<intent-filter>
2021
<action android:name="android.intent.action.MAIN" />
2122

2223
<category android:name="android.intent.category.LAUNCHER" />
2324
</intent-filter>
2425
</activity>
2526

26-
<!--BridgeWebViewActivity-->
27-
<activity android:name=".BridgeWebViewActivity" android:screenOrientation="portrait"/>
27+
28+
<!--拦截跳转实现交互-->
29+
<activity
30+
android:name="com.ganwenpeng.jsinterface.page.intercept.InterceptActivity"
31+
android:screenOrientation="portrait" />
32+
33+
<!--暴露对象给js实现交互-->
34+
<activity
35+
android:name="com.ganwenpeng.jsinterface.page.jsinterface.JsInterfaceActivity"
36+
android:screenOrientation="portrait" />
37+
38+
<!--JSBridge实现交互-->
39+
<activity
40+
android:name="com.ganwenpeng.jsinterface.page.jsbridge.JsBridgeAvtivity"
41+
android:screenOrientation="portrait" />
42+
2843
</application>
2944
</manifest>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>拦截跳转实现通信</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport"
7+
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
8+
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.12.4/jquery.js"></script>
9+
10+
<style type="text/css">
11+
.btn {
12+
background-color: #aaa;
13+
height: 40px;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
color: #444444;
18+
text-decoration: none;
19+
margin-top: 10px;
20+
}
21+
22+
#response {
23+
background: #eeeeee;
24+
word-wrap: break-word;
25+
display: block;
26+
outline: 1px solid #ccc;
27+
padding: 5px;
28+
margin: 5px;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<p style="color: red">嵌入页区域</p>
34+
<p>以拦截跳转链接的方式实现</p>
35+
<button class="btn" id="login">发起APP登录</button>
36+
37+
<button class="btn" id="share">发起社交化分享</button>
38+
39+
<pre id="response"></pre>
40+
<script>
41+
42+
/**
43+
* 显示响应信息
44+
* @param response 响应信心
45+
*/
46+
function showResponse(response) {
47+
$('#response').text(response);
48+
return response;
49+
}
50+
51+
$(function () {
52+
$('#login').click(function () {
53+
window.location = 'app://login?account=13011112222&password=123456';
54+
});
55+
56+
$('#share').click(function () {
57+
window.location = 'app://share?title=分享的标题&desc=分享的描述'
58+
});
59+
});
60+
</script>
61+
</body>
62+
</html>

Android/app/src/main/assets/jsbridge-test.html renamed to Android/app/src/main/assets/jsbridge.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
.btn {
1212
background-color: #aaa;
1313
height: 40px;
14-
margin: 10px;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
color: #444444;
18+
text-decoration: none;
19+
margin-top: 10px;
1520
}
1621

1722
#response {
@@ -25,6 +30,8 @@
2530
</style>
2631
</head>
2732
<body>
33+
<p style="color: red">嵌入页区域</p>
34+
<p>以JSBridge的方式实现</p>
2835
<button class="btn" id="getOS">获取当前操作系统</button>
2936
<button class="btn" id="login">发起登录</button>
3037

@@ -83,6 +90,16 @@
8390
responseCallback(data);
8491
});
8592
}
93+
94+
bridge.registerHandler('jsbridge_showMessage', function (data, responseCallback) {
95+
showResponse(data);
96+
});
97+
98+
bridge.registerHandler('jsbridge_getJsMessage', function (data, responseCallback) {
99+
showResponse(data);
100+
responseCallback('native 传过来的是:' + data);
101+
});
102+
86103
readyCallback();
87104
});
88105
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>拦截跳转实现通信</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport"
7+
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
8+
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.12.4/jquery.js"></script>
9+
10+
<style type="text/css">
11+
.btn {
12+
background-color: #aaa;
13+
height: 40px;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
color: #444444;
18+
text-decoration: none;
19+
margin-top: 10px;
20+
}
21+
22+
#response {
23+
background: #eeeeee;
24+
word-wrap: break-word;
25+
display: block;
26+
outline: 1px solid #ccc;
27+
padding: 5px;
28+
margin: 5px;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<p style="color: red">嵌入页区域</p>
34+
<p>以JavaScriptInterface的方式实现</p>
35+
<button class="btn" id="login">发起APP登录</button>
36+
37+
<button class="btn" id="logout">发起APP登出</button>
38+
39+
<button class="btn" id="getLoginUser">获取登录信息</button>
40+
41+
<pre id="response"></pre>
42+
<script>
43+
/**
44+
* 显示响应信息
45+
* @param response 响应信心
46+
*/
47+
function showResponse(response) {
48+
$('#response').text(response);
49+
}
50+
51+
$(function () {
52+
$('#login').click(function () {
53+
app.login("13011112222", "123456");
54+
});
55+
56+
$('#logout').click(function () {
57+
app.logout();
58+
});
59+
60+
$('#getLoginUser').click(function () {
61+
var info = app.getLoginUser();
62+
showResponse(info);
63+
});
64+
});
65+
</script>
66+
</body>
67+
</html>

Android/app/src/main/java/com/ganwenpeng/jsbridge/MainActivity.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.ganwenpeng.jsinterface;
2+
3+
import android.app.Application;
4+
5+
import com.orhanobut.logger.AndroidLogAdapter;
6+
import com.orhanobut.logger.FormatStrategy;
7+
import com.orhanobut.logger.Logger;
8+
import com.orhanobut.logger.PrettyFormatStrategy;
9+
10+
11+
public class JSApplication extends Application {
12+
@Override
13+
public void onCreate() {
14+
super.onCreate();
15+
16+
initLogger();
17+
}
18+
19+
/**
20+
* 初始化日志框架
21+
*/
22+
private void initLogger() {
23+
// 格式化log,添加全局tag【tugou-log】,查看日志时筛选 tugou-log即可
24+
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
25+
.tag("jsinterface-log")
26+
.build();
27+
28+
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
29+
@Override
30+
public boolean isLoggable(int priority, String tag) {
31+
return BuildConfig.DEBUG;
32+
}
33+
});
34+
Logger.d("initing");
35+
}
36+
}

0 commit comments

Comments
 (0)