In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to write the JS call Android source code in Html5 under the Cordova framework. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
According to my usual routine, I will say a little nonsense first. What is the relationship between PhoneGap and Cordova? Why is it called Cordova in some places and PhoneGap in others? PhoneGap is a HTML5 platform through which developers can use HTML, CSS and JavaScript to develop local mobile applications. As a result, developers can currently write applications only once and then distribute them in six major mobile platforms and application stores (app store), including iOS, Android, BlackBerry, webOS, bada and Symbian. Apache Cordova is an open source project after PhoneGap contributed to Apache. It is the core code extracted from PhoneGap and the core engine that drives PhoneGap. The general meaning is: Cordova is developed from PhoneGap, and now Cordova is the PhoneGap at that time. So what I call it back and forth below is actually the same thing. Back to the point, how do you implement JS calling Android native code under the PhoneGap or Cordova framework? (let's not dwell on how to integrate PhoneGap into Android programs.)
1. Config.js defines the name and method of your Plugin in your html5. Named MyPlugin, there are two plug-ins: one for automatic updates (Update) and one for password locking (PassLock)
Var MyPlugin = {UpDate: function (success, fail, url) {return PhoneGap.exec (function (args) {success (args);}, function (args) {fail (args);}, 'UpDate',' Update', [url]) }, PassLock: function (success, fail, url) {return PhoneGap.exec (function (args) {success (args);}, function (args) {fail (args);}, 'PassLock',' PassLock', [url]);}}
two。 Register your Plugin in plugin under the xml folder.
3. Customize your Plugin classes and processing methods. (only the code for automatically updating the plug-in is posted here, which is very simple, just a jump
Ublic class UpdatePlugin extends Plugin {@ Override public PluginResult execute (String arg0, JSONArray arg1, String arg2) {/ * * Jump to UpdateActivity * / Intent intent = new Intent (ctx.getContext (), UpdateActivity.class); ctx.startActivity (intent); String result = "jump to UpdateActivity"; PluginResult pluginResult = new PluginResult (Status.OK, result) Return pluginResult;}}
4. Call your plug-in in your JS.
/ / automatically update upDate: function () {/ / call the native method of android to check and update MyPlugin.UpDate (function () {}, function () {}, 'here is a parameter, I don't involve the use of parameters here, so write casually') }, passLock: function () {/ / call android native method for password locking related operation MyPlugin.PassLock (function () {}, function () {}, 'here is a parameter, I do not involve the use of parameters here, casually write');}
5. Test, when you click one of your buttons or links to execute your update:function JS, and then call the Update plug-in under MyPlugin in JS, which is defined in config.js, registered in plugin.xml, and the execute () method in UpdatePlugin where the plug-in is executed. After executing this method, you have already called the native code of Android, of course I am set to jump to another Activity.
The required tools are packaged and downloaded: http://pan.baidu.com/share/link?shareid=1636341858&uk=2937351404
The above is how to write the JS call Android source code in Html5 under the Cordova framework. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.