I want to get a return value from Javascript in Android. I can do it with iPhone, but I can't with Android. I used loadUrl, but it returned void instead of an object. Can anybody help me? Thanks.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Use
addJavascriptInterface()
to add a Java object to the Javascript environment. Have your Javascript call a method on that Java object to supply its "return value".On API 19+, the best way to do this is to call evaluateJavascript on your WebView:
Related answer with more detail: https://stackoverflow.com/a/20377857
Here's a hack on how you can accomplish it:
Add this Client to your WebView:
Now in your javascript call do:
Now in the onJsAlert call "message" will contain the returned value.
As an alternative variant that uses a custom scheme:
MainActivity
CustomWebView
customPage.html (located in the assets folded)
Same as
Keith
but shorter answerAnd implement the function
Don't forget to remove the
onData
fromproguard
list (if you have enabled proguard)Here's what I came up with today. It's thread-safe, reasonably efficient, and allows for synchronous Javascript execution from Java for an Android WebView.
Works in Android 2.2 and up. (Requires commons-lang because I need my code snippets passed to eval() as a Javascript string. You could remove this dependency by wrapping the code not in quotation marks, but in
function(){}
)First, add this to your Javascript file:
Then, add this to your Android activity: