I have a samsung j5. I am using the Serial Number that can be found in
Settings > About Device > Status > Serial Number.
It has a value of RF8H933KWLF which I assume to be unique. Now I register this value to my database and when I run my app I send a request to my database and check if the device I use is registered in my database. The way I get the Serial Number of device is:
String serialNumber = "";
try {
Class <? > c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
serialNumber = (String) get.invoke(c, "sys.serialnumber", "Error");
if (serialNumber.equals("Error")) {
serialNumber = (String) get.invoke(c, "ril.serialnumber", "Error");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
For my device I can get a correct value.
My question is why when I use a different device I cant get the value that I see in the Serial Number.
I tried on device and it returns Error.
What could be an alternative ID I can use that is unique to a device (does not change even if device is restored or rooted).
I need the ID to be visible so before hand I can register it to my database to allow access to app's login.
refer this you can find many other ways Is there a unique Android device ID?
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
You can use the combination of there phone number and IMEI number to get the unique Id.
You can ask phone number to user and also device can show IMEI numbers.
To get IMEI number and phone number programmatically do something like this.
TelephonyManager teleMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI number
String imei = teleMan.getDeviceId();
//get The Phone Number
String phone = teleMan.getLine1Number();
Hope this solves it.
Note :- I haven't tried this yet.
Related
Can I verify whether a device is a real android device or an emulator from only the information of the device ID? I read that you get NULL if you try to get the value in an emulator although I tried it and I get different values each time.
String android_id = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
you can check the System Properties usertype
in most emulators it will return "userdebug" and on normal devices will return "user"
NOTE : on some rooted devices it may return "userdebug" too
public static String getUserType() {
String uType = "-";
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
uType = (String) get.invoke(c, "ro.build.type", "-");
} catch (Exception ignored) {}
return uType;
}
I am making an app where I will get user's complete phone number from the sim - including the country and mobile prefixes. The phone number I have is 061555555, so when I save it to server it should look like this: +38761555555.
And here is my problem - when I use the code from below I get the following: +387218900032555555, ie. instead of 061 becoming 61, it becomes 218900032. This line number gives the wrong number:
MyPhoneNumber = tMgr.getLine1Number();
I have also tried this and this.
This is my code:
// Get users phone number
private String getMyPhoneNumber() {
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Get the SIM country ISO code
String simCountry = tMgr.getSimCountryIso().toUpperCase();
// Get the operator code of the active SIM (MCC + MNC)
String simOperatorCode = tMgr.getSimOperator();
// Get the name of the SIM operator
String simOperatorName = tMgr.getSimOperatorName();
// Get the SIM’s serial number
String simSerial = tMgr.getSimSerialNumber();
String MyPhoneNumber = "0000000000";
try {
MyPhoneNumber = tMgr.getLine1Number();
} catch (NullPointerException ex) {
}
if (MyPhoneNumber.equals("")) {
MyPhoneNumber = tMgr.getSubscriberId();
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber countryNumberProto = null;
try {
countryNumberProto = phoneUtil.parse(MyPhoneNumber, simCountry);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
String myPhone = phoneUtil.format(countryNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
// I tried INTERNATIONAL and NATIONAL as well
return myPhone;
}
I think you are parsing "national_number" instead of "country_code". Check your JSON parsing code fields logic
{
"country_code": 41,
"national_number": 446681800
}
https://github.com/googlei18n/libphonenumber
I checked my settings->status and found out my phone number is unknown, and instead of my phone number it displays IMEI, so I guess I will have the user confirm his phone number by entering it manually.
getDeviceId() returns the 14 digit MEID on Verizon phones (because it is a CDMA voice device). Is there a programmatic way to get the 15 digit IMEI (as it is listed in the Settings menu) instead?
Disclaimer: Solution uses non-published APIs. This does not represent best practice and can cause unintended results. API may not be implemented or may change. Use at your own risk.
There is a way to do this with reflection and a hidden Android API call. TelephonyManager has a public (but hidden) method getImei(). Not ideal, but the following works for my particular need.
private String getIMEI() throws NoIMEIException {
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
try {
Method method = mTelephonyMgr.getClass().getMethod("getImei");
String imei = (String) method.invoke(mTelephonyMgr);
if (imei == null) {
throw new NoIMEIException();
} else {
return imei;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new NoIMEIException();
}
}
I'm Trying the following code to get System phone number
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
But it is not working.
In my phone, i use
Settings->About Phone-->Status-Phone Number
is there a simple app i can use ? , I m writing from Turkey. does sim card make a difference ?
Not all telecom carriers store the phone number on the SIM card. The approach you are using only works if the SIM card in the device has the mobile number stored on it.
If you cannot see your phone number in the mobile settings, then it means that it isn't stored on the SIM card. You will have to manually ask the user to enter it into your app if you absolutely need it. There is no other way.
You can try something like this. It works perfectly.
public String getPhoneNumber(Context context) {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String num = mTelephonyMgr.getLine1Number();
return fillPhoneNumber(num);
}
private String fillPhoneNumber(String num) {
try{
if (num != null && num.length() > 0) {
if (num.length() >= 9) {
num = num.replaceAll("\\D", "");
if (num.substring(0, 1).equals("8")) {
num = "+3" + num;
} else if (num.substring(0, 1).equals("0")) {
num = "+38" + num;
} else if (num.substring(0, 1).equals("3")) {
num = "+" + num;
}
}
return num;
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
Note: Not all phones return a phone number.
There is no deterministic way to find the MSISDN. The above approach only works if SIM has stored the MSISDN. Other work around is to retrieve the number from facebook. It also may or may not work.
The only sure shot way is to create account with some SMS gateway provider like http://trumpia.com, then send SMS to the toll free number, and then call the API to retrieve the MSISDN.
How to read the device serial number (not IMEI) of a Samsung Android phone (same value that you get when you call 'adb devices') but from within an app or from the device; not using PC/USB/adb.
I found a solution for HTC and other devices, which is - to call
getprop ro.serialno
in a terminal
as described here,
http://groups.google.com/group/android-developers/browse_thread/thread/3d57b1a214cdf928
but it doesn't work on a Samsung Galaxy S.
Edit: You may also want to check my other answer on this topic.
Not sure whether that was possible when the question was first posted. 3 years later however, this is possible:
public static String getManufacturerSerialNumber() {
String serial = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
serial = (String) get.invoke(c, "ril.serialnumber", "unknown");
} catch (Exception ignored) {}
return serial;
}
I conclude that it's not possible on Samsung.
You can use the getprop command on the adb shell and check yourself that which of the files contains the correct Serial number.Many times the serial number is located on different files and code has to be device specific.
Foe samung Tab 3 you can use the following code:
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
serialnum = (String) (get.invoke(c, "sys.serialnumber", "unknown"));
} catch (Exception ignored) {
serialnum = "unknown";
}