我需要在phoneGAP eclipse中使用设备 SIM号验证用户输入值。我正在使用 Simon Mac唐纳德电话号码插件:
Telephone Number Plugin 和他的博客文章blog post
当我运行代码时,它总是出错......
代码:
JAVA
package com.simonmacdonald.cordova.plugins;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.content.Context;
import android.telephony.TelephonyManager;
public class TelephoneNumber extends CordovaPlugin {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (action.equals("get")) {
TelephonyManager telephonyManager = (TelephonyManager)this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
String result = telephonyManager.getLine1Number();
if (result != null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
return true;
}
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
}
}
Telephone.Js
cordova.define("cordova/plugin/telephonenumber",
function(require, exports, module) {
var exec = require("cordova/exec");
var TelephoneNumber = function () {};
var TelephoneNumberError = function(code, message) {
this.code = code || null;
this.message = message || '';
//alert("message = " + this.message + this.code);
};
TelephoneNumber.NO_TELEPHONE_NUMBER = 0;
TelephoneNumber.prototype.get = function(success,fail) {
var errorCallback = typeof fail !== 'function' ? null : function(code) {
fail({"code": code});
//alert("TelephoneNumber.prototype.get = ");
};
exec(success,errorCallback,"TelephoneNumber", "get",[]);
};
var telephoneNumber = new TelephoneNumber();
module.exports = telephoneNumber;
//alert("telephoneNumber = " + module.exports);
});
** Device.Ready **
<script type="text/javascript">
var pushNotification;
function onDeviceReady() {
alert("Device Ready");
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
console.log("result = " + result);
alert("result = " + result);
}, function(error) {
console.log("error = " + error.code);
alert("error = " + error.code);
});
}
</script>
如何解决此错误或是否需要搜索其他解决方案才能实现?