With the extension of phonegap (Cordova), we can use the local Android or ios code to expand the functions of the web App. The following describes a sharing plug-in, with this feature, we can customize the content of the text message and send it to your friends.
1. First, we will introduce two essential phonegap files: Local. java code Share. java
[Java]
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/
Package com. tricedesigns;
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
Import android. content. Intent;
Import org. apache. cordova. api. Plugin;
Import org. apache. cordova. api. PluginResult;
Public class Share extends Plugin {
@ Override
Public PluginResult execute (String action, JSONArray args, String callbackId ){
Try {
JSONObject jo = args. getJSONObject (0 );
DoSendIntent (jo. getString ("subject"), jo. getString ("text "));
Return new PluginResult (PluginResult. Status. OK );
} Catch (JSONException e ){
Return new PluginResult (PluginResult. Status. JSON_EXCEPTION );
}
}
Private void doSendIntent (String subject, String text ){
Intent sendIntent = new Intent (android. content. Intent. ACTION_SEND );
SendIntent. setType ("text/plain ");
SendIntent. putExtra (android. content. Intent. EXTRA_SUBJECT, subject );
SendIntent. putExtra (android. content. Intent. EXTRA_TEXT, text );
This. cordova. startActivityForResult (this, sendIntent, 0 );
}
}
2. (share. js file)
[Javascript]
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/
Var Share = {
Show: function (content, success, fail ){
Return cordova.exe c (function (args ){
Success (args );
}, Function (args ){
Fail (args );
}, 'Share ', '', [content]);
}
};
3. Then we add the above two files to the phonegap project.
4. Add a statement in plugin. xml (remember to modify packageName)
[Html]
<Plugin name = "Share" value = "com. schaul. plugins. share. Share"/>
5. Define the called js
[Javascript]
Function shareClick (){
Share. show ({
Subject: 'I like turtles ',
Text: 'http: // www.mndaily.com '},
Function () {}, // Success function
Function () {alert ('share failed')} // Failure function
);
}
The effect is as follows:
Author: xiaoguang44