Phonegap (Cordova custom plug-in) for sharing

Source: Internet
Author: User

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

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.