下面列出了org.apache.cordova.CallbackContext#sendPluginResult ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback id used when calling back into JavaScript.
* @return True if the action was valid, false otherwise.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (action.equals("getConnectionInfo")) {
this.connectionCallbackContext = callbackContext;
NetworkInfo info = sockMan.getActiveNetworkInfo();
String connectionType = "";
try {
connectionType = this.getConnectionInfo(info).get("type").toString();
} catch (JSONException e) { }
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
return true;
}
return false;
}
@Override
public void execute(String rawArgs, CallbackContext ctx) {
try {
JSONArray args = new JSONArray(rawArgs);
int id = Integer.parseInt(args.getString(0), 16);
int code = args.getInt(1);
String reason = args.getString(2);
Connection conn = _map.get(id);
if (conn != null) {
if (code > 0) {
conn.close(code, reason);
} else {
conn.close();
}
}
} catch (Exception e) {
if (!ctx.isFinished()) {
PluginResult result = new PluginResult(Status.ERROR);
result.setKeepCallback(true);
ctx.sendPluginResult(result);
}
}
}
/**
* Executes the request.
*
* @param action The action to execute.
* @param args The exec() arguments.
* @param callbackId The callback id used when calling back into JavaScript.
* @return Whether the action was valid.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (action.equals("start")) {
this.callbackContext = callbackContext;
if (this.status != AccelListener.RUNNING) {
// If not running, then this is an async call, so don't worry about waiting
// We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
this.start();
}
}
else if (action.equals("stop")) {
if (this.status == AccelListener.RUNNING) {
this.stop();
}
} else {
// Unsupported action
return false;
}
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, "");
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
return true;
}
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals("upload") || action.equals("download")) {
String source = args.getString(0);
String target = args.getString(1);
if (action.equals("upload")) {
try {
source = URLDecoder.decode(source, "UTF-8");
upload(source, target, args, callbackContext);
} catch (UnsupportedEncodingException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "UTF-8 error."));
}
} else {
download(source, target, args, callbackContext);
}
return true;
} else if (action.equals("abort")) {
String objectId = args.getString(0);
abort(objectId);
callbackContext.success();
return true;
}
return false;
}
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals("upload") || action.equals("download")) {
String source = args.getString(0);
String target = args.getString(1);
if (action.equals("upload")) {
try {
source = URLDecoder.decode(source, "UTF-8");
upload(source, target, args, callbackContext);
} catch (UnsupportedEncodingException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "UTF-8 error."));
}
} else {
download(source, target, args, callbackContext);
}
return true;
} else if (action.equals("abort")) {
String objectId = args.getString(0);
abort(objectId);
callbackContext.success();
return true;
}
return false;
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback id used when calling back into JavaScript.
* @return True if the action was valid, false otherwise.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (action.equals("getConnectionInfo")) {
this.connectionCallbackContext = callbackContext;
NetworkInfo info = sockMan.getActiveNetworkInfo();
String connectionType = "";
try {
connectionType = this.getConnectionInfo(info).get("type").toString();
} catch (JSONException e) { }
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
return true;
}
return false;
}
/**
* Get information about app and web versions.
*
* @param callback callback where to send the result
*/
private void jsGetVersionInfo(final CallbackContext callback) {
final Context context = cordova.getActivity();
final Map<String, Object> data = new HashMap<String, Object>();
data.put("currentWebVersion", pluginInternalPrefs.getCurrentReleaseVersionName());
data.put("readyToInstallWebVersion", pluginInternalPrefs.getReadyForInstallationReleaseVersionName());
data.put("previousWebVersion", pluginInternalPrefs.getPreviousReleaseVersionName());
data.put("appVersion", VersionHelper.applicationVersionName(context));
data.put("buildVersion", VersionHelper.applicationVersionCode(context));
final PluginResult pluginResult = PluginResultHelper.createPluginResult(null, data, null);
callback.sendPluginResult(pluginResult);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
if (action.equals("get")) {
TelephonyManager telephonyManager = (TelephonyManager)this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
result = telephonyManager.getDeviceId();
}
else {
status = PluginResult.Status.INVALID_ACTION;
}
callbackContext.sendPluginResult(new PluginResult(status, result));
return true;
}
/**
* Requests a filesystem in which to store application data.
*
* @param type of file system requested
* @param requiredSize required free space in the file system in bytes
* @param callbackContext context for returning the result or error
* @throws JSONException
*/
private void requestFileSystem(int type, long requiredSize, final CallbackContext callbackContext) throws JSONException {
Filesystem rootFs = null;
try {
rootFs = this.filesystems.get(type);
} catch (ArrayIndexOutOfBoundsException e) {
// Pass null through
}
if (rootFs == null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR));
} else {
// If a nonzero required size was specified, check that the retrieved filesystem has enough free space.
long availableSize = 0;
if (requiredSize > 0) {
availableSize = rootFs.getFreeSpaceInBytes();
}
if (availableSize < requiredSize) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
} else {
JSONObject fs = new JSONObject();
fs.put("name", rootFs.name);
fs.put("root", rootFs.getRootEntry());
callbackContext.success(fs);
}
}
}
/**
* Requests a filesystem in which to store application data.
*
* @param type of file system requested
* @param requiredSize required free space in the file system in bytes
* @param callbackContext context for returning the result or error
* @throws JSONException
*/
private void requestFileSystem(int type, long requiredSize, final CallbackContext callbackContext) throws JSONException {
Filesystem rootFs = null;
try {
rootFs = this.filesystems.get(type);
} catch (ArrayIndexOutOfBoundsException e) {
// Pass null through
}
if (rootFs == null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR));
} else {
// If a nonzero required size was specified, check that the retrieved filesystem has enough free space.
long availableSize = 0;
if (requiredSize > 0) {
availableSize = rootFs.getFreeSpaceInBytes();
}
if (availableSize < requiredSize) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
} else {
JSONObject fs = new JSONObject();
fs.put("name", rootFs.name);
fs.put("root", rootFs.getRootEntry());
callbackContext.success(fs);
}
}
}
private void sendUpdateToListener(ExecuteResult logicResult, Object[] listenerExtras) {
try {
if (listenerExtras != null && listenerExtras.length > 0) {
Log.d(TAG, "Sending update");
CallbackContext callback = (CallbackContext)listenerExtras[0];
callback.sendPluginResult(transformResult(logicResult));
Log.d(TAG, "Sent update");
}
} catch (Exception ex) {
Log.d(TAG, "Sending update failed", ex);
}
}
private void callStatus(CallbackContext callbackContext) {
if (mCall == null) {
callbackContext.sendPluginResult(new PluginResult(
PluginResult.Status.ERROR));
return;
}
String state = getCallState(mCall.getState());
if (state == null) {
state = "";
}
PluginResult result = new PluginResult(PluginResult.Status.OK, state);
callbackContext.sendPluginResult(result);
}
private void getIntent(CallbackContext callbackContext) throws JSONException {
Intent intent = this.cordova.getActivity().getIntent();
PluginResult result = new PluginResult(PluginResult.Status.OK, buildIntent(intent));
callbackContext.sendPluginResult(result);
}
/**
* Start a device discovery.
*
* @param args Arguments given.
* @param callbackCtx Where to send results.
*/
private void startDiscovery(JSONArray args, CallbackContext callbackCtx)
{
// TODO Someday add an option to fetch UUIDs at the same time
try
{
if(_bluetooth.isConnecting())
{
this.error(callbackCtx, "A Connection attempt is in progress.", BluetoothError.ERR_CONNECTING_IN_PROGRESS);
}
else
{
if(_bluetooth.isDiscovering())
{
_wasDiscoveryCanceled = true;
_bluetooth.stopDiscovery();
if(_discoveryCallback != null)
{
this.error(_discoveryCallback,
"Discovery was stopped because a new discovery was started.",
BluetoothError.ERR_DISCOVERY_RESTARTED
);
_discoveryCallback = null;
}
}
_bluetooth.startDiscovery();
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
callbackCtx.sendPluginResult(result);
_discoveryCallback = callbackCtx;
}
}
catch(Exception e)
{
this.error(callbackCtx, e.getMessage(), BluetoothError.ERR_UNKNOWN);
}
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback context from which we were invoked.
* @return A PluginResult object with a status and message.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("clearCache")) {
this.clearCache();
}
else if (action.equals("show")) {
// This gets called from JavaScript onCordovaReady to show the webview.
// I recommend we change the name of the Message as spinner/stop is not
// indicative of what this actually does (shows the webview).
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
webView.postMessage("spinner", "stop");
}
});
}
else if (action.equals("loadUrl")) {
this.loadUrl(args.getString(0), args.optJSONObject(1));
}
else if (action.equals("cancelLoadUrl")) {
//this.cancelLoadUrl();
}
else if (action.equals("clearHistory")) {
this.clearHistory();
}
else if (action.equals("backHistory")) {
this.backHistory();
}
else if (action.equals("overrideButton")) {
this.overrideButton(args.getString(0), args.getBoolean(1));
}
else if (action.equals("overrideBackbutton")) {
this.overrideBackbutton(args.getBoolean(0));
}
else if (action.equals("exitApp")) {
this.exitApp();
}
callbackContext.sendPluginResult(new PluginResult(status, result));
return true;
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return false;
}
}
public void canPurchase(CordovaArgs args, CallbackContext ctx) {
ctx.sendPluginResult(new PluginResult(Status.OK, service.canPurchase()));
}
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
JSONObject obj = new JSONObject();
try{
if (action.equals(GETLOCALENAME)){
obj = getLocaleName();
}else if (action.equals(GETPREFERREDLANGUAGE)){
obj = getPreferredLanguage();
} else if (action.equalsIgnoreCase(DATETOSTRING)) {
obj = getDateToString(data);
}else if(action.equalsIgnoreCase(STRINGTODATE)){
obj = getStringtoDate(data);
}else if(action.equalsIgnoreCase(GETDATEPATTERN)){
obj = getDatePattern(data);
}else if(action.equalsIgnoreCase(GETDATENAMES)){
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD) {
throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
} else {
obj = getDateNames(data);
}
}else if(action.equalsIgnoreCase(ISDAYLIGHTSAVINGSTIME)){
obj = getIsDayLightSavingsTime(data);
}else if(action.equalsIgnoreCase(GETFIRSTDAYOFWEEK)){
obj = getFirstDayOfWeek(data);
}else if(action.equalsIgnoreCase(NUMBERTOSTRING)){
obj = getNumberToString(data);
}else if(action.equalsIgnoreCase(STRINGTONUMBER)){
obj = getStringToNumber(data);
}else if(action.equalsIgnoreCase(GETNUMBERPATTERN)){
obj = getNumberPattern(data);
}else if(action.equalsIgnoreCase(GETCURRENCYPATTERN)){
obj = getCurrencyPattern(data);
}else {
return false;
}
callbackContext.success(obj);
}catch (GlobalizationError ge){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ge.toJson()));
}catch (Exception e){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
return true;
}
public void getProducts(CordovaArgs args, CallbackContext ctx) {
ctx.sendPluginResult(new PluginResult(Status.OK, productsToJSON(service.getProducts())));
}
public void isPurchased(CordovaArgs args, CallbackContext ctx) {
String productId = args.optString(0);
boolean purchased = productId != null ? service.isPurchased(productId) : false;
ctx.sendPluginResult(new PluginResult(Status.OK, purchased));
}
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback context from which we were invoked.
* @return A PluginResult object with a status and message.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("clearCache")) {
this.clearCache();
}
else if (action.equals("show")) {
// This gets called from JavaScript onCordovaReady to show the webview.
// I recommend we change the name of the Message as spinner/stop is not
// indicative of what this actually does (shows the webview).
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
webView.getPluginManager().postMessage("spinner", "stop");
}
});
}
else if (action.equals("loadUrl")) {
this.loadUrl(args.getString(0), args.optJSONObject(1));
}
else if (action.equals("cancelLoadUrl")) {
//this.cancelLoadUrl();
}
else if (action.equals("clearHistory")) {
this.clearHistory();
}
else if (action.equals("backHistory")) {
this.backHistory();
}
else if (action.equals("overrideButton")) {
this.overrideButton(args.getString(0), args.getBoolean(1));
}
else if (action.equals("overrideBackbutton")) {
this.overrideBackbutton(args.getBoolean(0));
}
else if (action.equals("exitApp")) {
this.exitApp();
}
else if (action.equals("messageChannel")) {
messageChannel = callbackContext;
return true;
}
callbackContext.sendPluginResult(new PluginResult(status, result));
return true;
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return false;
}
}