I have developing phonegap android application. I'm trying to establish the communication between javascript and android java classes.
For Communication i have used addJavaScriptInterfaceMethod in android main activity.So that i can use java object and method in javascript.
following is the Java Code :
public class Activity extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setIntegerProperty("loadUrlTimeoutValue", 60000);
super.loadUrl("file:///android_asset/www/jq3.html");
MapTablesWrapper table = new MapTablesWrapper();
super.appView.addJavascriptInterface(new MapTablesWrapper(), "tables");
}
}
And in javascript :the following code does not printing the table value.It's alerting undefined
var tableName = tables.getTableName();
function(){
alert(tableName);
}
And if i declare var table inside the function.Its printing the tableName and following code is
function(){
var table = tables.getTableName();
alert(tableName);
}
But i would like to declare variable outside of the function and want to use that varaible in inside the function.
You can do like
var global = {
table : null
};
function()
{
global.table = tables.getTableName();
alert(global.table);
}
you can rename global as your JS file name, so that this variable can be accessed all the functions in the current file and also in any other JS File by using your fileName.table here it is global.table
Related
I am developing an Android app using PhoneGap system. What I would like to do is to call a java method from javascript. This is what I tried to do:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.ldpi);
super.setStringProperty("loadingDialog", "Loading Silver Angel ...");
appView.addJavascriptInterface(this, "MainActivity");
super.setIntegerProperty("loadUrlTimeoutValue", 50000);
super.loadUrl("file:///android_asset/www/index.html",50000);
}
public String customFunctionCalled() {
return "test";
}
In my HTML:
function capturePhotoRisk() {
var testing = window.MainActivity.customFunctionCalled();
alert(testing);
}
When I click a button in the app, I want to display return that text for example. Now the problem that I keep facing is this error: Uncaught TypeError: Object [object Object] has no method 'customFunctionCalled' at file:///android_asset/www/index/html!!
I am working on an android project using HTML and webview to display.
I have
display.loadUrl("file:///android_asset/index.html");
display.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url)
{
display.loadUrl("javascript:openDialog()");
}
});
and this works perfectly. But the javascript function i want to call is in another page (chat.html). How do i call the javascript functions on this pages from java?
If you are the owner of the webpage (chat.html), you can integrate a JS-function which invokes a native method. And in this native method you can call your target-JS:
chat.html:
function callItNow() {
if (typeof Android != "undefined"){
if (Android.caller!= "undefined") {
Android.caller();
}
}
}
in native Code, define a class:
class MyJavascriptBridge {
public void caller() {
//now you know you are on the right place (chat.html)
webView.loadUrl("javascript:openDialog()");
}
}
and of course you have to declare the bridge to your webview:
webView.addJavascriptInterface(new MyJavascriptBridge(), "Android");
suppose you'r function in javascript is hello.
webview.loadUrl("javascript:hello();");
I think that'll do it.
Actually in my Flex sample applicaion have Main mxml called Demo.mxml..
in the main mxml file have login button when we click login button Login.mxml file called
protected function button2_clickHandler(event:MouseEvent):void
{
PopUpManager.createPopUp(this,Login);
}
In Login.mxml file doing some authetication useing java..
public var userService:UserService = new UserService();
[Bindable] public var userVO:UserVO = new UserVO();
protected function loginUser(event:MouseEvent):void
{
var rpcAuthenticateUser:AsyncToken = userService.authenticateUser(userid_id.text, password_id.text);
rpcAuthenticateUser.addResponder(new mx.rpc.Responder(handle_authenticate_success, handler_failure));
}
........
userVO=userService.getUser();
......
All are done in Login.mxml file correctly Now i am getting value.
How to get userVO object in Demo.mxml file ?
Actually i'm trying but it give some Null values......Plz help me
Thanks in Advance...
create a model class that is a singleton See my answer here about making a singleton.
then in your Demo.mxml
[Bindable] public var model:MyModel = MyModel.getInstance();
then in your login form
public var model:MyModel = MyModel.getInstance();
then when you get the response from the service:
model.userVO = userService.getUser();
now in your Demo.mxml, userVO is now populated and usable there.
In my Application using Flex-Blazeds-java...,in my Flex application side have two mxml file
Main.mxml
Login.mxml
In Main.mxml file have button called Login click this button one popup open that is called Login.mxml
in this File i have authentication logic to connect java...sample code`
public var userService:UserService = new UserService();
[Bindable] public var userVO1:UserVO = new UserVO();
protected function loginUser(event:MouseEvent):void
{
var rpcAuthenticateUser:AsyncToken = userService.authenticateUser(userid_id.text, password_id.text);//Hear authenticateUser(-,-) is a java method it return UserVO object
rpcAuthenticateUser.addResponder(new mx.rpc.Responder(handler_success, handler_failure));
}
private function handler_failure(event:FaultEvent): void {
Alert.show("in handler_failure :" + event.message);
}
private function handler_success(event:ResultEvent): void {
userVO = event.result as UserVO;
Alert.show("test "+userVO.loginId);
}
Hear Login Working Perfectly according my Database logic and also if it is ResultEvent the Alert box show correct value (for ex:loginId is 'narasimham')...and everthing working perfectly no default in Login.mxml
Now The Problem Start...
I want to Use UserVO object in Main.mxml file so in that i'm using following code..
public var loginUserVar:Login = new Login();
protected function afterLoginUser(event:FlexEvent):void
{
Alert.show("LoginId ="+loginUserVar.userVO.loginId);
}
Actually my thinking this Alert box giving value narasimham but it is giving null value.
Why it is giving Null value?Is their any Scope specify to create variable?
In handler_success you need to set the value of userVO1 otherwise it won't be available otuside of your mxml file. You also need to to reference it in afterLoginUser as userVO1 instead of userVO.
Correct Ethrbunny i'm not store the value of userVO object so it is not available to Out side mxml file....
That's way in Flex(3.5) Application in Login.mxml file i'm adding following code...
Application.application.userVO = event.result as UserVO;
//Hear userVO is Object defined in Main.mxml file....
In an iOS app, I used
stringFromJavaScript = [webView stringByEvaluatingJavascriptFromString:#"document.getElementById(\"image\").getAttribute(\"src")"];
To get the src directory of the image that was being displayed on the webView. I want to do the same for Android. What are my options?
Basically the intent is to capture the path so that I can email this same picture...
ie.
"picture.php?image=%#",stringFromJavascript
This way, that same image would be loaded when the user clicks the link, or posts it to facebook etc.
Yeah, I miss this method greatly in Android ;)
To execute JavaScript and get response you can do as follows:
Define JavaScript callback interface in your code:
class MyJavaScriptInterface {
#JavascriptInterface
public void someCallback(String jsResult) {
// your code...
}
}
Attach this callback to your WebView
MyJavaScriptInterface javaInterface = new MyJavaScriptInterface();
yourWebView.addJavascriptInterface(javaInterface, "HTMLOUT");
Run your JavaScript calling window.HTMLOUT.someCallback from the script:
yourWebView.loadUrl("javascript:( function () { var resultSrc = document.getElementById(\"image\").getAttribute(\"src\"); window.HTMLOUT.someCallback(resultSrc); } ) ()");
Hope this helps!