gpt4 book ai didi

android - phonegap SMS 插件中的无效操作错误

转载 作者:太空狗 更新时间:2023-10-29 15:10:54 26 4
gpt4 key购买 nike

我正在为 Android 中的 phonegap 创建一个 SMS 插件。当我尝试发送消息时收到无效操作错误。我看过Android + phonegap +package manager .我的问题是一样的,但链接中的任何答案都没有帮助。

这是我的 .js 文件。

 var SmsPlugin = function () {};

SmsPlugin.prototype.send = function (phone, message, successCallback, failureCallback) {
return cordova.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]);
};

window.sms = new SmsPlugin();

下面是我的 .java 文件

package com.*.PhonegapSMSDPN;

import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

import org.apache.cordova.api.CordovaPlugin;

public class SmsPlugin extends CordovaPlugin {
public final String ACTION_SEND_SMS = "SendSMS";

public PluginResult execute(String action, JSONArray arg1, String callbackId) {
Toast.makeText(this.cordova.getActivity(), action, Toast.LENGTH_SHORT).show();
PluginResult result = new PluginResult(Status.INVALID_ACTION);
Log.e("msg", action);
if (action.equals(ACTION_SEND_SMS)) {
try {
String phoneNumber = arg1.getString(0);
String message = arg1.getString(1);
sendSMS(phoneNumber, message);
result = new PluginResult(Status.OK);
}
catch (JSONException ex) {
result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage());
Log.e("error", result+" ");
}
}

return result;
}

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);
}

}

我也对 config.xml 文件进行了必要的更改。请指导我解决问题。谢谢

PS-我用的是Phonegap2.7.0

最佳答案

您的 js 文件缺少一些代码。我也在 phonegap 中实现了一个短信插件,这是我的工作 js 代码。您应该自定义插件路径。

var SmsPlugin = function() {};

SmsPlugin.prototype.send = function(phone, message, successCallback, failureCallback) {
return PhoneGap.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]);
};

PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("sms", new SmsPlugin());
});

cordova.define("app/scripts/vendor/smsplugin", function (require, exports, module) {
var exec = require('cordova/exec');
var SmsPlugin = function () {};

/**
* Check if the device has a possibility to send and receive SMS
*/
SmsPlugin.prototype.isSupported = function (successCallback, failureCallback) {
return exec(successCallback, failureCallback, 'SmsPlugin', 'HasSMSPossibility', []);
}
/**
* Send a message to the given phone number
*/
SmsPlugin.prototype.send = function (phone, message, successCallback, failureCallback) {
return exec(successCallback, failureCallback, 'SmsPlugin', 'SendSMS', [phone, message]);
}

var smsplugin = new SmsPlugin();
module.exports = smsplugin;
});

关于android - phonegap SMS 插件中的无效操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com