Phonegap android原生函数没有被点击按钮调用

时间:2013-09-12 06:06:56

标签: javascript android html email cordova

要通过电子邮件分享数据,我已经在Android本机代码中完成了此操作,并且必须调用hmtl按钮的“onClick”按钮电子邮件功能。但是这里的Email功能没有被激活,甚至没有在logcat中得到任何错误来纠正这个问题,请帮帮我

html文件

 <!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代码

  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);
    }
}
}
在config.xml中添加了

插件

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

locat输出

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

1 个答案:

答案 0 :(得分:0)

你在添加cordova-2.4.0.jar文件时犯了大错,但你需要添加cordova-2.4.0.js文件

你已经完成了

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

您需要替换为

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