package cordova.plugin.test;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.Toast;
import com.kevin.test.Operation;
/**
* 继承CordovaPlugin接口,重写execute方法
*/
public class TestPlugin extends CordovaPlugin {
/**
* action:js传来的参数,通过匹配执行相应的方法
* args:js传来的参数,以JSONArray格式,通过get方法获取值,它携带了从H5中传递过来的参数
* callbackContext:java提供的接口供回掉给H5
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if(action.equals("add")){
int a=args.getInt(0);
int b=args.getInt(1);
int result=Operation.Add(a,b);
callbackContext.success(result);
return true;
}else if(action.equals("showToast")){
String msg=args.getString(0);
Toast.makeText(cordova.getActivity(), msg, Toast.LENGTH_SHORT).show();
callbackContext.success("success" + msg);
return true;
}
return false;
}
}