Developing a Plugin on Android
For details, click the address above. Here I will only pick the most practical address.
To use the plug-ins in js, you need to call the methods in the following forms: callback success functions, callback failure functions, services deployed in java, and actions ..), And parameter list.
[Javascript]
Xec (<successFunction>, <failFunction>, <service>, <action>, [<args>]);
Exec (<successFunction>, <failFunction>, <service>, <action>, [<args>]);
Plug-in deployment...
[Html]
<Plugin name = "<service_name>" value = "<full_name_including_namespace>"/>
<Plugin name = "<service_name>" value = "<full_name_including_namespace>"/>
Next, go to the instance.
1. Deploy the plug-in config. xml.
[Html]
<Plugin name = "Echo" value = "org. apache. cordova. plugin. Echo"/>
<Plugin name = "Echo" value = "org. apache. cordova. plugin. Echo"/>
2. java implementation is what to do after receiving the parameters passed by the page
[Java]
Import org. apache. cordova. api. CordovaPlugin;
Import org. apache. cordova. api. PluginResult;
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
/**
* This class echoes a string called from JavaScript.
*/
Public class Echo extends CordovaPlugin {
@ Override
Public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
If (action. equals ("echo ")){
String message = args. getString (0); // The page parameters are received here, in the order of passing parameters.
This. echo (message, callbackContext); // when passing the parameter, the current callback context is passed, which is indispensable.
Return true;
}
Return false;
}
Private void echo (String message, CallbackContext callbackContext ){
If (message! = Null & message. length ()> 0 ){
CallbackContext. success (message );
// Json objects can also be passed back to the page as parameters
// JSONObject jsono = new JSONObject ();
// Jsono. put ("message", message );
// CallbackContext. success (jsono );
} Else {
CallbackContext. error ("Expected one non-empty string argument .");
}
}
}
Import org. apache. cordova. api. CordovaPlugin;
Import org. apache. cordova. api. PluginResult;
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
/**
* This class echoes a string called from JavaScript.
*/
Public class Echo extends CordovaPlugin {
@ Override
Public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
If (action. equals ("echo ")){
String message = args. getString (0); // The page parameters are received here, in the order of passing parameters.
This. echo (message, callbackContext); // when passing the parameter, the current callback context is passed, which is indispensable.
Return true;
}
Return false;
}
Private void echo (String message, CallbackContext callbackContext ){
If (message! = Null & message. length ()> 0 ){
CallbackContext. success (message );
// Json objects can also be passed back to the page as parameters
// JSONObject jsono = new JSONObject ();
// Jsono. put ("message", message );
// CallbackContext. success (jsono );
} Else {
CallbackContext. error ("Expected one non-empty string argument .");
}
}
}
3. js implementation is page value transfer and callback. It's easy to understand.
[Javascript]
Cordova.exe c (successFunction, failFunction, "Echo", "echo", [message]); // write [] without parameters. The order of multiple parameters must not be wrong.
Function successFunction (e ){
Alert (e); // e is the message passed
// If the json object is passed in callbackContext. success, it can be directly read here.
// E. message
}
Function failFunction (e ){
// Similar to successFunction, it depends on the information passed in callbackContext. error.
}
Cordova.exe c (successFunction, failFunction, "Echo", "echo", [message]); // write [] without parameters. The order of multiple parameters must not be wrong.
Function successFunction (e ){
Alert (e); // e is the message passed
// If the json object is passed in callbackContext. success, it can be directly read here.
// E. message
}
Function failFunction (e ){
// Similar to successFunction, it depends on the information passed in callbackContext. error.
}
4. This is practical. If there is another thing that cannot be understood, it is related to the thread. Put it here. The second asynchronous execution is not an effect described above...
[Java]
If you need to interact with the UI, you should use the following:
@ Override
Public boolean execute (String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
If ("beep". equals (action )){
Final long duration = args. getLong (0 );
Cordova. getActivity (). runOnUiThread (new Runnable (){
Public void run (){
...
CallbackContext. success (); // Thread-safe.
}
});
Return true;
}
Return false;
}
If you do not need to run on the UI thread, but do not want to block the WebCore thread:
@ Override
Public boolean execute (String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
If ("beep". equals (action )){
Final long duration = args. getLong (0 );
Cordova.getthreadpool(cmd.exe cute (new Runnable (){
Public void run (){
...
CallbackContext. success (); // Thread-safe.
}
});
Return true;
}
Return false;
}
If you need to interact with the UI, you should use the following:
@ Override
Public boolean execute (String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
If ("beep". equals (action )){
Final long duration = args. getLong (0 );
Cordova. getActivity (). runOnUiThread (new Runnable (){
Public void run (){
...
CallbackContext. success (); // Thread-safe.
}
});
Return true;
}
Return false;
}
If you do not need to run on the UI thread, but do not want to block the WebCore thread:
@ Override
Public boolean execute (String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
If ("beep". equals (action )){
Final long duration = args. getLong (0 );
Cordova.getthreadpool(cmd.exe cute (new Runnable (){
Public void run (){
...
CallbackContext. success (); // Thread-safe.
}
});
Return true;
}
Return false;
}
Above.