Example usage for android.telephony TelephonyManager getLine1Number

List of usage examples for android.telephony TelephonyManager getLine1Number

Introduction

In this page you can find the example usage for android.telephony TelephonyManager getLine1Number.

Prototype

@SuppressAutoDoc 
@RequiresPermission(anyOf = { android.Manifest.permission.READ_PHONE_STATE,
        android.Manifest.permission.READ_SMS, android.Manifest.permission.READ_PHONE_NUMBERS })
public String getLine1Number() 

Source Link

Document

Returns the phone number string for line 1, for example, the MSISDN for a GSM phone.

Usage

From source file:com.nostra13.example.universalimageloader.HomeActivity.java

private String getMyPhoneNumber() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    return mTelephonyMgr.getLine1Number();
}

From source file:org.openmidaas.app.activities.PushNotificationActivity.java

private String getPhoneNumberFromSIM() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String yourNumber = mTelephonyMgr.getLine1Number();
    return yourNumber;
}

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

@Override
protected void onResume() {
    super.onResume();

    // Only do these checks/changes on KitKat+, the "mSetDefaultSmsLayout" has its visibility
    // set to "gone" in the xml layout so it won't show at all on earlier Android versions.
    if (Utils.hasKitKat()) {
        if (Utils.isDefaultSmsApp(this)) {
            // This app is the default, remove the "make this app the default" layout and
            // enable message sending components.
            mSetDefaultSmsLayout.setVisibility(View.GONE);
            mSendSmsEditText.setHint(R.string.sms_send_new_hint);
            mSendSmsEditText.setEnabled(true);
            mSendSmsButton.setEnabled(true);
        } else {//from   ww w.java 2s. c  om
            // Not the default, show the "make this app the default" layout and disable
            // message sending components.
            mSetDefaultSmsLayout.setVisibility(View.VISIBLE);
            mSendSmsEditText.setText("");
            mSendSmsEditText.setHint(R.string.sms_send_disabled);
            mSendSmsEditText.setEnabled(false);
            mSendSmsButton.setEnabled(false);

            Button button = (Button) findViewById(R.id.set_default_sms_button);
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Utils.setDefaultSmsApp(MainActivity.this);
                }
            });
        }
    }
    String phoneNumberToast = "Unable to obtain phone number (not default SMS app?)";
    final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    try {
        phoneNumberToast = "Phone number: " + telephony.getLine1Number();
    } catch (SecurityException e) {
    }
    Toast.makeText(this, phoneNumberToast, Toast.LENGTH_SHORT).show();
}

From source file:foo.fruitfox.evend.LoginActivity.java

private String getPhoneNumber() {
    String countryCode = "";
    TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    String mRawPhoneNumber = tMgr.getLine1Number();
    String countryISO = tMgr.getSimCountryIso().toUpperCase(Locale.ENGLISH);
    String[] countryCodeList = this.getResources().getStringArray(R.array.CountryCodes);
    String mPhoneNumber;/* w  w  w  .ja  v a2  s . c o m*/

    if (mRawPhoneNumber.startsWith("+") == false) {
        for (int i = 0; i < countryCodeList.length; i++) {
            String[] countryCodePair = countryCodeList[i].split(",");
            if (countryCodePair[1].trim().equals(countryISO.trim())) {
                countryCode = countryCodePair[0];
                break;
            }
        }

        mPhoneNumber = "+" + countryCode + mRawPhoneNumber;
    } else {
        mPhoneNumber = mRawPhoneNumber;
    }

    return mPhoneNumber;
}

From source file:se.goransson.messengerapp.MainActivity.java

protected void connect() {
    TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String localNbr = mTelephonyManager.getLine1Number();
    String localCode = mTelephonyManager.getNetworkCountryIso().toUpperCase(Locale.getDefault());

    if (localNbr != null && localCode != null) {
        phoneNbr = fixInternationalPhoneForSubscribe(getInternaltionalNumber(localNbr, localCode));
    }/*from   w  w w .  jav  a  2  s .  c om*/

    if (phoneNbr != null) {
        client.setHost(getBroker());
        client.setPort(1883);
        client.setId(phoneNbr);
        client.setKeepAlive(7000);
        client.connect();
    } else {
        Toast.makeText(this, R.string.failed_phone, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.mobileglobe.android.customdialer.common.compat.telecom.TelecomManagerCompat.java

/**
 * Return the line 1 phone number for given phone account.
 *
 * @param telecomManager the {@link TelecomManager} to use in the event that
 *    {@link TelecomManager#getLine1Number(PhoneAccountHandle)} is available
 * @param telephonyManager the {@link TelephonyManager} to use if TelecomManager#getLine1Number
 *    is unavailable/*from  w ww .  j  a  v  a  2 s .  c om*/
 * @param phoneAccountHandle the phoneAccountHandle upon which to check the line one number
 * @return the line one number
 */
@Nullable
public static String getLine1Number(@Nullable TelecomManager telecomManager,
        @Nullable TelephonyManager telephonyManager, @Nullable PhoneAccountHandle phoneAccountHandle) {
    if (telecomManager != null && CompatUtils.isMarshmallowCompatible()) {
        if (ActivityCompat.checkSelfPermission(DialerApplication.getContext(),
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        return telecomManager.getLine1Number(phoneAccountHandle);
    }
    if (telephonyManager != null) {
        return telephonyManager.getLine1Number();
    }
    return null;
}

From source file:eu.cyberkat.h2owirelessbalancecheck.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_about:
        PackageInfo pInfo = null;//  w  w  w .j  a v a  2s .c o  m
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            pInfo = null;
        }
        if (pInfo == null) {
            return false;
        }
        String version = pInfo.versionName;
        int versionCode = pInfo.versionCode;
        Context aboutToastContext = getApplicationContext();
        CharSequence aboutToastText = "H2O Data Balance v" + version + ", (" + ordinal(versionCode)
                + " Revision)\nCopyright (c) 2014, Dylan J. Morrison <insidious@cyberkat.eu>, Licensed under the ISC license.";
        int aboutToastDuration = Toast.LENGTH_LONG;
        Toast aboutToast = Toast.makeText(aboutToastContext, aboutToastText, aboutToastDuration);
        aboutToast.show();
        return true;
    case R.id.action_grabnum:
        TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String mPhoneNumber = tMgr.getLine1Number();
        if (mPhoneNumber == null) {
            Context numberErrorContext = getApplicationContext();
            CharSequence numberErrorText = "Unable to get device phone number.";
            int numberErrorDuration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(numberErrorContext, numberErrorText, numberErrorDuration);
            toast.show();
        } else {
            EditText textbox = (EditText) this.findViewById(R.id.editText2);
            mPhoneNumber = mPhoneNumber.replaceAll("[^\\d]", "");
            textbox.setText(mPhoneNumber);
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:org.lumicall.android.sip.RegisterAccount.java

private String getMyPhoneNumber() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String num = mTelephonyMgr.getLine1Number();

    // MSISDN should not have a +, but some phones return it with a +
    // We want to see a +
    if (num != null && num.length() > 0 && !(num.charAt(0) == '+'))
        num = "+" + num;

    /* if(num == null || num.length() == 0) {
       PhoneHelper ph = new PhoneHelper();
       ph.init(this);/*from w  ww .  j a  v a2 s.com*/
       num = ph.getLine1Number();
    } */

    if (num == null || num.length() == 0)
        num = "+" + getCountryCode(mTelephonyMgr.getSimCountryIso());

    return num;
}

From source file:uk.bcu.ItuneActivity.java

private String getMyPhoneNO() {
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    String yourNumber = mTelephonyMgr.getLine1Number();
    return yourNumber;
}

From source file:jp.realglobe.sugo.actor.android.call.MainActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode != PERMISSION_REQUEST_CODE) {
        return;//from w  w  w .j a va 2 s.  c  o  m
    }

    final Set<String> required = new HashSet<>(Arrays.asList(REQUIRED_PERMISSIONS));
    for (int i = 0; i < permissions.length; i++) {
        if (!required.contains(permissions[i])) {
            continue;
        }
        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
            continue;
        }
        required.remove(permissions[i]);
    }

    if (!required.contains(Manifest.permission.READ_SMS)) {
        final TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        this.phoneNumber = manager.getLine1Number();
        Log.i(LOG_TAG, "Phone number is " + this.phoneNumber);
    }

    showPermissionStatus(required.isEmpty());
}