├── README.md └── JsBridge_Android ├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── lyk │ │ └── jsbridge │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── demo.html │ ├── java │ │ └── com │ │ │ └── lyk │ │ │ └── jsbridge │ │ │ ├── MainActivity.java │ │ │ └── modle │ │ │ └── User.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── content_main.xml │ │ └── main_detail.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── lyk │ └── loader │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /README.md: -------------------------------------------------------------------------------- 1 | # JsBriage_Android 2 | java 和 js互通框架 3 | WebViewJavascriptBridge是移动UIView和Html交互通信的桥梁,用作者的话来说就是实现java(ios为oc)和js的互相调用的桥梁。 4 | 替代了WebView的自带的JavascriptInterface的接口,使得我们的开发更加灵活和安全。 5 | 6 | 7 | ![main.png](http://upload-images.jianshu.io/upload_images/2022038-1153245a6a0877c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 8 | 9 | 10 | API介绍:http://www.jianshu.com/p/52071a3d07b4 11 | -------------------------------------------------------------------------------- /JsBridge_Android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/.name: -------------------------------------------------------------------------------- 1 | JsBridge_Android -------------------------------------------------------------------------------- /JsBridge_Android/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /JsBridge_Android/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /JsBridge_Android/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /JsBridge_Android/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.lyk.loader" 9 | minSdkVersion 21 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | repositories { 22 | maven { url "https://jitpack.io" } 23 | } 24 | 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.android.support:appcompat-v7:23.2.1' 30 | compile 'com.android.support:design:23.2.1' 31 | compile 'com.github.lzyzsd:jsbridge:1.0.4' 32 | compile 'com.google.code.gson:gson:2.3.1' 33 | } 34 | -------------------------------------------------------------------------------- /JsBridge_Android/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Users\liuyongkui726\AppData\Local\Android\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/androidTest/java/com/lyk/jsbridge/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.lyk.jsbridge; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 16 | 17 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/assets/demo.html: -------------------------------------------------------------------------------- 1 | html> 2 | 3 | 4 | 5 | js调用java 6 | 7 | 8 | 9 | 10 |

11 |

12 | 13 |

14 |

15 |

16 | 17 |

18 |

19 | 20 |

21 | 22 |

23 | 24 |

25 | 26 |

27 | 29 |

30 | 31 |

32 | 34 |

35 | 36 |

37 | 38 |

39 | 40 |

41 | 42 |

43 | 44 | 136 | 137 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/java/com/lyk/jsbridge/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lyk.jsbridge; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import android.support.design.widget.FloatingActionButton; 8 | import android.support.design.widget.Snackbar; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.Toolbar; 11 | import android.util.Log; 12 | import android.view.View; 13 | import android.webkit.ValueCallback; 14 | import android.webkit.WebChromeClient; 15 | import android.widget.Toast; 16 | 17 | import com.github.lzyzsd.jsbridge.BridgeHandler; 18 | import com.github.lzyzsd.jsbridge.BridgeWebView; 19 | import com.github.lzyzsd.jsbridge.BridgeWebViewClient; 20 | import com.github.lzyzsd.jsbridge.CallBackFunction; 21 | import com.github.lzyzsd.jsbridge.DefaultHandler; 22 | import com.google.gson.Gson; 23 | import com.lyk.jsbridge.modle.User; 24 | 25 | public class MainActivity extends AppCompatActivity { 26 | 27 | private BridgeWebView mWebView; 28 | 29 | ValueCallback mUploadMessage; 30 | 31 | int RESULT_CODE = 0; 32 | 33 | private static final String TAG = "MainActivity"; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_main); 39 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 40 | setSupportActionBar(toolbar); 41 | 42 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 43 | fab.setOnClickListener(new View.OnClickListener() { 44 | @Override 45 | public void onClick(View view) { 46 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 47 | .setAction("Action", null).show(); 48 | } 49 | }); 50 | 51 | mWebView = (BridgeWebView) findViewById(R.id.webView); 52 | initWebView(); 53 | } 54 | 55 | private void initWebView() { 56 | // 设置具体WebViewClient 57 | mWebView.setWebViewClient(new MyWebViewClient(mWebView)); 58 | // set HadlerCallBack 59 | mWebView.setDefaultHandler(new myHadlerCallBack()); 60 | // setWebChromeClient 61 | mWebView.setWebChromeClient(new WebChromeClient() { 62 | 63 | @SuppressWarnings("unused") 64 | public void openFileChooser(ValueCallback uploadMsg, String AcceptType, String capture) { 65 | this.openFileChooser(uploadMsg); 66 | } 67 | 68 | @SuppressWarnings("unused") 69 | public void openFileChooser(ValueCallback uploadMsg, String AcceptType) { 70 | this.openFileChooser(uploadMsg); 71 | } 72 | 73 | public void openFileChooser(ValueCallback uploadMsg) { 74 | mUploadMessage = uploadMsg; 75 | pickFile(); 76 | } 77 | }); 78 | 79 | mWebView.loadUrl("file:///android_asset/demo.html"); 80 | 81 | //必须和js函数名字一致,注册好具体执行回调函数,类似java实现类。 82 | mWebView.registerHandler("submitFromWeb", new BridgeHandler() { 83 | 84 | @Override 85 | public void handler(String data, CallBackFunction function) { 86 | 87 | String str = "这是html返回给java的数据:" + data; 88 | // 例如你可以对原始数据进行处理 89 | str = str + ",Java经过处理后截取了一部分:" + str.substring(0, 5); 90 | Log.i(TAG, "handler = submitFromWeb, data from web = " + data); 91 | Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); 92 | //回调返回给Js 93 | function.onCallBack(str + ",Java经过处理后截取了一部分:" + str.substring(0, 5)); 94 | } 95 | 96 | }); 97 | 98 | mWebView.registerHandler("functionOpen", new BridgeHandler() { 99 | 100 | @Override 101 | public void handler(String data, CallBackFunction function) { 102 | Toast.makeText(MainActivity.this, "网页在打开你的下载文件预览", Toast.LENGTH_SHORT).show(); 103 | pickFile(); 104 | 105 | } 106 | 107 | }); 108 | //模拟用户信息 获取本地位置,用户名返回给html 109 | User user = new User(); 110 | user.setLocation("上海"); 111 | user.setName("Bruce"); 112 | // 回调 "functionInJs" 113 | mWebView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() { 114 | @Override 115 | public void onCallBack(String data) { 116 | 117 | Toast.makeText(MainActivity.this, "网页在获取你的位置,"+ data, Toast.LENGTH_SHORT).show(); 118 | 119 | } 120 | }); 121 | mWebView.send("hello"); 122 | } 123 | 124 | /** 125 | * 自定义的WebViewClient 126 | */ 127 | class MyWebViewClient extends BridgeWebViewClient { 128 | 129 | public MyWebViewClient(BridgeWebView webView) { 130 | super(webView); 131 | } 132 | } 133 | 134 | 135 | /** 136 | * 自定义回调 137 | */ 138 | class myHadlerCallBack extends DefaultHandler { 139 | 140 | @Override 141 | public void handler(String data, CallBackFunction function) { 142 | if(function != null){ 143 | 144 | Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show(); 145 | } 146 | } 147 | } 148 | 149 | public void pickFile() { 150 | Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT); 151 | chooserIntent.setType("image/*"); 152 | startActivityForResult(chooserIntent, RESULT_CODE); 153 | } 154 | 155 | @Override 156 | public void onActivityResult(int requestCode, int resultCode, Intent intent) { 157 | if (requestCode == RESULT_CODE) { 158 | if (null == mUploadMessage) { 159 | return; 160 | } 161 | Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData(); 162 | mUploadMessage.onReceiveValue(result); 163 | mUploadMessage = null; 164 | } 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/java/com/lyk/jsbridge/modle/User.java: -------------------------------------------------------------------------------- 1 | package com.lyk.jsbridge.modle; 2 | 3 | /** 4 | * Created by LIUYONGKUI726 on 2016-06-01. 5 | */ 6 | public class User { 7 | private String location ; 8 | private String name = "Bruce"; 9 | 10 | public User() { 11 | } 12 | 13 | public String getLocation() { 14 | return location; 15 | } 16 | 17 | public void setLocation(String location) { 18 | this.location = location; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 25 | 26 | 33 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/layout/main_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 16 | 25 | 26 | 27 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tamicer/JsBridge-Android/8983d43e357386b146195dd11803ed4291d842c2/JsBridge_Android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tamicer/JsBridge-Android/8983d43e357386b146195dd11803ed4291d842c2/JsBridge_Android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tamicer/JsBridge-Android/8983d43e357386b146195dd11803ed4291d842c2/JsBridge_Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tamicer/JsBridge-Android/8983d43e357386b146195dd11803ed4291d842c2/JsBridge_Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tamicer/JsBridge-Android/8983d43e357386b146195dd11803ed4291d842c2/JsBridge_Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | > 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | jsBridge 3 | 4 | 5 | 6 | MainActivity 7 | 8 | -------------------------------------------------------------------------------- /JsBridge_Android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |