PhoneGap or Cordova framework to implement the HTML5 JS call Android native code

Source: Internet
Author: User

PhoneGap or Cordova Framework implementation HTML5 in JS call Android native code look at the news network > look engine > Open source Products
0 People bookmark this article,


Posted 8 hours ago (2013-09-06 00:39),
Already have -Times Read, Total 0A review

In accordance with my usual routine, I will say a bit of nonsense first.

What's the relationship between PhoneGap and Cordova? for what some place called Cordova and some place called PhoneGap? PhoneGap is a HTML5 flat table. It enables developers to use HTML, CSS, and JavaScript to develop local mobile applications. So now developers can just write an application and advertise it in 6 basic mobile platforms and app stores, mobile platforms and app stores that include: IOS, Android, BlackBerry, WebOS, Bada and Symbian. Apache Cordova is an open source project PhoneGap has contributed to Apache. Is the core code drawn from the PhoneGap. Is the core engine that drives phonegap. Probably meaning:Cordova is developed by PhoneGap, now Cordova is the phonegap of the year.

In the following article I switch back and forth, in fact, is a thing.

The following. How to implement JS to call Android native code in PHONEGAP or Cordova framework? (This is no longer verbose how to integrate PhoneGap in Android programs)

1. Config.js define the name and method of your plugin in your HTML5.

Named Myplugin, there are two plugins: one for self-initiated updates (update). One for Password lock (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]);
     
     }
};

2. Register your plugin in the plugin in the XML directory.

<plugin name= "UpDate" value= "Com.gapsh.plugin.UpdatePlugin"/>
<plugin name= "Passlock" value= "Com.gapsh.plugin.PassLockPlugin"/>


3. Define your plugin class and how to handle it yourself. (This is just the code that posted its own active update plugin, very easy.) Just make a jump)

Public 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 plugin in your JS.

//proactively update yourself
Update:function () {
Call Android native method to check and update
Myplugin.update (function () {},function () {}, ' here is a parameter, I do not involve the use of the reference, so write it casually ');
},
Passlock:function () {
Call the Android native method to do a password lock about the operation
Myplugin.passlock (function () {},function () {}, ' here is a parameter, I do not involve the use of the reference, the random write ');
},

5. Test, when you click on one of your button or link to run your update:function this JS. Then in JS called the Myplugin under the update plugin, the plugin is defined in the config.js. In Plugin.xml, the Execute () method in Updateplugin is the place where the plugin is run in detail. After running this method, you have already called the Android native code. Of course I am here to set up a jump to another activity.

Required Tools package Download: http://pan.baidu.com/share/link?shareid=1636341858&uk=2937351404

PhoneGap or Cordova framework to implement the HTML5 JS call Android native code

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.