I was a bit surprised when three people asked me how to do this in one day. I figured if there was enough interest in it then maybe it deserved a post of it's own. Basically you want to know the primary phone number of the device your app is running on. In order to do that in PhoneGap you will need to write a plugin to access the TelephonyManager service on Android.
As always the code for this plugin is is GitHub at my TelephoneNumberPlugin repo. If you'd rather have a more detailed walk through please read on.
First up let's code our JavaScript interface which we will use to get the telephone number. We'll setup a new interface at "cordova/plugin/telephonenumber" to avoid name collisions. Then the rest is fairly boiler plate code where we declare and error object that we won't even use and finally provide a get method. When you call the get method you should provide a success callback. The success callback will be invoked with a string that will contain the devices phone number. If something goes wrong the failure callback will be invoked.
I've extracted the execute method from TelephoneNumber.java which extends from the new CordovaPlugin interface. The method is fairly self explanatory. If the device has a phone number it will be returned. If not the error call back is invoked with the NO_TELEPHONE_NUMBER error code. If you try to call anything other than "get" on this class you'll get a INVALID_ACTION error.
In order to use this plugin you'll need to include the telephonenumber.js in your HTML as a script tag:
and add a line to res/xml/config.xml to tell the JavaScript side where to find your Java class:
and if you don't already have READ_PHONE_STATE as a permission in your AndroidManifest.xml you will need to add it as well.
Now you should be able to get your devices phone number. Here is some sample code that writes the phone number out to the console:

28 comments:
Hi Simon,
great post, thanks!
BTW, can such a plugin be written in iOS/WP7+/BB?
@BorG
Yes, of course. Reuse the JS but each OS will need its own native implementation.
Any particular reason why you would limit this to just the telephone number and not make a more generic plugin? Looking at the API (http://developer.android.com/reference/android/telephony/TelephonyManager.html), there appears to be other things in there that might be of use as well. (Although certainly none as much as the main telephone #.)
@Raymond Camden
No reason other than that was the question that I was trying to answer for the three or so folks who contacted me. It would be pretty easy to add more capabilities to the plugin and since it is up on Github I welcome pull requests from anyone that has done so.
Hey Simon, I have tried your code but no luck, Its working on Android 4.0.4. Not working on below version's plz help me form this issue.
@Vasu Nanguluri
Define, no luck.
Hi Simon,
It was very great to have the article, but i have trying to send the sms in background, but i was not much sucessfull, could you help me in this issue.
could you send me any sample if you have, i have tried the sample in the github i was getting the exception "Uncaught TypeError : Cannot read property 'sms' of undefined".
@jagadesh
What code on github are you using? It sounds like the plugin you are using is not updated for Cordova 2.0.0 or greater.
HI simon,
i need to know is there any database in android storing the total sms count .
if i use content://sms/ it is returning only the available sms count in the mobile but i want overall sms count sent form the mobile from the begining ..
thanks in advance
Sangeeth
Dear Simon,
Could you help me in integrating the Facebook, linked in phonegap, I have tried a lot but had no luck.
Thanks in advance
@sangeeth_LVS
To the best of my knowledge content://sms will only give you the total number of SMS currently on the device. If you want to get an idea of every SMS ever received by the device you'll need to create an app that keeps track of it.
@jagadesh
I hate Facebook with a passion that I usually reserve for opponent hockey teams (think of that cricket team you love to hate). So sorry, no I've never tried to integrate with Facebook.
This is super, thank you.
One question (I'm still new w/phonegap), I'm running cordova-2.3.0, when I add this plugin, I get the error:
02-12 11:14:37.786: E/Web Console(380): module cordova/plugins/telephonenumber not found at undefined:0
in my log. However, the plugin seems to be working? I must have missed something, but I'm at a loss as to what.
@Geoffrey Giordano
That was a mis-spelling on my part. I've fixed it on github. Go grab the lastest JS file for the fix.
Hi Simon,
Thanks for this, but having some difficulty getting this working. I've followed the instructions exactly (or so I believe!) and I'm getting this error:
E/Web Console(12504): Uncaught module cordova/plugin/telephonenumber not found at file:///android_asset/www/cordova-2.3.0.js:48
Any suggestions?
Thanks
Oops, solved it. Like an idiot I put telephonenumber.js in the wrong place. :) Thanks a lot for this plugin!
Hi Simon,
I read your blog. It was very great to have the article. i am new to phonegap and using cordova2.4.0 version. i have one query related to cordova.define(path). what is path here means you are asking (cordova/plugin/ipaddress)
@Navjot Singh
I saw your email on this question. You did not include the script tag for telephonenumber.js.
@simon MacDonald many many thanks to you
Hi simon
i have using cordova2.4.0 and when i run a demo project for testing plugin is working or not getting an error "Falling back on PROMPT mode since _cordovaNative is missing."
when app is run after that i click on app than i got error "Invalid action"
i am using this code with my new cordova 2.4.0 version.
download.macromedia.com/pub/developer/phonegap/myfirstphonegapplugin_a.zip
can you please help me.
Thanks & regards
Navjot singh
Skype : navjotsinghmca
@Navjot Singh
Well when I import that code into Eclipse it runs just fine. However it is using Cordova 1.6.1. If you are running against 2.4.0 with the same code you will run into an issue where some of the API's are deprecated but it should still run. The JS will need to change a bit.
Try...
var exec = cordova.require("cordova/exec");
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeAction", [resultType]);
}
};
@Simon
Could you explain what you meant earlier when you said
"Yes, of course. Reuse the JS but each OS will need its own native implementation." ?
I don't understand how to apply this to iOS.
@xakim
Take the same JS file then write the native plugin for iOS.
http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS
Hi Simon,
I followed all the instruction above and developed app which gets quite a few phone/sim information.
It works on Samsung Galaxy Nexus and does not work on any HTC phone. I googled and found some issue related to hydration. Can you please let me know how I can fix it for HTC ?
I am using Eclipse to build and deploy app on my local box, I have not uploaded my app anywhere.
Any help really appeciated.
Cheers,
Prashanth
@Prashanth Rao
Sorry some vendors don't publish the phone number to the SIM card. You'll have to ask the user for it.
@Simon
Thanks for the info. I will try and see how i can get around few tricky bits.
Cheers,
Prashanth
Hi Simon
Great work, thanks much for the plugin.
I am using cordova 2.7 and trying to console log the phone number, I have tried many sim cards but it returns blank.
Is there a different way to write the JS file for this version of cordova? or will the existing JS work?
Thanks much
Mohan
@Mohan Kuldeep Ponnada
Sorry not every vendor puts the number on the SIM card.
Post a Comment