├── .gitignore ├── AndroidManifest.xml ├── README.md ├── assets ├── WebViewJavascriptBridge.js.txt ├── WebViewJavascriptBridge.min.js.txt └── demo.html ├── gen └── com │ └── example │ └── androidbridge │ ├── BuildConfig.java │ └── R.java ├── ic_launcher-web.png ├── libs └── android-support-v4.jar ├── proguard-project.txt ├── project.properties ├── res ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── drawable-xxhdpi │ └── ic_launcher.png ├── layout │ └── activity_main.xml ├── menu │ └── main.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml ├── values-w820dp │ └── dimens.xml └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── src └── com └── example └── androidbridge ├── BridgeHandler.java ├── BridgeUtil.java ├── BridgeWebView.java ├── CallBackFunction.java ├── DefaultHandler.java ├── MainActivity.java ├── Message.java └── WebViewJavascriptBridge.java /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | bin/* 4 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 13 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JsBridge 2 | ======== 3 | 4 | JsBridge for Android Hybrid 5 | -------------------------------------------------------------------------------- /assets/WebViewJavascriptBridge.js.txt: -------------------------------------------------------------------------------- 1 | ;(function() { 2 | if (window.WebViewJavascriptBridge) { return } 3 | var messagingIframe 4 | var sendMessageQueue = [] 5 | var receiveMessageQueue = [] 6 | var messageHandlers = {} 7 | 8 | var CUSTOM_PROTOCOL_SCHEME = 'yy' 9 | var QUEUE_HAS_MESSAGE = '__QUEUE_MESSAGE__' 10 | 11 | var responseCallbacks = {} 12 | var uniqueId = 1 13 | 14 | function _createQueueReadyIframe(doc) { 15 | messagingIframe = doc.createElement('iframe') 16 | messagingIframe.style.display = 'none' 17 | messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE 18 | doc.documentElement.appendChild(messagingIframe) 19 | } 20 | //add by hq 21 | function isAndroid(){ 22 | var ua = navigator.userAgent.toLowerCase(); 23 | var isA = ua.indexOf("android") > -1; 24 | if(isA){ 25 | return true; 26 | } 27 | return false; 28 | } 29 | //add by hq 30 | function isIphone(){ 31 | var ua = navigator.userAgent.toLowerCase(); 32 | var isIph = ua.indexOf("iphone") > -1; 33 | if(isIph){ 34 | return true; 35 | } 36 | return false; 37 | } 38 | 39 | //set default messageHandler 40 | function init(messageHandler) { 41 | if (WebViewJavascriptBridge._messageHandler) { throw new Error('WebViewJavascriptBridge.init called twice') } 42 | WebViewJavascriptBridge._messageHandler = messageHandler 43 | var receivedMessages = receiveMessageQueue 44 | receiveMessageQueue = null 45 | for (var i=0; i-1;if(isA){return true;} 15 | return false;} 16 | function isIphone(){var ua=navigator.userAgent.toLowerCase();var isIph=ua.indexOf("iphone")>-1;if(isIph){return true;} 17 | return false;} 18 | function init(messageHandler){if(WebViewJavascriptBridge._messageHandler){throw new Error('WebViewJavascriptBridge.init called twice')} 19 | WebViewJavascriptBridge._messageHandler=messageHandler 20 | var receivedMessages=receiveMessageQueue 21 | receiveMessageQueue=null 22 | for(var i=0;i 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 | 27 |

28 |

29 | 31 |

32 |

33 | 34 |

35 |

36 | 37 |

38 | 39 | 100 | 101 | -------------------------------------------------------------------------------- /gen/com/example/androidbridge/BuildConfig.java: -------------------------------------------------------------------------------- 1 | /** Automatically generated file. DO NOT MODIFY */ 2 | package com.example.androidbridge; 3 | 4 | public final class BuildConfig { 5 | public final static boolean DEBUG = true; 6 | } -------------------------------------------------------------------------------- /gen/com/example/androidbridge/R.java: -------------------------------------------------------------------------------- 1 | /* AUTO-GENERATED FILE. DO NOT MODIFY. 2 | * 3 | * This class was automatically generated by the 4 | * aapt tool from the resource data it found. It 5 | * should not be modified by hand. 6 | */ 7 | 8 | package com.example.androidbridge; 9 | 10 | public final class R { 11 | public static final class attr { 12 | } 13 | public static final class dimen { 14 | /** Default screen margins, per the Android Design guidelines. 15 | 16 | Example customization of dimensions originally defined in res/values/dimens.xml 17 | (such as screen margins) for screens with more than 820dp of available width. This 18 | would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). 19 | 20 | */ 21 | public static final int activity_horizontal_margin=0x7f040000; 22 | public static final int activity_vertical_margin=0x7f040001; 23 | } 24 | public static final class drawable { 25 | public static final int ic_launcher=0x7f020000; 26 | } 27 | public static final class id { 28 | public static final int action_settings=0x7f070002; 29 | public static final int button=0x7f070000; 30 | public static final int webView=0x7f070001; 31 | } 32 | public static final class layout { 33 | public static final int activity_main=0x7f030000; 34 | } 35 | public static final class menu { 36 | public static final int main=0x7f060000; 37 | } 38 | public static final class string { 39 | public static final int action_settings=0x7f050002; 40 | public static final int app_name=0x7f050000; 41 | public static final int button_name=0x7f050003; 42 | public static final int hello_world=0x7f050001; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/ic_launcher-web.png -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/libs/android-support-v4.jar -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library.reference.1=../appcompat_v7_2 16 | -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacin1/JsBridge/e543c5d2f61b037b6b65e6d7974d2cf14abeff3e/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 |