简体   繁体   中英

Phonegap android native function not getting called on button click

To share the data over Email i had done this in android native code, and on "onClick" of hmtl button email function has to be called. but here the Email function is not fired up, and even did not get any error in logcat to rectify this issue, please help me

html file

 <!DOCTYPE HTML>
     <html>
     <head>
    <link rel="stylesheet" href="index.css" /> 
<script type="text/javascript" charset="utf-8" src="libs/cordova-2.4.0.js">  </script>
<script type="text/javascript" charset="utf-8" src="social.js"></script>
<script type="text/javascript" charset="utf-8" src="Esocial.js"></script>
    <script type="text/javascript" charset="utf-8">

 function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
  }
function onDeviceReady() 
{
 window.SocialShare = new SocialShare(); 
console.log("in onDeviceReady");
 }


function sendEmail()
  {
SocialShare.Email= function(param, successCallback, failCallback) {
    window.SocialShare.email({message:'Email body', subject: "Email Subject"},
        function(msg) {
        alert('Write Some real Text please.');
        },
        function(fail) {  

        }
    );
}
  }   
</script>

 </head>
<body onload="onLoad()">

<button onclick="sendEmail()" id="email">Email send </button>
 </body>
</html>

java code

  public class SocialShare extends CordovaPlugin{

private String callback;
Context context;
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try
    {
        if( action.equals("startEmailActivity") ) 
        {
            JSONObject obj = args.getJSONObject(0);
            String msg = obj.has("message") ? obj.getString("message") : "";
            String subject = obj.has("subject") ? obj.getString("subject") : "";

            startEmailActivity(msg, subject );
        }


    }
    catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }


    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
    mPlugin.setKeepCallback(true);
    this.callback = callbackId;
    return mPlugin;
}



public void startEmailActivity ( String msg, String emailaddress )
{

    GMailSender mailsender = new GMailSender("abc@gmail.com", "xxxxx");

    String[] toArr = { "abc@gmail.com", "abc@gmail.com" };
    mailsender.set_to(toArr);
    mailsender.set_from("sender@gmail.com");
    mailsender.set_subject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
    mailsender.setBody("Email body.");

    try {
        //mailsender.addAttachment("/sdcard/filelocation");

        if (mailsender.send()) {

            Toast.makeText(context,
                    "Email was sent successfully.",
                    Toast.LENGTH_LONG).show();
        } else {

            Toast.makeText(context, "Email was not sent.",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {

        Log.e("MailApp", "Could not send email", e);
    }
}
}

plugin added in config.xml

<plugin name="SocialShare" value="com.gmail.plugins.SocialShare"/>

locat output

08-29 09:56:18.934: D/dalvikvm(2566): GC_FOR_ALLOC freed 35K, 6% free 2475K/2632K,  paused 88ms, total 90ms
08-29 09:56:18.944: I/dalvikvm-heap(2566): Grow heap (frag case) to 3.139MB for 635812-byte allocation
08-29 09:56:19.054: D/dalvikvm(2566): GC_FOR_ALLOC freed 1K, 5% free 3095K/3256K, paused 109ms, total 109ms
08-29 09:56:19.104: D/dalvikvm(2566): GC_CONCURRENT freed <1K, 5% free 3100K/3256K, paused 4ms+11ms, total 50ms
08-29 09:56:19.224: D/JsMessageQueue(2566): Set native->JS mode to 2
08-29 09:56:19.423: D/gralloc_goldfish(2566): Emulator without GPU emulation detected.
08-29 09:56:20.644: D/chromium(2566): Unknown chromium error: -6
08-29 09:56:22.944: D/TilesManager(2566): Starting TG #0, 0x2a2600f8

you have done big mistake to add cordova-2.4.0.jar file but there you need to add cordova-2.4.0.js file

You have done

    <script type="text/javascript" charset="utf-8" src="libs/cordova-2.4.0.jar">

you need to replace with

    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM