单击“发送”按钮时没有任何操作。在Android环境中从assets / WWW构建应用程序时,Smsplugin.js消失了。我在互联网上尝试了很多方法,但没有任何效果。
SmsPlugin.js
var SmsPlugin = function () {};
SmsPlugin.prototype.SendSMS = function (successCallback,failureCallbackphone, phone,message, method) {
return cordova.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message,method]);
};
cordova.addConstructor(function() {
cordova.addPlugin("SmsPlugin", new SmsPlugin());
});
主要javascript文件
function send(){
var myElement = document.getElementById('btnDefaultSMS');
myElement.onclick = function() {
var number = $("#numberTxt").val();
var message = $("#messageTxt").val();
window.SmsPlugin.SendSMS(number , message,
function () {
alert('Message successfully sent to' + contactNumber);
},
function (event) {
alert('Message failed due to:' + event);
}
,"INTENT");
};
}
document.addEventListener("deviceready", send , false);
SmsPlugin.java
package org.apache.cordova.plugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
public class SmsPlugin extends CordovaPlugin {
public final String ACTION_SEND_SMS = "SendSMS";
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION_SEND_SMS)) {
try {
String phoneNumber = args.getString(0);
String message = args.getString(1);
String method = args.getString(2);
if(method.equalsIgnoreCase("INTENT")){
invokeSMSIntent(phoneNumber, message);
callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.NO_RESULT));
} else{
sendSMS(phoneNumber, message);
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
}
catch (JSONException ex) {
callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.JSON_EXCEPTION));
}
}
return false;
}
private void invokeSMSIntent(String phoneNumber, String message) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", message);
sendIntent.setType("vnd.android-dir/mms-sms");
this.cordova.getActivity().startActivity(sendIntent);
}
private void sendSMS(String phoneNumber, String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
}
}
答案 0 :(得分:0)
看起来您正在尝试将插件添加到纯Cordova应用程序而不是Worklight应用程序......您有read the training materials for using Cordova plug-ins in Worklight吗?
请参阅我对以下问题的回答,详细介绍了HowTo。它也适合“导入”现有的插件。该程序大致相同*:IBM Worklight 6.1 - Cordova plug-in not getting executed
*您提到Worklight 6.0而不是6.1,因此区别在于config.xml
中声明插件的结构;就像文件中其他插件声明一样关注套件......