phonegap发送短信插件

项目信息:


1 java端代码:

package com.cordova.sms;

import java.io.File;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.telephony.SmsManager;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class SMS extends Plugin {

	@Override
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("sendSms")) {
            	sendSms(args.getString(0));
            }
            else {
                status = PluginResult.Status.INVALID_ACTION;
            }
            return new PluginResult(status, result);
        } catch (JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    private void sendSms(String url) {
    	Uri uri=Uri.parse("smsto:");
        Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
        intent.putExtra("sms_body", url);
        this.ctx.startActivity(intent);
    }

}

2 插件及权限注册:

在AndroidManifest.xml中,注册权限:<uses-permission android:name="android.permission.SEND_SMS" />

在res/xml/plugins.xml中,插件添加<plugin name="SMS" value="com.cordova.sms.SMS"/>

name为java类名,value为java在项目中的位子

3 js代码编写

/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 *
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2011, IBM Corporation
 */


/**
 * Constructor
 */
function SendSmser() {
};




SendSmser.prototype.send = function(sms) {
    PhoneGap.exec(null,
    
    null,
    "SMS",
    "SendSMS", 
    [sms]);
    
};






PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("sendSmser", new SendSmser());
});//注册插件

//

PhoneGap.exec(null,
    
    null,
    "sendSms",
    "SendSMS", 
    [sms]);中

SMS为java类名,sendSms是行为。

4 插件的调用:

window.plugins.sendSmser.send(success, error, tel, content);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值