Unable to get phone number from a dual sim - android

Unable to get phone number from mobile-:
TelephonyManager tMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Log.i("Log", "Phone no is -: "+mPhoneNumber);
myContactNo.setText(mPhoneNumber);
I am not getting the phone number and it is empty. I have added permission in Android Manifest.

Try this..
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
In AndroidManifest.xml, write the following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
See here

Related

How to fetch device's mobile number programmatically in Android?

This code is not working...It giving null...
TelephonyManager tMgr = (TelephonyManager)HomeActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Log.e("Mobile No. :",""+mPhoneNumber);
Toast.makeText(getApplicationContext(),mPhoneNumber,Toast.LENGTH_SHORT).show();
Here is the code for it:
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
String getSimNumber = telemamanger.getLine1Number();
and you also have to add "READ_PHONE_STATE" permission in Androidmanifest.xml file.
and if you are testing the app on higher OS device then you also have to ask for the permission externally.
hope this will help you. :)
Check in Phone--> Settings --> About --> Phone identity If you are able to view the number then you will get phone number otherwise won't able to get number
I got result by using of below code,
TelephonyManager telephonyMananger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mobNumber = telephonyMananger.getLine1Number();

Android imei number - how to pick it?

developed an app using a mobile phone
used this code to pick imei number
Utill.class
static TelephonyManager telephonyManager;
public static String getDeviceID(){
telephonyManager=(TelephonyManager) MyApplication.getInstance().getSystemService(MyApplication.getInstance().TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
in my job.class
String imeino = Util.getDeviceID();
tested with mobile devises and it works properly but when apk installed a tab (in client side ) imei number does not pick and give a null pointer
Note. for tab i have added different layouts and it also works properly in my tab
is this a matter of base url or
how can i avoid this issue of imei number ?
java Class and add following code
TelephonyManager tel;
TextView imei;
tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = (TextView) findViewById(R.id.textView2);
imei.setText(tel.getDeviceId().toString());
AndroidManifest.xml and add following code
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Have this common method. Devices without SIM slot will return null IMEI. So pick ANDROID_ID.
public String getDeviceID(){
String devcieId;
TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
devcieId = mTelephony.getDeviceId();
}else{
devcieId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
return devcieId;
}
Try this code it will return you IMEI number if device GSM based or if not get back the Android ID.
String devcieId;
TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null){
devcieId = mTelephony.getDeviceId();
}else{
devcieId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID);
}
Also give the read phone state permission in your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Cannot get phone number

I want to get the SIM's phone number .
My Manifest file is..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambre.bazarmada"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SEND_SMS" />
....
</application>
</manifest>
and my code in acivity to get Phone number is as ..
public static String getPhoneNumber(Context ctxt) {
TelephonyManager phoneManager = (TelephonyManager) ctxt.getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
return phoneNumber;
}
The phone is Samsung Galaxy S2 , the problem is that the function getPhoneNumber returns null. So what is wrong in my code?
try this..
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
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.
Please assure you have permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
and use the following code
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
String getSimNumber = telemamanger.getLine1Number();
follow https://developer.android.com/reference/android/telephony/TelephonyManager.html#getLine1Number%28%29 for more and Get my phone number in android

Android Get Phone SIM number

How can I get The SIM card number using code ??
I tried this code and it returned no number
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
use
TelephonyManager phoneManager = (TelephonyManager)
getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();
Needs READ_PHONE_STATE permission.
Try the following code:
TelephonyManager mTelephonyMgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String sDeviceID = mTelephonyMgr.getDeviceId();
String sSimSerial = mTelephonyMgr.getSimSerialNumber();
String sSimlineNumber = mTelephonyMgr.getLine1Number();
Set the following permission
android.permission.READ_PHONE_STATE in Android Manifest file
Try like this..
TelephonyManager telphMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String simSerialNumber = telphMgr.getSimSerialNumber();
Ref :
http://developer.android.com/reference/android/telephony/TelephonyManager.html#getSimSerialNumber%28%29

Retrieve phone Number in android

This is the following code to retrieve phone number wwas able to get it on samsung 4.0.4, but getting below error on htc one v mobile..any clue?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textDeviceID = (TextView)findViewById(R.id.deviceid);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
textDeviceID.setText(getMy10DigitPhoneNumber(telephonyManager));
}
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(TelephonyManager telephonyManager){
String s = getMyPhoneNumber();
return s.substring(0);
}
}
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String mblNumber = mTelephonyMgr.getLine1Number();
Note: Dont forget to add READ_PHONE_STATE permission to be added inside the AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
According to the documentation .getLine1Number() "Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable. "
Apparently .getLine1Number() reads this information from SIM card, so if the operator has set the MSISDN field it will return you its value and null if they did not set this field.
In your case probably your SIM card does not have this field populated by operator.

Categories

Resources