Get the user phone number with cordova / phonegap

2019-06-03 16:05发布

问题:

I have installed this plugin : https://github.com/8enmann/TelephoneNumberPlugin/

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 ?

回答1:

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

For this to work you need to enable javascript in your app and define function get_imei() in 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;    
}
}

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

Required Permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

It worked for me. Hope it helps.!



回答2:

Looks like this plugin is implemented using TelephonyManager.

Unfortunately, it doesn't work on many devices (particularly, on none of those I've tested)

See for example: TelephonyManager.getLine1Number() failing?



回答3:

You have to add js code :

<script src="cordova.js"></script>
<script src="scripts/telephonenumber.js"></script>

EDITED :

I tried thiese steps to get it worked :

1) I installed the plugin and I I get sure that I have "com.simonmacdonald.telephonenumber" folder inside Plugins folder in my project.

2) I searched and replaced all "cordova/plugin/telephonenumber" with "telephonenumber" in js file in the plugin folder (com.simonmacdonald.telephonenumber).

3) then It didn't work for me, I tried to include "telephonenumber.js" to my project, this file could be found on com.simonmacdonald.telephonenumber\www , so I copy that file to the scripts folder of my project and it works well.

4) now this works :

var telephoneNumber = cordova.require("telephonenumber");
telephoneNumber.get(function (result) {
    alert(result);
    callback(result); //callback function is called here
}, function () {
    alert("error");
});

U should know that this plugin doesn't work on some phones, I tries on ASUS K00E and GT-5830 without result, but I get result on S7562 and N7003.

I think that this plugin is not perfect, Now I'm using SMS sending from clients to get their phonenumber ...



回答4:

I know i am late. After Searching a lot i found this plugin which works for me perfectly... add these plugin in your project

Put these code in your index.js

here $(document).ready(function () {
        document.addEventListener("deviceready", onDeviceReady, false);
 });

 function onDeviceReady() {
        window.plugins.phonenumber.get(success, failed);

        document.addEventListener("backbutton", onBackKeyDown, false);
    }

    function success(phonenumber) {
        console.log("My number is " + phonenumber);
    }

    function failed(phonenumber) {
        console.log("Error " + phonenumber);
    }

If your phone do not allow to show phonenumber then it will go to failed block.

But Please Note That Before Use It: Some vendors don't publish the phone number to the SIM card.

You can check it in Settings-> About Phone-> Status-> SIM Status.