forked from ChinaLike/JsBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6b20999
Showing
56 changed files
with
1,706 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# JsBridge | ||
android 与 WebView 互调 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/build | ||
.idea | ||
.idea/ | ||
target/ | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'kotlin-android' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.1" | ||
|
||
defaultConfig { | ||
applicationId "com.like.jsbridge" | ||
minSdkVersion 17 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
sourceSets { | ||
main { | ||
assets { | ||
srcDirs 'src/main/assets' | ||
} | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.3.2' | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.material:material:1.2.1' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||
implementation project(path: ':library') | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||
|
||
implementation 'com.alibaba:fastjson:1.1.72.android' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/like/jsbridge/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.like.jsbridge | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.like.jsbridge", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.like.jsbridge"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.JsBridge"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html; charset=utf-8" http-equiv="content-type"> | ||
<title> | ||
js调用java | ||
</title> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
<input type="button" id="enter" value="发消息给Native" onclick="testClick();" /> | ||
</p> | ||
</body> | ||
<script> | ||
|
||
/** | ||
* 数据转JSON | ||
* @param {Object} params 数据 | ||
*/ | ||
function JsBridge_Any_To_Json(params){ | ||
var paramsData = params; | ||
if (typeof params == 'string') { | ||
try{ | ||
var obj = JSON.parse(params); | ||
if (typeof obj == 'object' && obj) { | ||
paramsData = obj; | ||
} else{ | ||
throw new Error("params参数应该是JSON格式"); | ||
} | ||
}catch(e){ | ||
throw new Error("params参数应该是JSON格式"); | ||
} | ||
} | ||
if (paramsData == null || paramsData == '') { | ||
throw new Error("params参数应该是JSON格式"); | ||
}else{ | ||
return paramsData; | ||
} | ||
} | ||
|
||
function JsBridge_Object_Save(params,requireParams){ | ||
//参数 | ||
var paramsData ={}; | ||
|
||
if(params && params.length){ | ||
//有参数 | ||
if (params.length == 0) { | ||
if (requireParams) { | ||
throw new Error("调用toast方法需要传递参数"); | ||
} | ||
} else if(params.length == 1 && typeof params[0] != 'function'){ | ||
if (requireParams) { | ||
paramsData = JsBridge_Any_To_Json(params[0]) | ||
} else{ | ||
throw new Error("调用toast方法不需要传递参数"); | ||
} | ||
}else if(params.length == 1 && typeof params[0] === 'function'){ | ||
JsBridge.id++; | ||
JsBridge.successCallback[JsBridge.id] = params[0]; | ||
}else if(params.length == 2 && typeof params[0] != 'function' && typeof params[1] === 'function'){ | ||
if (requireParams) { | ||
paramsData = JsBridge_Any_To_Json(params[0]) | ||
} else{ | ||
throw new Error("调用toast方法不需要传递参数"); | ||
} | ||
JsBridge.id++; | ||
JsBridge.successCallback[JsBridge.id] = params[1]; | ||
}else if(params.length == 2 && typeof params[0] === 'function' && typeof params[1] === 'function'){ | ||
JsBridge.id++; | ||
JsBridge.successCallback[JsBridge.id] = params[0]; | ||
JsBridge.failCallback[JsBridge.id] = params[1]; | ||
}else if(params.length == 3 && typeof params[0] != 'function' && typeof params[1] === 'function' && typeof params[2] === 'function'){ | ||
if (requireParams) { | ||
paramsData = JsBridge_Any_To_Json(params[0]) | ||
} else{ | ||
throw new Error("调用toast方法不需要传递参数"); | ||
} | ||
JsBridge.id++; | ||
JsBridge.successCallback[JsBridge.id] = params[1]; | ||
JsBridge.failCallback[JsBridge.id] = params[2]; | ||
}else{ | ||
throw new Error("参数错误"); | ||
} | ||
}else{ | ||
//没有传递任何参数 | ||
if (requireParams) { | ||
throw new Error("调用toast方法需要传递参数"); | ||
} | ||
} | ||
paramsData.cbId = JsBridge.id; | ||
|
||
return paramsData; | ||
} | ||
|
||
var JsBridge = { | ||
id: 1, | ||
successCallback: {}, | ||
failCallback: {}, | ||
toast: function() { | ||
var requireParams = true; | ||
var paramsData = JsBridge_Object_Save(arguments,requireParams); | ||
console.log(JSON.stringify(paramsData)); | ||
console.log("successCallback:"+this.successCallback); | ||
if (requireParams) { | ||
window.Toast.toast(JSON.stringify(paramsData)); | ||
} else{ | ||
window.Toast.toast(); | ||
} | ||
}, | ||
on: function(cbId, result, deleteId) { | ||
console.log("cbId:"+cbId); | ||
console.log("返回结果:"+JSON.stringify(result)); | ||
var success = this.successCallback[cbId]; | ||
var fail = this.failCallback[cbId]; | ||
try { | ||
var resultData = result; | ||
if(typeof result == 'string'){ | ||
resultData = JSON.parse(result); | ||
} | ||
if (resultData.code == 0 && success) { | ||
success(resultData); | ||
} | ||
if (resultData.code != 0 && fail) { | ||
fail(resultData); | ||
} | ||
if (deleteId) { | ||
delete this.successCallback[cbId]; | ||
delete this.failCallback[cbId]; | ||
} | ||
} catch (e) { | ||
throw new Error("回调数据异常"); | ||
} | ||
} | ||
} | ||
|
||
function testClick() { | ||
JsBridge.toast({data:"123"}) | ||
} | ||
</script> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html; charset=utf-8" http-equiv="content-type"> | ||
<title> | ||
js调用java | ||
</title> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
<input type="button" id="enter" value="发消息给Native" onclick="testClick();" /> | ||
</p> | ||
</body> | ||
<script> | ||
|
||
|
||
function testClick() { | ||
JsBridge.toast1(1111, function(data) { | ||
alert(JSON.stringify(data)) | ||
},function(data){ | ||
alert(JSON.stringify(data)) | ||
}) | ||
} | ||
</script> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<html> | ||
<head> | ||
<meta content="text/html; charset=utf-8" http-equiv="content-type"> | ||
<title> | ||
js调用java | ||
</title> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
<input type="button" id="enter" value="发消息给Native" onclick="testClick();" /> | ||
</p> | ||
</body> | ||
<script> | ||
|
||
|
||
|
||
function testClick() { | ||
JsBridge.toast(function(data){ | ||
alert(JSON.stringify(data)) | ||
}) | ||
} | ||
</script> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.like.jsbridge | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import com.core.web.BaseWebView | ||
|
||
/** | ||
* | ||
* @author like | ||
* @date 5/24/21 4:43 PM | ||
*/ | ||
class DemoWebView : BaseWebView { | ||
|
||
constructor(context: Context) : super(context) { | ||
|
||
} | ||
|
||
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | ||
|
||
} | ||
|
||
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super( | ||
context, | ||
attrs, | ||
defStyle | ||
) { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.