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


    一、前端代码的编写

    <!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.");
                }
            }
    }
  • 相关阅读:
    Spiral Matrix
    Merge Intervals
    Edit Distance
    Insertion Sort List
    Add Binary
    Partition List
    Binary Tree Postorder Traversal
    单向链表反转
    Facebook Hacker cup Qualification round Problem 1
    判断二叉树是否为平衡二叉树
  • 原文地址:https://www.cnblogs.com/LO-ME/p/4587169.html
Copyright © 2020-2023  润新知