JSExtend演示了如何在.amx页面中调用定制的Javascript方法。
这种技术非常有用,特别是在调用那些没有暴露在DeviceFeatures DataControl中的Cordova方法。
你也可以增加自己的Javascript方法,然后用这种方式去调用。
JSExtend还演示了如何在Javascript中回调Java方法。
1. MyClass.java 代码
package mobile;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.el.ValueExpression;
import oracle.adfmf.amx.event.ActionEvent;
import oracle.adfmf.framework.api.AdfmfContainerUtilities;
import oracle.adfmf.framework.api.AdfmfJavaUtilities;
public class MyClass {
public MyClass() {
}
// This method calls the "doAlert" javascript function in the "Javascript" feature and passes in a variable number of params
public void FireAlerts(ActionEvent actionEvent) {
AdfmfContainerUtilities.invokeContainerJavaScriptFunction("Javascript", "doAlert", new Object[] {});
AdfmfContainerUtilities.invokeContainerJavaScriptFunction("Javascript", "doAlert", new Object[] {"arg1"});
AdfmfContainerUtilities.invokeContainerJavaScriptFunction("Javascript", "doAlert", new Object[] {"arg1", "arg2"});
}
// This method calls the "fetchPic" javascript function in the "Javascript" feature with no params
public void FetchPic(ActionEvent actionEvent) {
AdfmfContainerUtilities.invokeContainerJavaScriptFunction("Javascript", "fetchPic", new Object[] {});
}
// This method calls the "fetchVideo" javascript function in the "Javascript" feature with no params
public void FetchVideo(ActionEvent actionEvent) {
AdfmfContainerUtilities.invokeContainerJavaScriptFunction("Javascript", "fetchVideo", new Object[] {});
}
// This method will be called by the Javascript method so we show 2-way communication
public void FetchCallback(String path) {
/* Now you have the full path to the file so you can use code like the following to read it
FileInputStream file;
try {
file = new FileInputStream(path);
int bytesread = 0;
byte[] b = new byte[1000];
do {
bytesread = file.read(b);
// now do something with the byte array like copy it somewhere, stream it over a web service, etc
} while (bytesread < 1000);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
*/
// We'll simply set a scoped variable that we are displaying on the page
ValueExpression ve = AdfmfJavaUtilities.getValueExpression("#{pageFlowScope.picpath}", String.class);
ve.setValue(AdfmfJavaUtilities.getAdfELContext(), path);
}
}
2. methods.js 代码
(function () {
// This method shows you how to use variable args and prints out the results
doAlert = function () {
var args = arguments;
var str = "doAlert, argCount:" + args.length + ", arguments:";
for (x = 0;x < args.length;x++) {
if (x > 0) {
str += ", ";
}
str += arguments[x];
}
alert(str);
};
// This method uses PhoneGap and calls the getPicture method to get a picture from the photo library
fetchPic = function () {
navigator.camera.getPicture(onSuccess, onFail,{quality : 50, destinationType : navigator.camera.DestinationType.FILE_URI, sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY});
};
// Once a valid picture returns, it calls back to java with the result
function onSuccess(URI) {
adf.mf.api.invokeMethod("mobile.MyClass", "FetchCallback", URI, onInvokeSuccess, onFail);
};
function onFail() {
alert("It failed");
};
function onInvokeSuccess(param) {
};
// This method uses PhoneGap and calls the getPicture method to get a picture from the photo library
fetchVideo = function () {
navigator.device.capture.captureVideo(captureSuccess, captureFail, {limit : 1});
};
function captureSuccess(mediaFiles) {
var i, len;
for (i=0, len=mediaFiles.length; i
adf.mf.api.invokeMethod("mobile.MyClass", "FetchCallback", mediaFiles[i].fullPath, onInvokeSuccess, onFail);
}
};
function captureFail() {
alert("It failed. Note: This is not supported on the simulator");
};
})();
没有评论:
发表评论