Getting the Wrong Mobile Prefix in Android - android

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.

Related

android unique id for each device like serial number

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.

Is there any way to get the unique device id from android [duplicate]

This question already has answers here:
Is there a unique Android device ID?
(54 answers)
Closed 8 years ago.
i want unique device id from android device. I want unique device id from android device how to get that one.
yes you can get android device mac id which is known as unique identifier assigned to network interfaces for communications on the physical network segment this is a unique id for each device which cannot be changed however.
you can get it by WIFImanager in android
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
String MACAddress = wifiInfo.getMacAddress();
dont forget to add permission to your manifest file
<uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
I don't think there's any (perfect) way to do this. See link. If you just want to identify the user, you can generate one UUID and store it in your application's storage.
have created once class and use this in my app see pastie
its create unique ID and generate MD5 message as string of unique device ID
public String getUDID(Context c) {
// Get some of the hardware information
String buildParams = Build.BOARD + Build.BRAND + Build.CPU_ABI
+ Build.DEVICE + Build.DISPLAY + Build.FINGERPRINT + Build.HOST
+ Build.ID + Build.MANUFACTURER + Build.MODEL + Build.PRODUCT
+ Build.TAGS + Build.TYPE + Build.USER;
// Requires READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager) c
.getSystemService(Context.TELEPHONY_SERVICE);
// gets the imei (GSM) or MEID/ESN (CDMA)
String imei = tm.getDeviceId();
//gets the android-assigned id you can omit this because
//It's known to be null sometimes, it's documented as "can change upon factory reset".
//Use at your own risk, and it can be easily changed on a rooted phone.
String androidId = Secure.getString(c.getContentResolver(),
Secure.ANDROID_ID);
// requires ACCESS_WIFI_STATE
WifiManager wm = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
// gets the MAC address
String mac = wm.getConnectionInfo().getMacAddress();
// concatenate the string
String fullHash = buildParams + imei + androidId + mac;
return md5(fullHash);
}
md5(String fullHash) Function
public String md5(String toConvert) {
String retVal = "";
MessageDigest algorithm;
try {
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(toConvert.getBytes());
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
retVal = hexString + "";
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}
It's really simple, actually..
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);

getLine1Number() Returns blank, not null [duplicate]

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" />

Android app to get system phone number

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 retrieve SIM card IMSI in Android?

public String getSubscriberId(){
operator = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = operator.getSubscriberId();
return IMSI;
}
simID = (TextView) findViewById(R.id.text2);
simIMSI = getSubscriberId().toString();
if (simIMSI.equals("")){
simID.setText("No SIM card detected!");
}
else{
simID.setText(simIMSI.toString());
SaveUniqueId(simIMSI.toString());
}
I wish to retrieve the phone SIM card IMSI and display in a layout, I run the program using an emulator even though I know emulator does not have SIM card attached but it should have result like "No SIM card detected" right? But why I get error for this coding or is it something wrong in my "getSubscriberId()"?
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
Now let's start with actual source code to retrieve the information:-
public String findDeviceID() {
String deviceID = null;
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
int deviceType = m_telephonyManager.getPhoneType();
switch (deviceType) {
case (TelephonyManager.PHONE_TYPE_GSM):
break;
case (TelephonyManager.PHONE_TYPE_CDMA):
break;
case (TelephonyManager.PHONE_TYPE_NONE):
break;
default:
break;
}
deviceID = m_telephonyManager.getDeviceId();
return deviceID;
}
For more details please refer this site http://ashnatechnologies.blogspot.in/2010/10/how-to-get-imei-on-android-devices.html
You can visit this link http://www.anddev.org/tinytut_-_getting_the_imsi_-_imei_sim-device_unique_ids-t446.html
use following code for IMSI
String imsi = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
This code should work.
String myIMSI =
android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);

Categories

Resources