I want to create an phonegap app using this plugin. I have done this steps to create and add plugin :
1:I am creatin this app using phonegap cli :-
phonegap create GetPhone --id "com.phone" --name "GetPhoneApp"
2: Going into the project and buid
cd GetPhone
phonegap local build android
3:Installing plugin
phonegap local plugin add https://github.com/macdonst/TelephoneNumberPlugin
4:moving telephonenumber.js to www folder
5: adding
<script type="text/javascript" charset="utf-8" src="telephonenumber.js"></script>
into index.html then added below ondeviceready :-
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function(error) {
alert("error = " + error.code);
});
6: Added
<plugin name="TelephoneNumber" value="com.simonmacdonald.cordova.plugins.TelephoneNumber"/>
into config.xml in www directory.
7: Build
phonegap local build android
8: Uploaded into build.phonegap.com and created .apk file.
Then when i am running this app on android device it does not alert anything. Is there any wrong step i have taken. I dont know much more about phone gap. After googling i have created this. I have seen this tutorial also to add this plugin. When i am build it locally it does not show any error. But it is also not running.
Please help me find the error.
What are the PhoneGap/Cordova plugins? A plugin is a bit of add-in code that provides JavaScript interface to native components. They allow your application to use native device capabilities beyond what is available to pure web apps.
Install the plugin on the Settings/Preferences | Plugins page, tab Marketplace. PhoneGap and Apache Cordova are frameworks for developing mobile application with single HTML, CSS, and JavaScript/TypeScript code base and targeting various mobile platforms, including Android.
Add Plugin Features The cordova plugin add command requires you to specify the repository for the plugin code. Please note that when you follow the Web Project Dev workflow and use the CLI, the CLI will take care of adding the plugin code to the appropriate place for each platform.
First of all I would suggest you get acquainted with standard tools, especially adb
. These will allow you to provide more information, find out what really is going on.
Skimming through the guide is also helpful, so you can be sure you know what you're doing and why you're doing it. Look at step-by-step debugging of Android code as well.
I would also encourage you to build a Cordova application from sources without using the fancy tools to get the hang of what goes where and how all the components come together. Your source tree is confusing and you seem to have used a mixture of Cordova 3.1 and Cordova 2.9, and even Phonegap? You only need to include cordova.js
and your script (js/script.js
in this case), nothing else at the moment.
Having said that, on to the major issue.
Your call to get
returns an error. Why? Take a quick look at the source code of the plugin:
https://github.com/macdonst/TelephoneNumberPlugin/blob/master/src/com/simonmacdonald/cordova/plugins/TelephoneNumber.java
The plugin retrieves the TelephonyManager
and tries to call the getLine1Number
. And as you can see result != null
is false which drops through to returning a PluginResult.Status.ERROR
, which is why you get your error alert box.
This is the core issue at hand. TelephonyManager
returning a null
for your request. Why? After searching around you might stumble on:
So, navigate over to Settings > About Phone > Status on your device and look at the "My phone number" field. Is it "Unknown"? Tough luck then. My SIM doesn't store the number, so I got the same results as you did.
Another thing to note, is that at least on Android 4.4, the method returns an empty string ""
instead of a null
, so when testing on my Android 2.3.7 device I got a null
and thus got the error branch. But on my Android 4.4.2 device I got a ""
and got the success branch, but the number was empty, obviously.
You can set the number on the emulator by using this pretty advanced guide if you'd like to try. So what can you do? Probably nothing by using the API, not even the Android system itself can get your number if it's not stored on the SIM card. Asking the user or sending an SMS to discover the number via a web service may be an option as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With