Example usage for android.telephony PhoneNumberUtils normalizeNumber

List of usage examples for android.telephony PhoneNumberUtils normalizeNumber

Introduction

In this page you can find the example usage for android.telephony PhoneNumberUtils normalizeNumber.

Prototype

public static String normalizeNumber(String phoneNumber) 

Source Link

Document

Normalize a phone number by removing the characters other than digits.

Usage

From source file:com.example.android.phonecallingsamplechallenge.MainActivity.java

/**
 * Uses an implicit intent to make the phone call.
 * Before calling, checks to see if permission is granted.
 *
 * @param view View that was clicked.//from ww w .j  a v a  2  s . c  om
 */
public void callNumber(View view) {
    String normalizedPhoneNumber;
    // Find the editText_main view and assign it to editText.
    EditText editText = (EditText) findViewById(R.id.editText_main);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Log.d(TAG, "Running version earlier than Lollipop. Can't normalize number.");
        normalizedPhoneNumber = editText.getText().toString();
    } else {
        normalizedPhoneNumber = PhoneNumberUtils.normalizeNumber(editText.getText().toString());
    }
    // Use format with "tel:" and phone number to create phoneNumber.
    String phoneNumber = String.format("tel: %s", normalizedPhoneNumber);
    // Log the concatenated phone number for dialing.
    Log.d(TAG, getString(R.string.dial_number) + phoneNumber);
    Toast.makeText(this, getString(R.string.dial_number) + phoneNumber, Toast.LENGTH_LONG).show();
    // Create the intent.
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    // Set the data for the intent as the phone number.
    callIntent.setData(Uri.parse(phoneNumber));
    // If package resolves to an app, check for phone permission,
    // and send intent.
    if (callIntent.resolveActivity(getPackageManager()) != null) {
        checkForPhonePermission();
        startActivity(callIntent);
    } else {
        Log.e(TAG, "Can't resolve app for ACTION_CALL Intent.");
    }
}

From source file:org.voltdb.MainActivity.java

private String validateInput() {
    // Validate phone number
    String phoneStr = mPhoneEdit.getText().toString();
    if (phoneStr == null || phoneStr.isEmpty()) {
        phoneStr = mPhoneEdit.getHint().toString();
    }//from   w  w  w . jav a 2s . c o  m
    try {
        mPhoneNumber = Long.parseLong(PhoneNumberUtils.normalizeNumber(phoneStr)) % 10000000000l;
    } catch (NumberFormatException e) {
        return getString(R.string.invalid_phone);
    }

    // Validate Contestant id
    try {
        mContestantId = Integer.parseInt(mContestant.getText().toString());
    } catch (NumberFormatException e) {
        return getString(R.string.invalid_contestant_id);
    }

    // Location
    if (BuildConfig.voter_has_location && mLocation == null) {
        // Try one more time
        try {
            mLocation = mLocationManager.getLastKnownLocation(mLocationProvider);
            onLocationChanged(mLocation);
        } catch (SecurityException se) {
            Toast toast = Toast.makeText(getApplicationContext(),
                    getString(R.string.no_geo_location_permissions), Toast.LENGTH_SHORT);
            toast.show();
        }
        if (mLocation == null) {
            return getString(R.string.invalid_location);
        }
    }

    return VALIDATION_SUCCESS;
}