Skip to content

Commit 7f14494

Browse files
author
lemon
committed
enhance the demo code
1 parent d903960 commit 7f14494

File tree

9 files changed

+78
-28
lines changed

9 files changed

+78
-28
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010
compileSdkVersion 27
1111
defaultConfig {
1212
applicationId "tech.easily.easybridge"
13-
minSdkVersion 15
13+
minSdkVersion 17
1414
targetSdkVersion 27
1515
versionCode 1
1616
versionName "1.0"
@@ -59,7 +59,7 @@ android {
5959
dependencies {
6060
implementation fileTree(include: ['*.jar'], dir: 'libs')
6161
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
62-
implementation 'com.android.support:appcompat-v7:27.1.0'
62+
implementation 'com.android.support:appcompat-v7:27.1.1'
6363
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
6464
testImplementation 'junit:junit:4.12'
6565
androidTestImplementation 'com.android.support.test:runner:1.0.1'

app/src/main/assets/demo.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33

44
<head>
5-
<meta charset="utf-8" />
5+
<meta charset="utf-8"/>
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<title>Page Title</title>
88
<meta name="viewport" content="width=device-width, initial-scale=1">
@@ -13,7 +13,7 @@
1313
toast('inject finished');
1414
window.easyBridge.registerHandler('resultBack', function (parameters, callback) {
1515
if (typeof callback == 'function') {
16-
callback(parameters);
16+
callback('callback from JavaScript,this is the data received from Java:'+parameters);
1717
}
1818
});
1919
}, false);
@@ -24,10 +24,20 @@
2424
});
2525
}
2626

27+
function getUserObject(){
28+
easyBridge.callHandler('getUserInfo',function(result){
29+
console.log('data receive:'+result);
30+
if(typeof result == 'object'){
31+
console.log('json string of the result object is:'+JSON.stringify(result));
32+
}
33+
});
34+
}
35+
2736
function jumpToPage(){
2837
var url=document.getElementById('tv_url').value;
2938
window.open(url);
3039
}
40+
3141
</script>
3242
</head>
3343

@@ -36,9 +46,9 @@
3646
<input type="text" value="http://google.com" id="tv_url">
3747
<input type="button" value="jump" onclick="jumpToPage()">
3848

39-
<a href="javascript:toast('click toast')">
40-
<h3>测试Toast</h3>
41-
</a>
49+
<a href="javascript:toast('click toast')"><h3>测试Toast</h3></a>
50+
51+
<a href="javascript:getUserObject()"><h3>获取用户对象</h3></a>
4252

4353
</body>
4454

app/src/main/java/tech/easily/easybridge/MainActivity.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package tech.easily.easybridge
22

33
import android.net.Uri
44
import android.os.Build
5-
import android.support.v7.app.AppCompatActivity
65
import android.os.Bundle
6+
import android.support.v7.app.AppCompatActivity
77
import android.webkit.*
88
import android.widget.Toast
99
import tech.easily.easybridge.lib.EBHandlerManager
1010
import tech.easily.easybridge.lib.EasyBridgeWebChromeClient
11-
import tech.easily.easybridge.lib.ResultCallBack
1211
import kotlinx.android.synthetic.main.activity_main.*
12+
import tech.easily.easybridge.lib.ResultCallBack
1313

1414
class MainActivity : AppCompatActivity() {
1515

@@ -25,6 +25,13 @@ class MainActivity : AppCompatActivity() {
2525
}
2626

2727
private fun init() {
28+
tvCallJS.setOnClickListener {
29+
webView.callHandler("resultBack", "this is the value pass from Java", object : ResultCallBack() {
30+
override fun onResult(result: Any?) {
31+
Toast.makeText(this@MainActivity, result?.toString(), Toast.LENGTH_SHORT).show()
32+
}
33+
})
34+
}
2835
EBHandlerManager.register(webView)
2936
webView.webViewClient = object : WebViewClient() {
3037
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
@@ -44,15 +51,6 @@ class MainActivity : AppCompatActivity() {
4451
else -> true
4552
}
4653
}))
47-
// call JavaScript From Java
48-
webView.postDelayed({
49-
webView.callHandler("resultBack", "this is the value pass from Java", object : ResultCallBack() {
50-
override fun onResult(result: Any?) {
51-
Toast.makeText(this@MainActivity, result?.toString(), Toast.LENGTH_SHORT).show()
52-
}
53-
54-
})
55-
}, 5000)
5654
}
5755

5856
override fun onBackPressed() {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tech.easily.easybridge.handler
2+
3+
import tech.easily.easybridge.annotation.EasyBridgeHandler
4+
import tech.easily.easybridge.lib.EasyBridgeWebView
5+
import tech.easily.easybridge.lib.ResultCallBack
6+
import tech.easily.easybridge.lib.handler.BaseBridgeHandler
7+
import tech.easily.easybridge.model.User
8+
9+
/**
10+
* Created by hzyangjiehao on 2018/4/10.
11+
*/
12+
@EasyBridgeHandler(name = "getUserInfo")
13+
class GetUserInfoHandler(handlerName: String, webView: EasyBridgeWebView) : BaseBridgeHandler(handlerName, webView) {
14+
override fun onCall(parameters: String?, callBack: ResultCallBack?) {
15+
val user = User("userName", 13, "Hangzhou")
16+
callBack?.onResult(user)
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package tech.easily.easybridge.model
2+
3+
/**
4+
* Created by hzyangjiehao on 2018/4/10.
5+
*/
6+
data class User(var name: String, var age: Int, var address: String)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="oval">
4+
5+
<padding
6+
android:bottom="8dp"
7+
android:left="8dp"
8+
android:right="8dp"
9+
android:top="8dp" />
10+
<solid android:color="@color/colorPrimary" />
11+
12+
</shape>
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
76
tools:context=".MainActivity">
87

98
<tech.easily.easybridge.lib.EasyBridgeWebView
10-
android:layout_width="match_parent"
11-
android:layout_height="match_parent"
129
android:id="@+id/webView"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent" />
12+
13+
<TextView
14+
android:id="@+id/tvCallJS"
15+
android:layout_width="60dp"
16+
android:layout_height="60dp"
17+
android:layout_gravity="right|bottom"
18+
android:layout_margin="16dp"
19+
android:background="@drawable/bg_circle_button"
20+
android:elevation="1dp"
21+
android:gravity="center"
22+
android:text="callJS"
23+
android:textColor="#fff" />
1724

18-
</android.support.constraint.ConstraintLayout>
25+
</FrameLayout>

easybridge/src/main/assets/easybridge.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
}
9595

9696
function _resolveResult(result) {
97-
console.log(result);
9897
if (typeof result == 'string') {
9998
result = JSON.parse(result.replace(/\n/g, '\\\\n'));
10099
}

0 commit comments

Comments
 (0)