Installation of plugin in phonegap app

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.


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:

  • Retrieve phone Number in android
  • TelephonyManager.getLine1Number() failing?
  • Reading device phone number throws NULLPointerException
  • 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.


    Step number 5 above will not work unless the code is called after the 'deviceready' event is triggered.

    you will need some sort of call to know when the document is loaded, you can do either:

    <body onload="onLoad()">
    <script>
    function onLoad() {
        document.addEventListener('deviceready', deviceReady, false);
    }
    </script>
    

    OR (w/ jquery):

    <script>
    $(document).ready(function () {
        document.addEventListener('deviceready', deviceReady, false);
    }
    </script>
    

    and then create this function:

    function deviceReady() {
        var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
        telephoneNumber.get(function(result) {
        alert("result = " + result);
        }, function(error) {
        alert("error = " + error.code);
        });
    }
    

    This will ensure that your document has loaded and that cordova is ready to call to.

    链接地址: http://www.djcxy.com/p/53412.html

    上一篇: 科尔多瓦:脸谱插件为Android生成错误

    下一篇: 在phonegap应用程序中安装插件