0

I want to get device phone number in phonegap application.How can I do? I use "cordova/plugin/telephonenumber" but I couldn't. Which is more usefull for me while developing app use of eclipse or use dreamveaver etc. editor?

Can I use plugin while using dreamveawer editor?

Thanks...

Kaan Ozdogan
  • 1
  • 1
  • 4
  • it's just possible in a few android devices, so, just ask for it to the user and use some service like twitter digits to validate the number – jcesarmobile Oct 09 '15 at 07:33

1 Answers1

0

You can't get the device phone number in iOS (the API for doing this was deprecated for iOS 6 and higher). For Android there are APIs (check out TelephonyManager) for this that you could use in a plugin.

Take a look at the plugin development guide for how to structure a plugin, but the Java code needed for Android would look something like this:

package com.mycompany.devicenumber;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; 
import org.json.JSONArray;

import android.content.Context;
import android.telephony.TelephonyManager;

public class DeviceNumber extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        if (action.equals("getDeviceNumber")) {
            getDeviceNumber(callbackContext);
            return true;
        }

        // Method not found. 
        return false;
    }

    private void getDeviceNumber(CallbackContext callbackContext) {
        // Get the device number
        TelephonyManager telephonyManager = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String result = telephonyManager.getLine1Number();
        if (result != null) {
            callbackContext.success(result);
        } else {
            callbackContext.error("Failed to get phone number from device.");
        }
    }
}

Regarding editors, use what you are comfortable with for editing a mix of Javascript, HTML, CSS, JSON, XML and Java / Objective C files. I personally find Sublime Text perfect for my needs.

Simon Prickett
  • 3,838
  • 1
  • 13
  • 26