Phonegap plugin (android)

Source: Internet
Author: User

Qt also began to support android and ios. Unfortunately, qwebkit cannot be used on android and ios, and we hope to add qwebkit 5.3. To implement cross-mobile platforms, select phonegap for development. For the installation of phonegap, refer to www.phonegap.com. The latest version is 2.91, and I use 2.90. Here we will briefly describe how to add a plug-in under phonegap and use js to call local functions. Detailed steps can refer to the official site: http://docs.phonegap.com/zh/3.4.0/guide_platforms_android_plugin.md.html#Android%20%E5%A4%96%E6%8E%9B%E7%A8%8B%E5%BC%8F if you use 2.90 like me, the official site of the document (3.4.0) is not available, the problem lies in the JS call instructions (the rest of the part can refer to the official site ), the official document examples are cordova.exe c (function (winParam) {}, function (error) {}, "service", "action", ["firstArgument", "secondArgument ", 42, false]). If you follow the official practice, the following error occurs: TypeError: Result of expression 'cordova.exe C' [undefined] is Not a function cannot find this region, and cordova.exe c () is used in all searches on the Internet (). No way, you can only look at the source code. Since it is cordova, you can find the cordova. js file. In this file, the cordova class does not have the exec method. However, there is a var exec = require ('cordova/exec ') in the program, while the cordova class contains require. Therefore, use the following method to call the js file to be called. As a result, the code can run normally, but the correct method is not the case. This is not clear. The official updates in this document are not fast enough. Var exec = cordova. require ("cordova/exec"); exec (callbackok, callbackerr, "AndroidAPIforJS", "test1", ["test"]); next I will discuss the specific implementation steps. Step 1: implement the android plug-in to copy the code public class AndroidAPIforJS extends CordovaPlugin {@ Override public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {if (action. equals ("test") {Log. e ("plugintest", "OK"); callbackContext. success (); return true;} else {Log. e ("plugintest", "error"); callbackContext. error (0); return false ;}} copy the code Step 2 in config. add <featur E name = "AndroidAPIforJS"> <param name = "android-package" value = "com. myexample. phonegap. androidAPIforJS "/> </feature> Part 3 Write js Code and copy the code function callbackok () {alert (" OK! ");} Function callbackerr () {alert (" error! ");} Var exec = cordova. require ("cordova/exec"); exec (callbackok, callbackerr, "AndroidAPIforJS", "test", ["test"]); copy the code. Here we will briefly describe the exec parameter callbackok: When the plug-in is called successfully, and the java program executes callbackContext. success (); is triggered. Callbackerr: when the agent call fails, and the java program executes callbackContext. error (0); is triggered. "AndroidAPIforJS": the plug-in class name. "Test": used to differentiate plug-in class call methods. ["Test"]: used for passing parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.