码迷,mamicode.com
首页 > 移动开发 > 详细

phoneGap的Android下编写phonegap 发送短信插件

时间:2015-06-18 23:45:24      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

一、前端代码的编写

<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<title>jQuery  Mobile  Web 应用程序</title>
<link href="jquery.mobile-1.3.2.css" rel="stylesheet" type="text/css"/>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.mobile-1.3.2.js" type="text/javascript"></script>
<script src="cordova.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function(){
        //点击发送按钮调用onSend方法
        $(#send).bind(click, onSend);
    
        var onSend = function(){
        
            var tel = $(#tel).val();
            var content = $(#content).val();
            
            window.send(success, error, tel, content);
            
            var success = function(data){
                alert("发送短信成功tel : " + data);
            };
            
            var error = function(e){
                alert("发送短信失败"+e);
            };
        };
    });
    
    //调用插件  
    window.send = function(success, error, tel, content) {
        cordova.exec(success, error, "Message", "send", [tel,content]);
    };
</script>
</head> 
<body>
<div data-role="page" id="page">
    <div data-role="header">
        <h1>Phonegap发送短信插件</h1>
    </div>
    <div data-role="content">
         <div id="messageDiv">
            <input type="tel" id="tel" value="15201686455" />
            <textarea rows="20" cols="25" id="content">你好树根我们做朋友吧!</textarea>
            <button type="button" id="send">发送</button>           
        </div>
    </div>
    <div data-role="footer">
        <h4>phonegap中文网</h4>
    </div>
</div>

</body>
</html>

 

 

 

二、注册插件

  在Android :res/xml/  这个目录下的配置文件下注册

<feature name="Message">
      <param name="android-package" value="com.example.phonegap.sendmessage.Message"/>
</feature>

 

 

 

三、 设置权限(需要调用权限的地方设置权限) 

  这里是需要发送短信,所以设置发送短信的权限

<uses-permission android:name="android.permission.SEND_SMS" />

 

 

 

 

 

 

四、java 代码编写

  1、创建一个包:com.example.phonegap.sendmessage;

  2、包里创建一个class :Message

package com.example.phonegap.sendmessage;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.telephony.SmsManager;//该程序需要发送短信 所以要加上这个包

public class Message extends CordovaPlugin{
      private static final String SEND = "send";        
        @Override
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
            if (SEND.equals(action)) {
                try {
                    JSONObject jsonObj = new JSONObject();
                    String target = args.getString(0);  //收件人的信息返回
                    String content = args.getString(1);    //发送短信的内容
                    
                    SmsManager sms = SmsManager.getDefault();//活动发送短信的对象
                    sms.sendTextMessage(target, null, content, null, null);//调用发送短信的对象的发送短信的方法
                    
                    jsonObj.put("target", target);
                    jsonObj.put("content", content);
                                  
                    this.echo(target, callbackContext);
                    return true;
                } catch (JSONException ex) {
                    this.echo("FAIL", callbackContext);
                    return false;
                }catch(IllegalArgumentException ex){
                    this.echo("FAIL", callbackContext);
                    return false;
                }
            } else {
                this.echo("FAIL", callbackContext);
                return false;
            }
        }
        private void echo(String message, CallbackContext callbackContext) {
            if (message != null && message.length() > 0) { 
                callbackContext.success(message);
            } else {
                callbackContext.error("Expected one non-empty string argument.");
            }
        }
}

 

phoneGap的Android下编写phonegap 发送短信插件

标签:

原文地址:http://www.cnblogs.com/LO-ME/p/4587169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!