How to get my phone number? [duplicate] - android

How can I get my phone number in Android?
When I use:
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
and use:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
it returns null, why?

From the documentation:
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
So you have done everything right, but there is no phone number stored.
If you get null, you could display something to get the user to input the phone number on his/her own.

If the function you called returns null, it means your phone number is not registered in your contact list.
If instead of the phone number you just need an unique number, you may use the sim card's serial number:
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();

As Answered here
Use below code :
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, give the following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.
Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!
Suggested Workaround:
Get the user's phone number as manual input from the user.
Send a code to the user's mobile number via SMS.
Ask user to enter the code to confirm the phone number.
Save the number in sharedpreference.
Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.

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

Method 1:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
With below permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Method 2:
There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.
Try below code:
String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
main_data, "mimetype=?",
new String[]{"vnd.android.cursor.item/phone_v2"},
"is_primary DESC");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext())
break;
String s1 = ((Cursor) (object)).getString(4);
} while (true);
((Cursor) (object)).close();
}
You will need to add these two permissions.
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
Hope this helps,
Thanks!

Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.
editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
TelephonyManager tMgr;
tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
if (mPhoneNumber != null){
editTextPhoneNumber.setText(mPhoneNumber);
}

Related

how to get info of second sim in android

By using this below method i am getting information of sim one
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
// Get the SIM country ISO code
String simCountry = telephonyManager.getSimCountryIso();
Log.e("Show:", simCountry);
// Get the operator code of the active SIM (MCC + MNC)
String simOperatorCode = telephonyManager.getSimOperator();
Log.e("Show:", simOperatorCode);
// Get the name of the SIM operator
String simOperatorName = telephonyManager.getSimOperatorName();
Log.e("Show:", simOperatorName);
// Get the SIM’s serial number
String simSerial = telephonyManager.getSimSerialNumber();
Log.e("Show:", simSerial);
// Get the phone number
String mPhoneNumber = telephonyManager.getLine1Number();
Log.e("Show:", mPhoneNumber);
how to get information of second sim of dual sim phone?
// Get the phone number
String mPhoneNumber = telephonyManager.getLine1Number();
Log.e("Show:", mPhoneNumber);
work on some device
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
So what should i do? Is there any Mistake?
From Android API 22 SubscriptionMananger is available
You just need to call
SubscriptionManager.from(context).getActiveSubscriptionInfo(1)
to get info about second sim card

Get my phone number in android

How can I get my phone number in Android?
When I use:
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
and use:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
it returns null, why?
From the documentation:
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
So you have done everything right, but there is no phone number stored.
If you get null, you could display something to get the user to input the phone number on his/her own.
If the function you called returns null, it means your phone number is not registered in your contact list.
If instead of the phone number you just need an unique number, you may use the sim card's serial number:
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
As Answered here
Use below code :
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, give the following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But remember, this code does not always work, since Cell phone number is dependent on the SIM Card and the Network operator / Cell phone carrier.
Also, try checking in Phone--> Settings --> About --> Phone Identity, If you are able to view the Number there, the probability of getting the phone number from above code is higher. If you are not able to view the phone number in the settings, then you won't be able to get via this code!
Suggested Workaround:
Get the user's phone number as manual input from the user.
Send a code to the user's mobile number via SMS.
Ask user to enter the code to confirm the phone number.
Save the number in sharedpreference.
Do the above 4 steps as one time activity during the app's first launch. Later on, whenever phone number is required, use the value available in shared preference.
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
Method 1:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
With below permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Method 2:
There is another way you will be able to get your phone number, I haven't tested this on multiple devices but above code is not working every time.
Try below code:
String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
main_data, "mimetype=?",
new String[]{"vnd.android.cursor.item/phone_v2"},
"is_primary DESC");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext())
break;
String s1 = ((Cursor) (object)).getString(4);
} while (true);
((Cursor) (object)).close();
}
You will need to add these two permissions.
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
Hope this helps,
Thanks!
Robi Code is work for me, just put if !null so that if phone number is null, user can fill the phone number by him/her self.
editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhoneNumber);
TelephonyManager tMgr;
tMgr= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
if (mPhoneNumber != null){
editTextPhoneNumber.setText(mPhoneNumber);
}

How to get current SIM card number in Android?

I want to know user mobile number in Android. I used this code but I'm not getting number.
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String n = tm.getLine1Number();
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Is it compulsory to save number in android mobile settings --> about phone --> status --> myphone number
Any idea on this?
I think sim serial Number and sim number is unique. You can try this for get sim serial number and get sim number and Don't forget to add permission in manifest file.
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
String getSimNumber = telemamanger.getLine1Number();
And add below permission into your Androidmanifest.xml file.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Let me know if there is any issue.
Getting the Phone Number, IMEI, and SIM Card ID
TelephonyManager tm = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
For SIM card, use the getSimSerialNumber()
//---get the SIM card ID---
String simID = tm.getSimSerialNumber();
if (simID != null)
Toast.makeText(this, "SIM card ID: " + simID,
Toast.LENGTH_LONG).show();
Phone number of your phone, use the getLine1Number() (some device's dont return the phone number)
//---get the phone number---
String telNumber = tm.getLine1Number();
if (telNumber != null)
Toast.makeText(this, "Phone number: " + telNumber,
Toast.LENGTH_LONG).show();
IMEI number of the phone, use the getDeviceId()
//---get the IMEI number---
String IMEI = tm.getDeviceId();
if (IMEI != null)
Toast.makeText(this, "IMEI number: " + IMEI,
Toast.LENGTH_LONG).show();
Permissions needed
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Update: This answer is no longer available as Whatsapp had stopped exposing the phone number as account name, kindly disregard this answer.
=================================================================================
Its been almost 6 months and I believe I should update this with an alternative solution you might want to consider.
As of today, you can rely on another big application Whatsapp, using AccountManager. Millions of devices have this application installed and if you can't get the phone number via TelephonyManager, you may give this a shot.
Permission:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Code:
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
ArrayList<String> googleAccounts = new ArrayList<String>();
for (Account ac : accounts) {
String acname = ac.name;
String actype = ac.type;
// Take your time to look at all available accounts
System.out.println("Accounts : " + acname + ", " + actype);
}
Check actype for whatsapp account
if(actype.equals("com.whatsapp")){
String phoneNumber = ac.name;
}
Of course you may not get it if user did not install Whatsapp, but its worth to try anyway.
And remember you should always ask user for confirmation.
You have everything right, but the problem is with getLine1Number() function.
getLine1Number()- this method returns the phone number string for line
1, i.e the MSISDN for a GSM phone. Return null if it is unavailable.
this method works only for few cell phone but not all phones.
So, if you need to perform operations according to the sim(other than calling), then you should use getSimSerialNumber(). It is always unique, valid and it always exists.
You can use the TelephonyManager to do this:
TelephonyManager t = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = t.getLine1Number();
Have you used
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

How do i get the carrier name of an incoming number in android..?

I am able to get the carrier name using the following snippet :
TelephonyManager telephonyManager = ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE));
String operatorName = telephonyManager.getNetworkOperatorName();
It works really fine.
I am also able to get the incoming call number using the following snippet:
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
}
}
}
I want to find out the carrier name / service provider name of an incoming number.
How can I achieve that?
Is that possible to get the incoming number's location, say, for example the country?
It's not possible to get the carrier name of a whatever mobile number neither with the Android API. At least is not that easy (and you could fall into some privacy related issue i think).
Read this article for more information:
http://sms411.net/2006/07/finding-out-someones-carrier/
Of course you can try to find the original carrier (using the prefix), but that could be different from the actual one...
It is not possible to retrieve the details of the carrier of a calling party programatically. Also, because of number portability, it is not possible to retrieve the carrier's name from the caller's phone number. You can, however, get the country information from the first few digits of the number. Refer List of country calling codes for more information.
from API Level 22 (Andrid 5.1) This APIs are available.
SubscriptionManager subscriptionManager = (SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) {
for (SubscriptionInfo info : subscriptionInfoList) {
String carrierName = info.getCarrierName().toString();
String mobileNo = info.getNumber();
String countyIso = info.getCountryIso();
int dataRoaming = info.getDataRoaming();
}
}

Retrieving SIM ID

After searching the API and no luck, mybe anyone know how could I retrieve the SIM ID of the device?
thanks,
ray.
Here is the code to get International Mobile Subscriber Identity (IMSI No.) id and phone id (IMEI No.) and Sim No. programmatically
Before doing this also set the user permission in the manifest file "android.permission.READ_PHONE_STATE"
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
String imei = mTelephonyMgr.getDeviceId();
String simno = mTelephonyMgr.getSimSerialNumber();
Log.v("", ""+imsi);
Log.v("", ""+imei);
Log.v("", ""+simno);
Frrom wikiPedia
The Id = Issuer identification number
(IIN) Maximum of seven digits: Major
industry identifier (MII), 2 digits,
89 for telecommunication purposes.
Country code, 1-3 digits, as defined
by ITU-T recommendation E.164. Issuer
identifier, 1-4 digits.
so the API is :
public String getSimCountryIso ()
public String getSimSerialNumber ()
public String getSubscriberId ()
To retrieve the IMSI (subscriber ID in the SIM), Use the getSubscriberId method in the TelephonyManager API.

Categories

Resources