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.
Related
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();
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"/>
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei_no=mngr.getDeviceId();
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
- it will work fine for IMEI Number of android phone but i needs
android Tablet PC SNID No.
Use this method it will return both fore mobile and tablet
public String getImeiNumber()
{
String imei;
final TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null)
imei = mTelephony.getDeviceId(); // *** use for mobiles
else
imei = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); // *** use for
return imei;
}
This question already has answers here:
How to get the device's IMEI/ESN programmatically in android?
(22 answers)
Closed 8 years ago.
I want to get phone serial number by programming to registration configuration of my application and number of users and phones that my app has been installed on them.
Can I get that?
and can I get phone model that my app has been installed on them?
pls refer this
https://stackoverflow.com/a/1972404/951045
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mngr.getDeviceId();
add READ_PHONE_STATE permission to AndroidManifest.xml
public String getIMEI(Context context){
TelephonyManager mngr = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
String imei = mngr.getDeviceId();
return imei;
}
Here is the code:-
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = telephonyManager.getDeviceId();
Log.d(TAG, "getDeviceId() " + deviceId);
phoneType = telephonyManager.getPhoneType();
Log.d(TAG, "getPhoneType () " + phoneType);
try this
final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
Use below code for IMEI:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei= tm.getDeviceId();
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