使用cordova/phonegap获取用户电话号码 [英] Get the user phone number with cordova / phonegap
问题描述
我已经安装了此插件: https://github.com/8enmann/TelephoneNumberPlugin/
I have installed this plugin : https://github.com/8enmann/TelephoneNumberPlugin/
并将此代码放入我的angularjs控制器中:
and put this code in my angularjs controller:
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function() {
alert("error");
});
什么也没发生,我的代码坏了...
nothing happens and my code it's broken...
我看到这是一个比原始版本更近的叉子,我认为它工作得很好.我犯了一个错误,或者最新的科尔多瓦版本存在错误?
i see it's a fork more recent than original and i thinked it's working good. I made a mistake or there is a bug with the most recent cordova version ?
推荐答案
嘿,您可以尝试在Java中创建自己的代码,并使用JavaScript来使用它们.
例如,我使用此javascript代码在我的应用程序之一中获取IMEI:
Hey you can try creating your own code in java and use them using javascript.
For example I use this javascript code to get the IMEI in one of my app's:
$imei=window.YourActivityName.get_imei();
要使其正常工作,您需要在应用中启用javascript并在Java中定义函数get_imei().
For this to work you need to enable javascript in your app and define function get_imei() in Java.
您的Java应该看起来像这样:
Your Java should look something like:
public class YourActivityName extends CordovaActivity
{
.........
public void onCreate(Bundle savedInstanceState)
{
.......
appView.addJavascriptInterface(this, "YourActivityName");
super.loadUrl(Config.getStartUrl(), 10000);
.......
}
//Define function to return imei in Java:
@JavascriptInterface
public String get_imei() {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
return imei;
}
}
您可以将java中的以下代码替换为imei代码,如下所示:
you can replace imei code with below code in java as
//Define function to return Number in Java:
@JavascriptInterface
public String get_number() {
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
return mPhoneNumber;
}
所需权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
对我有用.希望能帮助到你.!
It worked for me. Hope it helps.!
这篇关于使用cordova/phonegap获取用户电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!