Compiling Java object-oriented method using the Phonegap plug-in

Source: Internet
Author: User

In the previous blog post, I reproduced the compiling method of phonegap written by others. Let's take a look at it briefly:

Cordova has the Plugin class, and the written plug-ins inherit from this class. The exec method is used to determine the action to be executed based on the command of the second parameter. All plug-ins must override this exec method. Then, add the record of the plug-in by modifying plugin. xml. Then I wrote JavaScript. Js uses the exec method of cordova to implement communication between js and java. Then you can use window. Plugin. * To use the defined js plug-in. Obviously, the code in java is based on the process-oriented idea, and the real object-oriented idea seems to be more fully embodied in js.

Java is a typical advanced object-oriented language. So I found a simpler method to develop the phonegap plug-in. In this way, js and java-related connections can be connected by using the addJavascriptInterface () method of appView in the main class (the main class inherits from Droidap. Java code still follows the object-oriented idea. The two parameters of that method are java objects and js objects. Then, we can use java objects in js. Other java objects associated with that java object can be obtained and used directly. This interface is like a js hole leading to java. the java objects you can see through this hole can be directly used in js.

The following is an example program:

Package com. yelbosh. plugin;

Import org. apache. cordova. DroidGap;

Import android. content. Context;
Import android. telephony. TelephonyManager;
Import android. webkit. WebView;
Import com. yelbosh. plugin. People;

Public class MyClass {
Private WebView mAppView;
Private DroidGap mGap;

Public MyClass (DroidGap gap, WebView view)
{
MAppView = view;
MGap = gap;
}

Public String getTelephoneNumber (){
TelephonyManager tm =
(TelephonyManager) mGap. getSystemService (Context. TELEPHONY_SERVICE );
String number = tm. getLine1Number ();
// String number = "11111 ";
Return number;
}
Public People getPeople (){
People mypeo = new People ("hahah ");
Return mypeo;
}
}

This is the defined plug-in, and the class "People" is also used. The People class is defined as follows:

Package com. yelbosh. plugin;

Public class People {
Private String name;
Public People (String name ){
This. name = name;
}
Public String getName (){
Return this. name;
}

The main class is defined as follows:

Public class MainActivity extends DroidGap {

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. activity_main );
Super. loadUrl ("file: // android_asset/www/test.html ");
AppView. addJavascriptInterface (new MyClass (this, this. appView), "MyCls ");
}

In js code, you can directly use the MyClass object through the MyCls object. The usage is the same.

Note that the window is a global variable. Because it is directly defined in the browser.

A = window. MyCls. getPeople ()

Alert (a. getName ())

You can get the desired result.

Think about it. In fact, the process of writing plug-ins is the process of extending the browser, so that js Code can know what those plug-ins are doing.

So what is the principle of phonegap?

Android: intercept the JavaScript prompt command to implement communication from JavaScript to local code. JavaScript
By default, the prompt command will pop up a dialog box. The Android local code of PhoneGap intercepts the dialog box and obtains JavaScript data. Correspondingly, the PhoneGap on Android implements an HTTP server using Java. Through persistent XHR connections, JavaScript can continuously poll the information stored in the internal XHR server, thus, the communication from Java to JavaScript is realized.

Most of the phonegap plug-ins are functional rather than views.


You can package your own plug-ins for future use. But be sure to make the add declaration in the main class. Perhaps this can be implemented through xml.

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.