我正在寻找可以与Phonegap 3.x一起使用的phonegap插件。我需要它才能在Android和iOS中工作。最好是两个都有一个插件,但是我可以使用两个单独的插件也可以。如果可以使用以下命令安装它,则也更可取:

phonegap local plugin add


那里有这样的插件吗?还是有关于如何升级现有sms插件以与phonegap 3.0配合使用的说明?

编辑

我分叉了一个可在2.9上运行的插件的仓库,并试图使其在phonegap 3.x(https://github.com/aharris88/phonegap-sms-plugin)中运行
到目前为止,我可以使用以下命令将其放入我的phonegap项目中

phonegap local plugin add https://github.com/aharris88/phonegap-sms-plugin


并将其所需的权限正确地放置在AndroidManifest.xml中,并将该功能放置在res / xml / config.xml中,但是当我在手机上安装该功能时,它并没有说它需要发送文本的权限,而我没有无法从此代码获得任何成功或错误消息:

var number = $('#number').val();
var message = $('#text').val();
alert("Send text to "+number+" with message: "+message);
SmsPlugin.prototype.send(number, message, '',
    function () {
        alert('Message sent successfully');
    },
    function (e) {
        alert('Message Failed:' + e);
    }
);

最佳答案

调试它的最佳方法是使用ADT(Android开发人员工具)。有很多小错误。本文是非常有用的资源:http://devgirl.org/2013/09/17/how-to-write-a-phonegap-3-0-plugin-for-android/

这是sms.java代码:

package com.adamwadeharris.sms;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;

public class Sms extends CordovaPlugin {
    public final String ACTION_SEND_SMS = "send";

    @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{
                    send(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.putExtra("address", phoneNumber);
        sendIntent.setType("vnd.android-dir/mms-sms");
        this.cordova.getActivity().startActivity(sendIntent);
    }

    private void send(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);
    }
}


这是sms.js代码:

var sms = {
    send: function(phone, message, method, successCallback, failureCallback) {
        cordova.exec(
            successCallback,
            failureCallback,
            'Sms',
            'send',
            [phone, message, method]
        );
    }
}

module.exports = sms;


这是plugin.xml:



<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
           id="com.adamwadeharris.sms"
      version="0.1.0">
    <name>Sms</name>
    <description>Cordova SMS Send Plugin</description>
    <license>MIT</license>
    <keywords>cordova,phonegap,sms</keywords>


    <js-module src="www/sms.js" name="Sms">
        <clobbers target="window.sms" />
    </js-module>

    <!-- android -->
    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Sms">
                <param name="android-package" value="com.adamwadeharris.sms.Sms"/>
            </feature>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/manifest">
            <uses-permission android:name="android.permission.SEND_SMS" />
        </config-file>

        <source-file src="src/android/Sms.java" target-dir="src/com/adamwadeharris/sms" />
    </platform>

</plugin>


编辑
另外,我在github上有可用的插件:https://github.com/aharris88/phonegap-sms-plugin

编辑
该插件现已移至:
https://github.com/cordova-sms/cordova-sms-plugin

关于cordova - 适用于Android和iOS的SMS Phonegap 3.0插件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18771095/

10-15 06:40