You can use the WebChrome to do almost anything you want with
Javascript. Sorry, I wasn't clear on exactly what you wanted to do, but
here is stripped down version of one my Android Bridge Classes. Each
function is called from Javascript. I should have also included the
index.html file, so I added it below. Just compare the function names in
the Bridge, and you will get the idea... Oh, probably should have also
mentioned that your .css and .js files also go into the assets folder..
Of course.. most of the function names are my own functions.. so change
to yours :
private class AndroidBridge
{
public void sendResults(final String arg)
{
if (handler != null)
{
HotSharedFunctions sharedFunctions = new
HotSharedFunctions();
sharedFunctions.SendMessage(handler,
BistroSharedStatic.MESSAGE_LOCATION_CHOSEN, 0, 0, arg);
sharedFunctions = null;
}
}
public void getParams(final int cellHeight, final int
numberOfValues, final int wrapperTop)
{
scrollCellHeight = cellHeight;
numValues = numberOfValues;
divTop = wrapperTop;
setupTimer();
}
public void readyForJavascript(final String arg)
{
webView.loadUrl("javascript:SpinningWheel.addSlot(" +
valuesString + ", 'center', 2);");
webView.loadUrl("javascript:SpinningWheel.setPosition(0, 2);");
if (sdkVersion < 5)
{
webView.loadUrl("javascript:getSpinnerValues(1);");
}
else
{
webView.loadUrl("javascript:getSpinnerValues(0);");
}
}
}
INDEX.HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="spinningwheel.css" type="text/css"
media="all" />
<script type="text/javascript" src="spinningwheel.js?v=1.4"></script>
<title></title>
<script type="text/javascript">
function getData()
{
var results = SpinningWheel.getSelectedValues();
window.android.sendResults(results.values.join(' ') );
}
function getSpinnerValues(lowerSDKVersion)
{
var cellHeight = SpinningWheel.cellHeight;
if (lowerSDKVersion == 1)
{
SpinningWheel.swSlotWrapper.style.height = (cellHeight * 16);
}
var numberofValues SpinningWheel.slotData[0].length;
var wrapperTop = SpinningWheel.swSlotWrapper.style.top;
window.android.getParams(cellHeight, numberofValues, wrapperTop);
}
function notifyAndroid()
{
window.android.readyForJavascript('');
}
</script>
</head>
<body onload="javascript:notifyAndroid()"></body>
</html>
On 13/07/2010 9:43 AM, kypriakos wrote:
Perfect ! Thanks very much Brad.
Currently when my javascript modules send out messages, my app
intercepts
them manipulates them and then returns them to the open socket that
the
javascript was writing to. Would this be possible in Android? I will
play with
what you are suggesting below and see how it goes.
Thanks again
On Jul 12, 8:20 pm, Brad Gies<[email protected]> wrote:
Yes, you can read pages stored on the phone:
I'm using index.html here. Put it into your assets folder :
webView = (WebView)
spinnerView.findViewById(R.id.spinnerviewwebview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AndroidBridge(), "android");
webView.setVerticalScrollBarEnabled(false);
webView.setWebChromeClient(new WebChromeClient()
{
public void onConsoleMessage(String message, int
lineNumber, String sourceID)
{
}
});
webView.loadUrl("file:///android_asset/index.html");
The above also sets the WebView up to use Javascript.
On 12/07/2010 2:10 PM, kypriakos wrote:
In this case below, the URL can only be public and remote? I could not
read
a page stored locally on the phone - or is storing files locally not
an option?
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Thanks
On Jul 12, 3:21 pm, Mark Murphy<[email protected]> wrote:
On Mon, Jul 12, 2010 at 3:10 PM, kypriakos<[email protected]> wrote:
Instead of launching a UI I was wondering if I could have a process
monitor
the execution of a javascript in the emulator's browser and read in
the output
data. Is it possible to execute a local javascript file using the
phone's browser?
You can use WebView, but only from an Activity.
If you want to embed a Javascript interpreter, you can consider Rhino.
--
Mark Murphy (a Commons
Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
Android App Developer Books:http://commonsware.com/books
--
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro
Blurbhttp://www.bgies.comhttp://www.bistroblurb.comhttp://www.ihottonight.com
-----------------------------------------------------------------------
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead
--
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.ihottonight.com
-----------------------------------------------------------------------
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en