Android和JS的交互問題
問題描述
前端寫了這樣的代碼。
我應(yīng)該怎么響應(yīng)呢?大神請幫忙解答一下,不要說 讓我去百度了。我找不到不會(huì)才提問的。
問題解答
回答1:讓你去Google 你去嗎?哈哈哈,說著玩的。。。下面是解答:
return true;
是想把這個(gè) 布爾值返回給Android端嗎?如果是就調(diào)用Android 中約定好的方法。如果想從Javascript調(diào)的方法里面獲取到返回值,只需要定義一個(gè)帶返回值的@JavascriptInterface方法:
public class AndroidMessage {@JavascriptInterfacepublic String getMsg() { return 'form java';} }
添加Javascript的映射Webview:
webView.addJavascriptInterface(new AndroidMessage(), 'AndroidMessage');
Javascript直接調(diào)用Java方法:
function showAlert(){var str=window.AndroidMessage.getMsg();console.log(str); }
你可以參考這里:Android 與 js 交互
回答2:可以使用JavascriptInterface, 具體看WebView的addJavascriptInterface方法:https://developer.android.goo... java.lang.String).不過17以前存在漏洞,JS可以通過反射獲取到App的公開屬性和方法,并使用App已有的權(quán)限
This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk for apps targeting JELLY_BEAN or earlier. Apps that target a version later than JELLY_BEAN are still vulnerable if the app runs on a device running Android earlier than 4.2. The most secure way to use this method is to target JELLY_BEAN_MR1 and to ensure the method is called only when running on Android 4.2 or later. With these older versions, JavaScript could use reflection to access an injected object’s public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content.
可以使用這個(gè)JSBridge, 不過這個(gè)用了以后就不能使用WebView.setWebViewClient()方法:https://github.com/lzyzsd/JsB...
如果功能簡單的話, 參考上面JSBridge的思路, 可以自定義實(shí)現(xiàn)WebView的alert方法,用了傳事件
話說SF怎么知道我剛剛弄了Android和JS交互?
回答3:WebView有一個(gè)addJavascriptInterface方法,可以看下這個(gè),或者自己通過WebChromeClient自己實(shí)現(xiàn)。還有就是也可以使用第三方封裝,比如cordovacordova-android。
回答4:不去百度, 那就去Google呀...
JS代碼不能這么寫, Native & JS 之間需要有一個(gè)JS對(duì)象才能完成交互.
參考資料:Android中Java與JavaScript交互的幾種方式
回答5:先吐個(gè)槽,你們這前端相當(dāng)于什么都沒干嘛。。。我接觸這方面的東西不算多,也就是做過一些簡單的交互,說一點(diǎn)粗淺的用法關(guān)于js與Android原生交互,Android本身提供了JavaScriptInterface注解,使用這個(gè)注解可以實(shí)現(xiàn)兩端的交互,但是回調(diào)執(zhí)行起來卻不怎么方便(或許是我使用的姿勢不對(duì))。同時(shí)這里有一個(gè)不錯(cuò)的輪子,使用起來更簡單些,你也可以參考下:jsBridge.
回答6:去了解一下android和JS交互的基礎(chǔ)原理,其中之一是WebView.addJavaScriptInterface(),這個(gè)看明白了,你就知道該怎么做了,如果你僅僅是想實(shí)現(xiàn)個(gè)back的功能,非常的簡單,如果你的android和JS有很多類似于這樣的交互,建議你引入框架,比如JSBridge,這個(gè)框架把a(bǔ)ndroid和JS的通信進(jìn)行了封裝,所以大規(guī)模交互的時(shí)候,你用起來會(huì)輕松很多。望采納
回答7:addJavaScriptInterface這個(gè)方法是最簡單的了。假如還要更高級(jí)的,就需要看下下面這篇文章,寫的挺好。好好和h5溝通!幾種常見的hybrid通信方式
相關(guān)文章:
1. android-studio - Android Studio 運(yùn)行項(xiàng)目的時(shí)候一堆警告,跑步起來!?2. 如何解決Centos下Docker服務(wù)啟動(dòng)無響應(yīng),且輸入docker命令無響應(yīng)?3. MySQL數(shù)據(jù)庫中文亂碼的原因4. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。5. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?6. dockerfile - [docker build image失敗- npm install]7. angular.js - 關(guān)于$apply()8. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?9. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???10. nignx - docker內(nèi)nginx 80端口被占用
