1

I'm trying to do something a little tricky. I have an activity which has a webView inside. This web view is loading an external html file which has some javascript functions.

My activity is producing some reults periodically and I want to access these results in javascript functions. I thought writing output results to a file and reading in javascript side might be a solution. But I'm not sure about it.

Is there a way for doing sth like this ?

I know that we can navigate to android functions from javascript, but it is not exactly what I need.

Thanks..

6
  • not sure if JS can read files stored locally Commented Apr 10, 2012 at 8:56
  • js can read local files. You could place the js files into the assets folder of your app as well as the file to read and it will do it. My app does this to get satellite position, calculations done in the js are then read by my android web view and displayed on screen Commented Apr 10, 2012 at 9:13
  • @Katana24 actually I'm trying to do exactly same thing. Getting gps position and showing on a map. But as I know, I cannot write to a file in asset folder? Commented Apr 10, 2012 at 9:44
  • Not the asset folder (or anywhere in the app itself), but the local file storage directory. Commented Apr 10, 2012 at 9:58
  • So correct me if Im wrong - your activity gets the lat and long of something and writes them to a file. Then you want a javascript file to read from that file right? Commented Apr 10, 2012 at 10:13

1 Answer 1

2

If you need the results only once, and on the fly, then you could try not to write them to a file but to inject it to the WebView as javascript. For example in your page you could add a function:

function newData(jsonObj) { ... } 

and instead of writing to a file you would call this with your actual data.

webview.loadUrl("javascript:(newData({a:1, b:2,...}))"); 
Sign up to request clarification or add additional context in comments.

5 Comments

How can I call javascript function from android activity and inject jsonObj parameter?
added an example to the answer
@Flöcys - I'm not ure if it will work. Because I already loading webview with webView.loadDataWithBaseURL. Then calling loadUrl again might not work. I'll try..
Thank you very much, it works very well! It's definitely better solution than using temporary file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.