项目信息:
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);