Skip to content

Commit

Permalink
enhance the demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon committed Apr 10, 2018
1 parent d903960 commit 7f14494
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 28 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
compileSdkVersion 27
defaultConfig {
applicationId "tech.easily.easybridge"
minSdkVersion 15
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -59,7 +59,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/assets/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>

<head>
<meta charset="utf-8" />
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -13,7 +13,7 @@
toast('inject finished');
window.easyBridge.registerHandler('resultBack', function (parameters, callback) {
if (typeof callback == 'function') {
callback(parameters);
callback('callback from JavaScript,this is the data received from Java:'+parameters);
}
});
}, false);
Expand All @@ -24,10 +24,20 @@
});
}

function getUserObject(){
easyBridge.callHandler('getUserInfo',function(result){
console.log('data receive:'+result);
if(typeof result == 'object'){
console.log('json string of the result object is:'+JSON.stringify(result));
}
});
}

function jumpToPage(){
var url=document.getElementById('tv_url').value;
window.open(url);
}

</script>
</head>

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

<a href="javascript:toast('click toast')">
<h3>测试Toast</h3>
</a>
<a href="javascript:toast('click toast')"><h3>测试Toast</h3></a>

<a href="javascript:getUserObject()"><h3>获取用户对象</h3></a>

</body>

Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/tech/easily/easybridge/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package tech.easily.easybridge

import android.net.Uri
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.webkit.*
import android.widget.Toast
import tech.easily.easybridge.lib.EBHandlerManager
import tech.easily.easybridge.lib.EasyBridgeWebChromeClient
import tech.easily.easybridge.lib.ResultCallBack
import kotlinx.android.synthetic.main.activity_main.*
import tech.easily.easybridge.lib.ResultCallBack

class MainActivity : AppCompatActivity() {

Expand All @@ -25,6 +25,13 @@ class MainActivity : AppCompatActivity() {
}

private fun init() {
tvCallJS.setOnClickListener {
webView.callHandler("resultBack", "this is the value pass from Java", object : ResultCallBack() {
override fun onResult(result: Any?) {
Toast.makeText(this@MainActivity, result?.toString(), Toast.LENGTH_SHORT).show()
}
})
}
EBHandlerManager.register(webView)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
Expand All @@ -44,15 +51,6 @@ class MainActivity : AppCompatActivity() {
else -> true
}
}))
// call JavaScript From Java
webView.postDelayed({
webView.callHandler("resultBack", "this is the value pass from Java", object : ResultCallBack() {
override fun onResult(result: Any?) {
Toast.makeText(this@MainActivity, result?.toString(), Toast.LENGTH_SHORT).show()
}

})
}, 5000)
}

override fun onBackPressed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.easily.easybridge.handler

import tech.easily.easybridge.annotation.EasyBridgeHandler
import tech.easily.easybridge.lib.EasyBridgeWebView
import tech.easily.easybridge.lib.ResultCallBack
import tech.easily.easybridge.lib.handler.BaseBridgeHandler
import tech.easily.easybridge.model.User

/**
* Created by hzyangjiehao on 2018/4/10.
*/
@EasyBridgeHandler(name = "getUserInfo")
class GetUserInfoHandler(handlerName: String, webView: EasyBridgeWebView) : BaseBridgeHandler(handlerName, webView) {
override fun onCall(parameters: String?, callBack: ResultCallBack?) {
val user = User("userName", 13, "Hangzhou")
callBack?.onResult(user)
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/tech/easily/easybridge/model/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tech.easily.easybridge.model

/**
* Created by hzyangjiehao on 2018/4/10.
*/
data class User(var name: String, var age: Int, var address: String)
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/bg_circle_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<solid android:color="@color/colorPrimary" />

</shape>
25 changes: 16 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<tech.easily.easybridge.lib.EasyBridgeWebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_width="match_parent"
android:layout_height="match_parent" />

<TextView
android:id="@+id/tvCallJS"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="right|bottom"
android:layout_margin="16dp"
android:background="@drawable/bg_circle_button"
android:elevation="1dp"
android:gravity="center"
android:text="callJS"
android:textColor="#fff" />

</android.support.constraint.ConstraintLayout>
</FrameLayout>
1 change: 0 additions & 1 deletion easybridge/src/main/assets/easybridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
}

function _resolveResult(result) {
console.log(result);
if (typeof result == 'string') {
result = JSON.parse(result.replace(/\n/g, '\\\\n'));
}
Expand Down

0 comments on commit 7f14494

Please sign in to comment.