Cordova系列二、自定义插件

时间:2025-04-02 10:19:44
public class MyToast extends CordovaPlugin { private static final String TAG = "Toast"; @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); Context context = this.cordova.getActivity().getApplicationContext(); } @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { Activity activity = cordova.getActivity(); if("javaShow".equals(action)) { Toast.makeText(activity, "java show...", Toast.LENGTH_SHORT).show(); } else if("javaShowJs".equals(action)) { String str = args.getString(0); int lenth = args.getInt(1); if (lenth == 1) { Toast.makeText(activity, "::" + str, Toast.LENGTH_LONG).show(); } else { Toast.makeText(activity, "::" + str, Toast.LENGTH_SHORT).show(); } } else if("jsShowJava".equals(action)) { int a = 4, b = 5; if (a > b) { callbackContext.success("sucsucsuc" + a); } else { callbackContext.error("errerrerr" + b); } return true; } else if("jsShowJs".equals(action)) { String text = args.getString(0); if (!(text.equals(""))) { callbackContext.success("js'text: " + text); } else { callbackContext.error("errjsjs"); } return true; } else if("openVideo".equals(action)) { openVideo(args.getString(0)); } callbackContext.success(); return true; } private void openVideo(String text){ String url = text; String extension = MimeTypeMap.getFileExtensionFromUrl(url); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); Intent mediaIntent = new Intent(Intent.ACTION_VIEW); mediaIntent.setDataAndType(Uri.parse(url), mimeType); //startActivity(mediaIntent); cordova.startActivityForResult((CordovaPlugin) this, mediaIntent, 200); } }