I'm trying to retrieve the IMEI number of a phone. I'm using code below:
public static string GetDeviceId(Context context)
{
TelephonyManager telephonyMgr = context.GetSystemService(Context.TelephonyService) as TelephonyManager;
string deviceId = telephonyMgr.DeviceId == null ? "UNAVAILABLE" : telephonyMgr.DeviceId;
return deviceId;
}
Is there any problem with the code? What else should I do?
You'll need to add the following permission in the manifest file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Related
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"/>
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
This question already has answers here:
MSISDN : Is it a SIM Card Data? Why all The Provided Function (from Blackberry and Android) to fetch MSISDN not reliable?
(3 answers)
Closed 5 years ago.
I want to get Mobile Number of Device. I have used following code reference by Alex Volovoy's This Link
TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Log.d("msg", "Phone : "+mPhoneNumber);
OUTPUT in Logcat:
Without Simcard Phones Returns:
02-01 17:22:45.472: D/msg(29102): Phone : null
With Simcard:
02-01 17:22:45.472: D/msg(29102): Phone :
I have also taken permission in <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in AndroidManifest.xml
So what should i do? Is there any Mistake?
To get the phone number from the device , first you have to set your own phone number on the device, just through :
Settings -> About Phone -> Status -> My phone Number
Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:
To avoid this problem , we can catch the error and work accordingly. Like:
TelephonyManager tMgr = (TelephonyManager)
ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);
String MyPhoneNumber = "0000000000";
try
{
MyPhoneNumber =tMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}
if(MyPhoneNumber.equals("")){
MyPhoneNumber = tMgr.getSubscriberId();
}
I have another solution to get a phone number when telephony manager returns blank. I hope it'll help you.
Here is my sample code:
public static final String main_data[] = {
"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"
};
object object = (TelephonyManager) context.getSystemService("phone");
if (!TextUtils.isEmpty(((TelephonyManager) (object)).getLine1Number())) {
}
object = context.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;
}
if (s.equals("vnd.android.cursor.item/phone_v2")) {
String s1 = ((Cursor) (object)).getString(4);
boolean flag1;
if (((Cursor) (object)).getInt(5) > 0) {
flag1 = true;
} else {
flag1 = false;
}
Toast.makeText(SampleActivity.this, "Phone:-" + s1, Toast.LENGTH_LONG).show();
}
} while (true);
((Cursor) (object)).close();
}
Also don't forget to add these two permissions:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
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.
in which cases this method returns a null reference?
Can only depend on the sim card?
Doesn't exist, in these cases, an alternative to retrieve an identification number of the latter?
Thanks to all!
Please make sure whether you have added the following permission in the Android Manifest, if not please add this statement add try again.
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Note: Add this permission tag outside the application tag..
Sample Snippet:
.....
.....
.....
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android......"/>
..........
</manifest>
All the Best
getSimSerialNumber() needs privileged permissions from Android 10 onwards, and third party apps can't register this permission.
See : https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids
A possible solution is to use the TELEPHONY_SUBSCRIPTION_SERVICE from Android 5.1, to retrieve the sim serial number. Steps below:
Check for READ_PHONE_STATE permission.
Get Active subscription list.( Returns the list of all active sim cards)
Retrieve the sim details from Subscription Object.
if ( isPermissionGranted(READ_PHONE_STATE) ) {
String simSerialNo="";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManager subsManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subsList = subsManager.getActiveSubscriptionInfoList();
if (subsList!=null) {
for (SubscriptionInfo subsInfo : subsList) {
if (subsInfo != null) {
simSerialNo = subsInfo.getIccId());
}
}
}
} else {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
simSerialNo = tMgr.getSimSerialNumber();
}
}
Check if this helps
try the below code..i will help full for u..
TelehponyManager manager = (TelehponyManager)getSystemService(TELEPHONY_SERVICE);
String imei = manager.getDeviceId();
String imsi = manager.getSubscriberId();